Is there a simple way to reformat my HTML from within Komodo Edit or to automate the process against Tidy?
Something like the Ctrl+K, Ctrl>
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 the 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 the entire buffer and pipe it into the formatter.
komodo.doCommand('cmd_selectAll');
ko.run.runEncodedCommand(window, formatter + " {'insertOutput': True, 'operateOnSelection': True}");
// Restore the 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 in line 5: komodo.document.language becomes komodo.view.language and line 40: Run_RunEncodedCommand becomes ko.run.runEncodedCommand