How to control short/long tag names with docutils and pygments?

217 views Asked by At

I have two python codes, one using docutils and the other uses pygments. Both codes process java source files in order to highlight the source code. pygments reads directly java source files and docutils reads RST files that contains code-block directives that contains java snippets.

The problem is that pygments uses short tag names whereas docutils uses long tag names. I cannot figure how to tell to one of them to use short/long them in order to have both tools using the same syntax.

I built the following minimal example:

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

That gives the following output:

<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>

And it shows that for "if", pygments uses the tag "class='k'" and docutils uses the tag "class='keyword'".

How to change one of them to get the same tag names ?

1

There are 1 answers

1
Toyotaka Torii On

Please try add 'syntax_highlight' option to extra_settings.

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