快速将 djot 转换为 html+mathml | ~yosh

作者在其网站使用djot而非 markdown,被其速度和一致语法的承诺吸引。比较了 djot 的jotdown和 markdown 的lowdown在文件大小方面的缩放情况:

标题数量lowdownjotdown
10000.631s0.023s
20004.584s0.085s
300015.403s0.188s

可见 lowdown 似乎呈多项式缩放,jotdown 呈线性缩放。作者觉得 djot 语法规则少且歧义少,写嵌套列表时新建段落较 awkward 是唯一保留意见。djot 有明确数学语法,markdown 解析器需处理尴尬的双美元符号语法,作者曾在几年前写物理笔记时遇过 markdown 数学解析爆炸。
jotdown 解析数学块的示例:

$ cat math.djot
## check out this matrix

$$`
\begin{bmatrix}
    1 & 0 \\
    0 & 1
\end{bmatrix}
`

cool, right?

$ jotdown math.djot
<section id="check-out-this-matrix">
<h2>check out this matrix</h2>
<p><span class="math display">\[
\begin{bmatrix}
1 & 0 \\
0 & 1
\end{bmatrix}
\]</span></p>
<p>cool, right?</p>
</section>

但 jotdown 只是解析并输出数学内容,未用适当 LaTeX 显示/内联数学定界符处理。在写关于剪切的博客文章时,将所有数学解析外包给LaTeXML,将数学内联到 djot 中,这有两个问题:1. 编辑方程需从 LaTeXML 注释中重构 LaTeX,耗费时间;2. 文件导航困难。
直到两天前才知道pandoc可处理此问题:

$ pandoc -f djot --mathml math.djot
<h2 id="check-out-this-matrix">check out this matrix</h2>
<p><math display="block" xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mo stretchy="true" form="prefix">[</mo><mtable><mtr><mtd columnalign="center" style="text-align: center"><mn>1</mn></mtd><mtd columnalign="center" style="text-align: center"><mn>0</mn></mtd></mtr><mtr><mtd columnalign="center" style="text-align: center"><mn>0</mn></mtd><mtd columnalign="center" style="text-align: center"><mn>1</mn></mtd></mtr></mtable><mo stretchy="true" form="postfix">]</mo></mrow><annotation encoding="application/x-tex">
\begin{bmatrix}
1 & 0 \\
0 & 1
\end{bmatrix}
</annotation></semantics></math></p>
<p>cool, right?</p>

但用 pandoc 构建网站时构建时间会增加 7 倍,目前使用 jotdown 构建约需 0.17 秒,用 pandoc 则需 1.3 秒。所以作者快速拼凑了一个小 rust 程序,将 jotdown 库与从 LaTeX 渲染数学的 math-core crate结合,虽不完美但能满足需求,在其网站上可用返回首页

阅读 11
0 条评论