function CodeBeautifier() { CodeBeautifier = function(options) { this.program = "phpCB.exe"; this.options = options || [ ]; this.temporaryExtension = ".pbftmp"; this.temporaryFileName = ScriptFullName + this.temporaryExtension; } var self = CodeBeautifier.prototype; self.beautifyFile = function(fileName) { if (!fileName) fileName = document.FullName; var command = [ "cmd /c", this.program, this.options.join(" "), '"' + fileName + '"', '> "' + this.temporaryFileName + '"' ].join(" "); var shell = new ActiveXObject("WScript.Shell"); shell.Run(command, 7, true); return this.temporaryFileName; } self.beautifyDocument = function(doc) { if (!doc) doc = document; if (!doc.Saved) throw "セーブされていません"; var tmp = this.beautifyFile(doc.FullName); doc.selection.SelectAll(); doc.selection.InsertFromFile(tmp, doc.Encoding, 0); var fso = new ActiveXObject("Scripting.FileSystemObject"); fso.DeleteFile(tmp); } } CodeBeautifier(); var beautifier = new CodeBeautifier(); /* 設定ここから */ beautifier.options = [ // "-space-after-start-bracket", // "-space-before-end-bracket", "-space-after-if", "-space-after-switch", "-space-after-while", "-space-before-start-angle-bracket", "-space-after-end-angle-bracket", "-extra-padding-for-case-statement", "-one-true-brace-function-declaration", "-glue-amperscore", "-change-shell-comment-to-double-slashes-comment", // "-indent-with-tab", "-force-large-php-code-tag", "-force-true-false-null-contant-lowercase", "-align-equal-statements", "--equal-align-position 50", "--comment-rendering-style PEAR", "--padding-char-count 4", "-optimize-eol" ]; /* 設定ここまで */ if (!document.Saved) { if (confirm("保存されていません\n上書き保存してもよろしいですか?")) { try { document.Save(document.FullName); } catch(e) { alert("保存できませんでした"); Quit(); } } else { Quit(); } } beautifier.beautifyDocument(); document.selection.StartOfDocument(); alert("整形完了しました");
秀丸用マクロ「秀丸でPHPをインデントして読みやすくするマクロ」http://www.lyricfathom.com/pukiwiki/pukiwiki.php にインスパイヤされました。
実行には http://www.waterproof.fr/products/phpCodeBeautifier/ の phpCodeBeautifier に含まれる phpCB.exe をパスの通った場所(C:\WINDOWSなど)に置いておく必要があります。
Changelog
-
2006-05-05
- WshShell.Execute() ではなく WshShell.Run() を使って、出力をテンポラリファイル経由で読み込むようにした
使用者追記
-
2007-09-19
- phpCodeBeautifier1.0.1でオプションの頭の-は全て--にするようにバグフィックスが行われました。使用者の方はお気をつけください。