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

后端 未结 7 1660
抹茶落季
抹茶落季 2020-12-13 13:37

How can I use color with ReStructured Text? For example, **hello** translates into hello. How can I make ReStructure(r

7条回答
  •  臣服心动
    2020-12-13 14:18

    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.

提交回复
热议问题