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

后端 未结 7 1664
抹茶落季
抹茶落季 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:15

    RST file can be render by docutils or Sphinx(In fact, Sphinx use docutils too.)

    Sphinx

    If you need complete documentation, then choose Sphinx. You only need to set your styles once, and then you can use it for all places. that is concerned about config.py, your_css, your_role

    docutils

    If you just want to generate a simple HTML file, I think it is more convenient to embed CSS in RST below is an example,

    MyRST2html.py

    It's pretty similar to the rst2html.py, I just want to write the script by myself, and it's convenient to hack the source (and learning more from it.), and then you can customize it to your cool style

    # MyRST2html.py
    import docutils.core
    from pathlib import Path
    
    source_path = Path('demo.rst')
    destination_path = Path('output.html')
    
    if not 'save all data':
        docutils.core.publish_file(source_path=source_path, destination=destination_path, writer_name='html')
    elif 'save the body data only':
        with open(source_path, 'r', encoding='utf-8') as f:
            html_bytes: bytes = docutils.core.publish_string(f.read(), source_path, writer_name='html')
        html = html_bytes.decode('utf-8')
        html_data = html[html.find(''):html.find('')]
        with open(destination_path, 'w', encoding='utf-8') as f:
            f.write(html_data)
            f.write('')
    

    demo.rst

    .. raw:: html
    
        
    
    .. role:: red
    .. role:: b
    
    ==========
    Example
    ==========
    
    .. contents::
    
    Color
    ==========
    
    :red:`R`\G\ :b:`B`
    
    click me |RGB Colors|_
    
    .. |RGB Colors| replace:: :red:`R`\G\ :b:`B`
    .. _`RGB Colors`: https://www.w3schools.com/colors/colors_rgb.asp
    
    

    output.html

    
    

    Example

    Contents

    Color

    RGB

    click me RGB

    note

    If your IDE is PyCharm, it's Ok to see the result on the Editor and Preview

提交回复
热议问题