0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

HTML整形マクロ for Sakuraエディター

Posted at

以下のマクロは、Sakuraエディターのマクロ機能を使い、HTMLソースをきれいにインデント付きで整形します。外部ツールを使わず、正規表現と再帰的インデント処理でタグ階層を判定します。

;----------------------------------------
; Sakuraエディター マクロ:HTML整形
; ファイル名例:HtmlFormatter.mac
;----------------------------------------

; マクロエントリポイント
Main:
    ; 全体を対象にするなら SelectAll
    SelectAll
    ; 選択範囲をテキストとして取得
    let html = GetClipboardText()
    ; クリップボードをクリア
    SetClipboardText ""
    ; テキストをクリップボード経由で取得(Sakuraマクロの制限回避)
    Copy
    let html = GetClipboardText()

    ; 整形処理
    let formatted = FormatHTML(html)

    ; クリップボードに整形後をセット
    SetClipboardText formatted
    ; 選択範囲を置換
    Paste
    Return

; HTMLを行単位でインデント付きに整形する関数
Function FormatHTML(html)
    ; 改行コードを統一
    let html = Replace(html, "\r\n", "\n")
    ; タグ毎に改行を挿入
    ; 開始タグ前後/終了タグ前後に改行
    let html = Replace(html, ">(?=<)", ">\n")
    let html = Replace(html, "(?<=</[^>]+>)", "\n")
    ; 不要な空行を削除
    let lines = Split(html, "\n")
    let indentLevel = 0
    let out = ""

    ForEach line in lines
        let s = Trim(line)
        If s == "" Then Continue For

        ; 終了タグならインデントを減らす
        If RegexMatch(s, "^</") Then
            let indentLevel = indentLevel - 1
        EndIf

        ; インデント
        let out = out + StringRepeat("    ", indentLevel) + s + "\n"

        ; 開始タグかつ終了タグでないならインデントを増やす
        If RegexMatch(s, "^<[^/!][^>]*?>") And Not RegexMatch(s, "/>$") Then
            let indentLevel = indentLevel + 1
        EndIf
    Next

    Return out
EndFunction

;----------------------------------------
; 補助マクロ関数
; Sakura標準でないメソッドを定義
;----------------------------------------

; 文字列を繰り返す
Function StringRepeat(str, count)
    let res = ""
    For i = 1 To count
        let res = res + str
    Next
    Return res
EndFunction

; 前後空白を削除
Function Trim(str)
    Return Replace(Replace(str, "^\s+", ""), "\s+$", "")
EndFunction

; 正規表現マッチ
Function RegexMatch(s, pat)
    ; 先頭に^ と末尾に$を自動付与せず部分マッチ
    Let regex = new RegExp(pat, "i")
    Return regex.Test(s)
EndFunction

; 正規表現置換
Function Replace(s, pat, rep)
    Let regex = new RegExp(pat, "gi")
    Return s.replace(regex, rep)
EndFunction

; Split(基本版)
Function Split(s, delim)
    Dim arr
    arr = SplitString(s, delim)
    Return arr
EndFunction

;----------------------------------------
; End of HtmlFormatter.mac
;----------------------------------------

導入手順
上記コードをテキストファイルにコピーし、ファイル名を HtmlFormatter.mac として保存。

Sakuraエディターの設定画面で「マクロ/プラグイン」のタブを開き、「マクロ」を追加。

保存した HtmlFormatter.mac を登録し、ショートカットキーを割り当てる。

HTMLファイルを開き、整形したい範囲を選択またはファイル全体を選択してマクロを実行。

これにより、HTMLの開始タグ・終了タグに応じた階層インデントが適用され、可読性の高いソースコードに整形できます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?