我有两个python代码,一个使用docutils,另一个使用pygments。两个代码都处理java源文件以突出显示源代码。 pygments直接读取java源文件,docutils读取包含包含java片段的代码块指令的RST文件。
问题是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">"test"</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">"test"</span><span class="operator">)</span> <span class="operator">;</span>
</pre>
</div>
它表明,对于&#34;如果&#34;,pygments使用标签&#34; class =&#39; k&#39;&#34;和docutils使用标签&#34; class =&#39;关键字&#39;&#34;。
如何更改其中一个以获得相同的标记名称?
答案 0 :(得分:2)
请尝试添加&#39; syntax_highlight&#39; extra_settings的选项。
extra_settings = {
'initial_header_level': 4,
'doctitle_xform' : 0,
'syntax_highlight': 'short' # Possible values: 'long', 'short', 'none'
}