Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Komodo Edit - HTML reformatting / Tidy

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+D in Visual Studio would be brilliant. I am presently running Ubuntu with Tidy installed.

like image 815
Dave Avatar asked Jan 14 '11 08:01

Dave


2 Answers

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 (404)

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.

like image 170
Chris Fletcher Avatar answered Nov 11 '22 06:11

Chris Fletcher


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 and 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 we may corrupt edit buffer
    komodo.editor.endUndoAction();
}
like image 7
TAOcode Avatar answered Nov 11 '22 06:11

TAOcode