Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use color in text with ReStructured Text (rst2html.py) or how to insert HTML tags without blank lines?

How can I use color with ReStructured Text? For example, **hello** translates into <strong>hello</strong>. How can I make ReStructure(rst2html.py) translate something into <font color="####">text</font>?

I thought about ..raw:: html, but it introduces blank lines. I want to insert HTML tags without blank lines.

like image 504
prosseek Avatar asked Jan 12 '11 14:01

prosseek


3 Answers

I found this method working

First, you have the role.

.. role:: red

An example of using :red:`interpreted text`

It translates into as follows.

<p>An example of using <span class="red">interpreted text</span></p>

Now, you have the red class, you can use CSS for changing colors.

.red {
    color:red;
}
like image 195
prosseek Avatar answered Nov 14 '22 09:11

prosseek


Well, I am a new user now, therefore I can not comment on others answer, thanks to stackoverflow's policy here. https://meta.stackexchange.com/questions/51926/new-users-cant-ask-for-clarifications-except-as-answers

Sienkiew's answer is good, but I want to make correction about its last sentence.

There IS way to specify the style sheet in the RST file. The clue is in Prosseek's original post, that is the .. raw:: directive.

We can put following lines at the beginning of our RST file to specify its style.

.. raw:: html

    <style> .red {color:red} </style>
like image 30
RayLuo Avatar answered Nov 14 '22 08:11

RayLuo


The other answer here hints at what I wanted to do, but it assumes some detailed knowledge about stylesheets in docutils. Here is a a cookbook explanation:

In your RST file, declare the role once, then use it:

    .. role:: red

    This text is :red:`colored red` and so is :red:`this`

Then you need a style sheet file. First, use Python to copy the default style sheet out of the docutils package:

    python
    import os.path
    import shutil
    import docutils.writers.html4css1 as h
    shutil.copy(os.path.dirname(h.__file__)+"/html4css1.css","my.css")

Then edit my.css to add your customizations at the end:

    .red {
            color: red;
    }

Create a docutils configuration file named "docutils.conf":

    [html4css1 writer]
    stylesheet-path: my.css
    embed-stylesheet: yes

use rst2html.py to convert your document:

    rst2html.py my_document.rst > my_document.html

If you don't want to use docutils.conf, you can specify the style sheet every time you run rst2html:

    rst2html.py --stylesheet my.css my_document.rst > my_document.html

AFAIK, there is no way to specify the style sheet in the RST file.

like image 10
sienkiew Avatar answered Nov 14 '22 09:11

sienkiew