How can I get rst2html.py to enable CSS for syntax highlighting?

When I run rst2html.py against my source ReStructured Text, with its code block directive, it adds all the gaps and classes to the bits of code in the HTML, but there is no CSS to actually color these spaces. Is it possible to get RST to add a CSS link or paste CSS into an HTML file?

+6
source share
2 answers

With Docutils 0.9 you can use the code directive. In the example on this page:

 .. code:: python def my_function(): "just a test" print 8/2 

Alternatively, you can use Pyigs to highlight syntax. See Using Pyigs in REST Docs and this Answer.

Finally, you can also use the code in this or this blogpost.

Refresh . As discussed in the comments, to get the style file used by Pyigs, use the command

 pygmentize -S default -f html -a .highlight > style.css 

which will generate the Pyigs style.css CSS style file.

+7
source

In docutils 0.9 and 0.10 it is not suitable for using code, code or source code. All directives are considered the role of code .

This command generates css, which can be embedded in html via rst2html.py.

 pygmentize -S default -f html -a .code > syntax.css 

This command will create html:

 rst2html.py --stylesheet=syntax.css in.txt > out.html 

By default, rst2html.py displays gaps with class names such as comment , number , integer and operator . If you have docutils.conf in the same directory as the source, or /etc or in ~/.docutils with

 [parsers] [restructuredtext parser] syntax_highlight=short 

... then the class names will be c , m , mi and o , which correspond to the syntax.css generated by pygmentize .

See syntax highlighting in docutils documentation

+5
source

Source: https://habr.com/ru/post/911312/


All Articles