Skip to content
Go back

数式の中のcitationはどうする?

  • そういえば、数式ブロックは置換処理を回避していないので問題が起こるかもしれない
    • ただし、@や全角文字は使わないだろうし、[[]]はhtmlのタグに包まれてマッチしなさそうなので、なにか問題が起きてからでも良いかもしれない
  • 数式は<math>の中にあるので、<code>と同じ方法で置換処理を回避できそう
    • <span>で囲われるものもあるが、render-passthrough.htmloutputオプションをmathmlにすれば、<math>のみになる
  • この<math>はMathMLというWeb標準で数式を表示するので、実はKaTeXのCSSはいらなかったのかもしれない
    • Hugoが数式の変換にKaTeXを使っていそうな感じはあるので、きれいに表示するためにはあったほうが良いのかもしれない
    • 対応状況や互換性などは調べてないので、もしかしたら表示が崩れているところがあるかもしれない
  • <math>に対応するついでに、post_content.htmlを以下のように正規表現でやるようにしてみた
    • splitのときに比べて、ループひとつですべてを表現できるようになった
    • 正規表現は((タグの手前)(タグ1|タグ2|...))|(タグなし)のような感じ
      • タグN:(?:<tag(?:.|\n)*?<\/tag>)
      • タグの手前:(?:((?:.|\n)*?)
      • タグなし:((?:.|\n)*)
    • ただし、この正規表現で本当にあっているのかは良く分かっていない
{{- /* 指定のタグの外側だけに置換処理を適用するpartial */ -}}

{{- /* ページ内に存在する対象外タグを列挙する */ -}}
{{- $tags := slice -}}
{{/*  {{- with .Page.Store.Get "hasCode" -}}  */}}
{{- $tags = $tags | append "code" -}}
{{/*  {{- end -}}  */}}
{{- with .Page.Store.Get "hasMath" -}}
    {{- $tags = $tags | append "math" -}}
{{- end -}}


{{- $store := newScratch -}}
{{- if $tags -}}
    {{- /* 対象外タグがページ内に存在すれば、そのタグを避けて置換処理を適用する */ -}}

    {{- /* 対象外タグに囲まれた部分にマッチする正規表現を作る */ -}}
    {{- $pats := slice -}}
    {{- range $tags -}}
        {{- $pats = $pats | append (print `(?:<` . `(?:.|\n)*?<\/` . `>)`) -}}
    {{- end -}}

    {{- /* 「対象外タグに囲まれた部分とその手前部分」または「対象外タグがない部分」に対してマッチする正規表現で文字列を探す */ -}}
    {{- range findRESubmatch (print `(?:((?:.|\n)*?)(` (delimit $pats `|`) `))|((?:.|\n)*)`) .Content -}}
        {{- /* 対象外タグの外側に置換処理を適用する */ -}}
        {{- $content := or (index . 1) (index . 3) -}}
        {{- $content = partial "replace-japanese-novel-ruby.html" (dict "Content" $content "Store" $store) -}}
        {{- $content = partial "replace-citations.html" (dict "Content" $content "Store" $store) -}}
        {{- $content = partial "replace-wikilinks.html" (dict "Content" $content "Store" $store) -}}
        {{- $content | safeHTML  -}}

        {{- /* 対象外タグの内側はそのままにする */ -}}
        {{- with index . 2 -}}
            {{- . | safeHTML  -}}
        {{- end -}}
    {{- end -}}
{{- else -}}
    {{- /* 対象外タグがページ内に存在しないなら、ページ全体に置換処理を適用する */ -}}
    {{- $content := .Content -}}
    {{- $content = partial "replace-japanese-novel-ruby.html" (dict "Content" $content "Store" $store) -}}
    {{- $content = partial "replace-citations.html" (dict "Content" $content "Store" $store) -}}
    {{- $content = partial "replace-wikilinks.html" (dict "Content" $content "Store" $store) -}}
    {{- $content | safeHTML -}}
{{- end -}}

{{- partial "references.html" $store -}}

おまけ#

  • 数式と同じようにrender hookでブロックの有無を記録しようとしたが、数式と異なりインライン要素がhookされなかった
    • 結局、コードは必ずあると想定するしかなかった

参考文献#