Pygments 命令行不语法突出显示 HTML 输出

问题描述 投票:0回答:2

我正在尝试使用 python 脚本并准备将其用于带有语法突出显示和行编号的纸张打印。我使用了以下命令行指令:

pygmentize -f html -O style=colored,linenos=1 -l python -o <file>.html <file>.py

该指令运行时没有错误,但无论我如何指示样式和词法分析器,输出的 HTML 都不会出现语法突出显示。它的格式正确,具有正确的缩进和行号,但我无法让它与使用 pygments demo 或任何颜色时显示的彩色语法突出显示相匹配。

我也尝试过不同的python别名(例如

py
python3
py3
)和不同的样式,但HTML总是相同的。无论如何,我在 Windows 10 上使用 PowerShell 来运行指令,并使用 Google Chrome 来查看输出 HTML(尽管我也使用 Edge 检查过,没有发现任何差异)。

python syntax-highlighting command-line-tool pygments
2个回答
6
投票

在写上面的评论时,我实际上找到了解决方案。我深入研究了 javascript 代码,找到了实际的 python 调用,以使 python 实现正常工作。请注意,如何有一个 noclasses=True 作为格式化程序选项。

我不知道 noclasses 的作用,但如果将它作为格式化程序选项添加到 CLI,那么输出的颜色会正确:

pygmentize -l python -f html -O style=default -O noclasses=True -o <file>.html <file>.py

0
投票

根据 documentation 您应该将完整选项(即

-O full
)传递给您的命令,以便以颜色样式渲染 HTML 文件。 看到差异。首先,当您不使用完整选项时:

<div class="highlight"><pre><span></span><span class="k">fn</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="nb">Result</span><span class="o">&lt;</span><span class="p">(),</span><span class="w"> </span><span class="nb">Box</span><span class="o">&lt;</span><span class="k">dyn</span><span class="w"> </span><span class="n">Error</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">poll</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Poll</span><span class="p">::</span><span class="n">new</span><span class="p">()</span><span class="o">?</span><span class="p">;</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">events</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Events</span><span class="p">::</span><span class="n">with_capacity</span><span class="p">(</span><span class="mi">1024</span><span class="p">);</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">addr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;127.0.0.1:8080&quot;</span><span class="p">.</span><span class="n">parse</span><span class="p">()</span><span class="o">?</span><span class="p">;</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">server</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">TcpListener</span><span class="p">::</span><span class="n">bind</span><span class="p">(</span><span class="n">addr</span><span class="p">)</span><span class="o">?</span><span class="p">;</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">unique_token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">connections</span><span class="p">:</span><span class="w"> </span><span class="nc">HashMap</span><span class="o">&lt;</span><span class="n">Token</span><span class="p">,</span><span class="w"> </span><span class="n">TcpStream</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">HashMap</span><span class="p">::</span><span class="n">new</span><span class="p">();</span>

然后如果您使用完整选项:

      <style type="text/css">
/*
generated by Pygments <https://pygments.org/>
Copyright 2006-2024 by the Pygments team.
Licensed under the BSD license, see LICENSE for details.
*/
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
body .hll { background-color: #ffffcc }
body { background: #f8f8f8; }
body .c { color: #3D7B7B; font-style: italic } /* Comment */
body .err { border: 1px solid #FF0000 } /* Error */
.
.
.
body .vc { color: #19177C } /* Name.Variable.Class */
body .vg { color: #19177C } /* Name.Variable.Global */
body .vi { color: #19177C } /* Name.Variable.Instance */
body .vm { color: #19177C } /* Name.Variable.Magic */
body .il { color: #666666 } /* Literal.Number.Integer.Long */

  </style>
</head>
<body>
<h2></h2>

<div class="highlight"><pre><span></span><span class="k">fn</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">-&gt;</span><span class="w"> </span><span class="nb">Result</span><span class="o">&lt;</span><span class="p">(),</span><span class="w"> </span><span class="nb">Box</span><span class="o">&lt;</span><span class="k">dyn</span><span class="w"> </span><span class="n">Error</span><span class="o">&gt;&gt;</span><span class="w"> </span><span class="p">{</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">poll</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Poll</span><span class="p">::</span><span class="n">new</span><span class="p">()</span><span class="o">?</span><span class="p">;</span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">events</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Events</span><span class="p">::</span><span class="n">with_capacity</span><span class="p">(</span><span class="mi">1024</span><span class="p">);</span>
.
.
.

有一个命令可以获取pygmentize默认的CSS样式:

pygmentize -S default -f html > style.css
© www.soinside.com 2019 - 2024. All rights reserved.