gpt4 book ai didi

python - 如何使用 docutils 和 pygments 控制短/长标签名称?

转载 作者:太空宇宙 更新时间:2023-11-04 03:31:19 24 4
gpt4 key购买 nike

我有两个 python 代码,一个使用 docutils,另一个使用 pygments。两种代码都处理 java 源文件,以突出显示源代码。 pygments 直接读取 java 源文件,docutils 读取包含代码块指令的 RST 文件,代码块指令包含 java 片段。

问题是 pygments 使用短标签名,而 docutils 使用长标签名。我不知道如何告诉其中一个使用短/长它们,以便让两个工具使用相同的语法。

我构建了以下最小示例:

from pygments import highlight
from pygments.lexers import JavaLexer
from pygments.formatters import HtmlFormatter

# Here we read directly java with the Java Lexer
formatter = HtmlFormatter()
source1 = highlight("if (o.equals(\"test\") ;", JavaLexer(), formatter)
print source1

from docutils.core import publish_parts

# Here we read a RST file containing a code-block directive that includes java
extra_settings = {'initial_header_level': 4, 'doctitle_xform' : 0}
source2 = publish_parts(".. code-block:: java\n\n if (o.equals(\"test\") ;", writer_name='html',settings_overrides=extra_settings)['html_body']
print source2

这给出了以下输出:

<div class="highlight"><pre><span class="k">if</span> <span class="o">(</span><span class="n">o</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="s">&quot;test&quot;</span><span class="o">)</span> <span class="o">;</span>
</pre></div>

<div class="document">
<pre class="code java literal-block">
<span class="keyword">if</span> <span class="operator">(</span><span class="name">o</span><span class="operator">.</span><span class="name attribute">equals</span><span class="operator">(</span><span class="literal string">&quot;test&quot;</span><span class="operator">)</span> <span class="operator">;</span>
</pre>
</div>

它显示对于“if”,pygments 使用标签“class='k'”,docutils 使用标签“class='keyword'”。

如何更改其中之一以获得相同的标签名称?

最佳答案

请尝试将“syntax_highlight”选项添加到 extra_settings。

extra_settings = {
'initial_header_level': 4,
'doctitle_xform' : 0,
'syntax_highlight': 'short' # Possible values: 'long', 'short', 'none'
}

关于python - 如何使用 docutils 和 pygments 控制短/长标签名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31037341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com