Komodo Edit - HTML Reformatting / Tidy

最后都变了- 提交于 2019-12-03 04:06:59

If you want a solution that just straight up works, do the following:

Pop open the toolbox panel on the right Click on the gear and select New Macro, name it what you like.

Get the macro code here:

komodo edit macro

It includes the code from http://jsbeautifier.org/ and works like a charm...

Next is to set up a keystroke:

Select your new macro in the toolbox Now go to key bindings

Type a sequence and it will tell you if the sequence you typed is available. I use ctrl+/ because they are near each other.

Cheers!

I found this formatting script (macro) and adapted it for my personal use with the latest Komodo Edit (v6.1.0). It works well and I included the JavaScript formatting provided by a commentator but I think it may only work with Komodo IDE. It's unimportant for my purposes. Perhaps someone out there can find a universal improvement (using something like html tidy).

komodo.assertMacroVersion(3);
if (komodo.view) { komodo.view.setFocus(); }

var formatter;
var language = komodo.document.language;
switch (language) {
    case 'Perl':
        formatter = 'perltidy -i=2 -pt=2 -l=0';
        break;
    case 'XML':
    case 'XUL':
    case 'XLST':
        formatter = 'tidy -q -xml -i -w 80';
        break;
    case 'HTML':
        formatter = 'tidy -q -asxhtml -i -w 120';
        break;
  //case 'JavaScript':
  //    ko.views.manager.currentView.scimoz.selectAll();
  //    ko.views.manager.currentView.scimoz.replaceSel(js_beautify(ko.views.manager.currentView.scimoz.text, {indent_size: 2}));
  //    return null;
  default:
        alert("I don't know how to tidy " + language);
        return null;
}

//save current cursor position
var currentPos = komodo.editor.currentPos;

try {
    // Save the file.  After the operation you can check what changes where made by
    // File -> Show Unsaved Changes
    komodo.doCommand('cmd_save');

    // Group operations into a single undo
    komodo.editor.beginUndoAction();

    // Select entire buffer & pipe it into formatter.
    komodo.doCommand('cmd_selectAll');
    Run_RunEncodedCommand(window, formatter + " {'insertOutput': True, 'operateOnSelection': True}");

     // Restore cursor.  It will be close to the where it started depending on how the text was modified.
     komodo.editor.gotoPos(currentPos);

    // On windows, when the output of a command is inserted into an edit buffer it has unix line ends.
    komodo.doCommand('cmd_cleanLineEndings');
}
catch (e) {
    alert(e);
}
finally {
    // Must end undo action or may corrupt edit buffer
    komodo.editor.endUndoAction();
}

You can setup a command to run to replace a selection of html with the tidy version. Press Ctl+R to bring up the command window and enter tidy -utf8 -asxhtml -i for the command which formats indented xhtml using utf8 encoding. Check the two boxes to "Pass selection as input" and "Insert output". You can also specify custom key bindings there.

Example screenshot http://grab.by/8C3t

The answer that TAOcode made is great, but in newer versions of Komodo a few things have changed, so here is my update to the code to make it work again:

komodo.assertMacroVersion(3);
if (komodo.view) { komodo.view.setFocus(); }

var formatter;
var language = komodo.view.language;
switch (language) {
    case 'Perl':
        formatter = 'perltidy -i=2 -pt=2 -l=0';
        break;
    case 'XML':
    case 'XUL':
    case 'XLST':
        formatter = 'tidy -q -xml -i -w 500';
        break;
    case 'HTML':
        formatter = 'tidy -q -asxhtml -i -w 120';
        break;
  //case 'JavaScript':
  //    ko.views.manager.currentView.scimoz.selectAll();
  //    ko.views.manager.currentView.scimoz.replaceSel(js_beautify(ko.views.manager.currentView.scimoz.text, {indent_size: 2}));
  //    return null;
  default:
        alert("I don't know how to tidy " + language);
        return null;
}

//save current cursor position
var currentPos = komodo.editor.currentPos;

try {
    // Save the file.  After the operation you can check what changes where made by
    // File -> Show Unsaved Changes
    komodo.doCommand('cmd_save');

    // Group operations into a single undo
    komodo.editor.beginUndoAction();

    // Select entire buffer & pipe it into formatter.
    komodo.doCommand('cmd_selectAll');
    ko.run.runEncodedCommand(window, formatter + " {'insertOutput': True, 'operateOnSelection': True}");

     // Restore cursor.  It will be close to the where it started depending on how the text was modified.
     komodo.editor.gotoPos(currentPos);

    // On windows, when the output of a command is inserted into an edit buffer it has unix line ends.
    komodo.doCommand('cmd_cleanLineEndings');
}
catch (e) {
    alert(e);
}
finally {
    // Must end undo action or may corrupt edit buffer
    komodo.editor.endUndoAction();
}

The big differences are line 5: komodo.document.language becomes komodo.view.language and line 40: Run_RunEncodedCommand becomes ko.run.runEncodedCommand

Want a tabs instead of spaces?

In addition to what @justquick said, do a find/replace (Ctrl + h). Replacing a double space () with a tab (\t , make sure regex is ticked) to have the html tabbed instead of being spaced. Tidy uses two spaces by default, you'll have to change your find if you have configured Tidy differently.

1 Goto Toolbox=>Add=>New Command

2 Enter the tidy command line arguments in the Run field:

tidy -config tidy_config_html.txt

3 Check all the boxes

4 Enter the path to tidy in the Start In field

5 Click the Key Binding tab

6 Use Ctrl+1 as the New Key Sequence

7 Press Ctrl+A,Ctrl+1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!