Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

When I run rst2html.py against my ReStructured Text source, with its code-block directive, it adds all the spans and classes to the bits of code in the HTML, but the CSS to actually colorize those spans is absent. Is it possible to get RST to add a CSS link or embed the CSS in the HTML file?

like image 604
Mark Rendle Avatar asked Mar 21 '12 15:03

Mark Rendle


People also ask

Is world on the same line as the syntax highlighted section?

Even though “World!” should be on a new line, the syntax highlighted section displays it on the same line. The update function needs to be edited. Instead of using innerText, we can use innerHTML, replacing the open bracket character ( <) with < and replace the ampersand character ( &) with & to stop HTML escapes from being evaluated.

Is it possible to edit the syntax-highlighted code in a textarea?

Now it’s editable and highlighted! Now, when the textarea is edited — as in, a pressed key on the keyboard comes back up — the syntax-highlighted code changes. There are a few bugs we’ll get to, but I want to focus first on making it look like you are directly editing the syntax-highlighted element, rather than a separate textarea.

Is there a syntax highlighter that will work with WYSIWYG?

It would even work with a syntax highlighter you create yourself, if you want it to. I hope this becomes useful, and can be used in many places, whether it’s a WYSIWYG editor for a CMS, or even a forms where the ability to enter source code is a requirement like a front-end job application or perhaps a quiz.

Should you use CSS modules or static stylesheet?

The CSS Modules method, again, has some great use cases. For applications looking to take advantage of scoped styles while maintaining the performance benefits of a static, but compiled stylesheet, then CSS Modules may be the right fit for you!


2 Answers

As of Docutils 0.9 you could use the code directive. From the example on this page:

.. code:: python

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

Alternatively, you can use Pygments for syntax highlighting. See Using Pygments in ReST documents and this SO answer.

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

Update As discussed in the comments, to get the style file used by Pygments use the command

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

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

like image 157
Chris Avatar answered Sep 21 '22 13:09

Chris


In docutils 0.9 and 0.10 it doesn't mattter whether you use code, code-block or sourcecode. All directives are considered code role.

This command will generate css that can embedded into html by rst2html.py.

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

This command will generate the html:

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

By default, rst2html.py outputs spans with class names like comment, number, integer, and operator. If you have a docutils.conf either 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 matches syntax.css generated by pygmentize.

See syntax-highlight in docutils documentation

like image 30
Chad Skeeters Avatar answered Sep 21 '22 13:09

Chad Skeeters