Created
January 28, 2011 01:11
-
-
Save sams/799633 to your computer and use it in GitHub Desktop.
Komodo Macro to reformat a selection of markup within a mixed html/php file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Aim to expand this to work with cake ctp files to reformat (just an idea) | |
// has issues if tag attribs are echo'd | |
// does not indent; the origin might | |
// set the path to tidy back to env var | |
// the origin - http://community.activestate.com/forum/code-beautifier-mixed-htmlphp | |
var ke = komodo.editor | |
if (komodo.view.scintilla) { komodo.view.scintilla.focus(); } | |
var currentPos = komodo.editor.currentPos; | |
try { | |
var tidycmd = 'c:\\tidy.exe --input-xml y --indent auto --indent-spaces 2 --new-pre-tags textarea --wrap-php n --wrap 0 --break-before-br y --show-body-only auto --doctype auto --show-warnings n --quiet y'; | |
// 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 | |
ke.beginUndoAction(); | |
Run_RunEncodedCommand(window, tidycmd + " {'insertOutput': True, 'operateOnSelection': True}"); | |
komodo.doCommand('cmd_cleanLineEndings'); | |
var text = komodo.editor.selText; | |
//FIX TEXTAREA LINE BREAKS | |
var r_text = text.replace(/(\s*)<\?(.*?)\?>(\s*)<\/textarea>/gi, "<?$3?>"); | |
//FIX PHP TAG BREAKS | |
r_text = r_text.replace(/<\?(.*?)\?>/gi, "<?$1?>"); | |
//FIX <> errors inside PHP tags | |
r_text = r_text.replace(/<\?(.*?)(>)(.*?)\?>/gi, "<?$1>$3?>"); | |
r_text = r_text.replace(/<\?(.*?)(<)(.*?)\?>/gi, "<?$1<$3?>"); | |
//Convert to TABS | |
r_text = r_text.replace(/ /gi, " "); | |
// Replace the text in the editor. | |
if (text != r_text) { | |
komodo.editor.replaceSel(r_text); | |
} | |
} catch (e) { | |
alert(e); | |
} finally { | |
ke.endUndoAction(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment