开发者

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

开发者 https://www.devze.com 2023-02-03 16:29 出处:网络
How can I use color with ReStructured Text? For example, **hello** translates into &l开发者_JAVA百科t;strong>hello</strong>. How can I make ReStructure(rst2html.py) translate somethinginto &l

How can I use color with ReStructured Text? For example, **hello** translates into &l开发者_JAVA百科t;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.


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;
}


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>


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.


Works for me like this:

.. raw:: html

    <style> .red {color:#aa0060; font-weight:bold; font-size:16px} </style>

.. role:: red

:red:`test - this text should be red``


Sphinx already supports colors with the s5defs.txt standard definition file intended for inclusion (but is missing the CSS file):

  1. Create/append this text to the value of rst_epilog sphinx configuration, in your docs/conf.py file:

    rst_prolog = """
    .. include:: <s5defs.txt>
    .. default-role::
    
    """
    
  2. Follow Sphinx's instructions to add a css with the colors (e.g. adopt the hack.css from @Næreen's answer):

    • Place your css file into e.g. _static/css/s4defs-roles.css;
    • append it's path into shtml_css_files sphinx configuration:

      html_css_files = [
          'css/s4defs-roles.css',
      ]
      

You may then use:

Some :red:`colored text` at last!

TIP: Read this SO if you also want the styling to appear in Latex output.


Combining @prosseek's and @RayLuo's answers all in one place - to make easier to find

At the top of your RST file, place

.. raw:: html

    <style> .red {color:red} </style>

.. role:: red

:red:`test - this text should be red`

SIDE COMMENT:

Of course, many folks will want the style in a separate file, as @sienkiew says.

But not always.

E.g. I am generating the above from a script that I want other users to be able to run, often from a file URL. Depending on rst2html.py is bad enough - requiring something nonstandard to be in a config file is worse.

If there were a way to create a weak local definition for the style - e.g. "if there is no style .red already defined use this, but otherwise use the style already defined" - would be nice. But AFAIK local definitions are stronger.

This ran with rst2html.py (Docutils 0.13.1 [release], Python 3.6.4, on cygwin), but other RST tools rejected.


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('<body>'):html.find('</body>')]
    with open(destination_path, 'w', encoding='utf-8') as f:
        f.write(html_data)
        f.write('</body>')

demo.rst

.. raw:: html

    <style>
        .red {color:red; font-weight:bold;}
        .b {color:#0000FF; background-color:white;}
    </style>

.. 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

<body>
<div class="document" id="example">
<h1 class="title">Example</h1>

<style>
    .red {color:red; font-weight:bold;}
    .b {color:#0000FF; background-color:white;}
</style><div class="contents topic" id="contents">
<p class="topic-title">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#color" id="id1">Color</a></li>
</ul>
</div>
<div class="section" id="color">
<h1><a class="toc-backref" href="#id1">Color</a></h1>
<p><span class="red">R</span>G<span class="b">B</span></p>
<p>click me <a class="reference external" href="https://www.w3schools.com/colors/colors_rgb.asp"><span class="red">R</span>G<span class="b">B</span></a></p>
</div>
</div>
</body>

note

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消