Skip to main content

Generates (X)HTML5 documents from standalone reStructuredText sources

Project description

rst2html5 generates (X)HTML5 documents from standalone reStructuredText sources. It is a complete rewrite of the docutils’ rst2html and uses new HTML5 constructs such as <section> and <aside>.


$ pip install rst2html5


$ rst2html5 [options] SOURCE [DEST]



Don’t indent output

--stylesheet=<URL or path>

Specify a stylesheet URL to be included. (This option can be used multiple times)

--script=<URL or path>

Specify a script URL to be included. (This option can be used multiple times)

--script-defer=<URL or path>

Specify a script URL with a defer attribute to be included in the output HTML file. (This option can be used multiple times)

--script-async=<URL or path>

Specify a script URL with a async attribute to be included in the output HTML file. (This option can be used multiple times)


Specify a html tag attribute. (This option can be used multiple times)

--template=<filename or text>

Specify a filename or text to be used as the HTML5 output template. The template must have the {head} and {body} placeholders. The “<html{html_attr}>” placeholder is recommended.


Define a case insensitive identifier to be used with ifdef and ifndef directives. There is no value associated with an identifier. (This option can be used multiple times)

If DEST is not provided, the output is send to stdout.


Consider a file called example.rst that contains:


Some text and a target to `Title 2`_. **strong emphasis**:

* item 1
* item 2

Title 2

.. parsed-literal::

    Inline markup is supported, e.g. *emphasis*, **strong**, ``literal
    _`hyperlink targets`, and `references <>`_

The command to produce an example.html output file is:

$ rst2html5 example.rst example.html

The HTML5 produced is clean and tidy:

<!DOCTYPE html>
    <meta charset="utf-8" />
    <section id="title">
        <p>Some text and a target to <a href="#title-2">Title 2</a>. <strong>strong emphasis</strong>:</p>
            <li>item 1</li>
            <li>item 2</li>
    <section id="title-2">
        <h1>Title 2</h1>
        <pre>Inline markup is supported, e.g. <em>emphasis</em>, <strong>strong</strong>, <code>literal
<a id="hyperlink-targets">hyperlink targets</a>, and <a href="">references</a></pre>

Stylesheets and Scripts

No stylesheets or scripts are spread over the HTML5 by default. However stylesheets and javascripts URLs or paths can be included through stylesheet and script options:

$ rst2html5 example.rst \
--stylesheet \
--stylesheet-inline css/simple.css \
--script-defer js/test1.js
--script-async js/test2.js
<!DOCTYPE html>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="" />
    <style>h1 {font-size: 20em}
img.icon {
    width: 48px;
    height: 48px;
h2 {color: red}
    <script src=""></script>
    <script src="js/test1.js" defer="defer"></script>
    <script src="js/test2.js" async="async"></script>

HTML tag attributes can be included through html-tag-attr option:

$ rst2html5 --html-tag-attr 'lang="pt-BR"' example.rst
<!DOCTYPE html>
<html lang="pt-BR">


Custom HTML5 template via the --template option. Example:

$ template='<!DOCTYPE html>
<head>{head}    <!-- custom links and scripts -->
    <link href="css/default.css" rel="stylesheet" />
    <link href="css/pygments.css" rel="stylesheet" />
    <script src=""></script>

$ echo 'one line' > example.rst

$ rst2html5 --template "$template" example.rst
<!DOCTYPE html>
    <meta charset="utf-8" />
    <!-- custom links and scripts -->
    <link href="css/default.css" rel="stylesheet" />
    <link href="css/pygments.css" rel="stylesheet" />
    <script src=""></script>
    <p>one line</p>

New Directives

define, undef, ifdef and ifndef

rst2html5 provides some new directives: define, undef, ifdef and ifndef, similar to those used in C++. They allow to conditionally include (or not) some rst snippets:

.. ifdef:: x

    this line will be included if 'x' was previously defined

In case of you check two or more identifiers, there must be an operator ([and | or]) defined:

.. ifdef:: x y z
    :operator: or

    This line will be included only if 'x', 'y' or 'z' is defined.

stylesheet and script

From rst2html5 1.9, you can include stylesheets and scripts via directives inside a reStructuredText text:

Just an ordinary paragraph.

.. stylesheet:: css/default.css
.. stylesheet::

.. script::
.. script:: slide.js

.. script:: test/animations.js

Another paragraph
<!DOCTYPE html>
    <meta charset="utf-8" />
    <link href="css/default.css" rel="stylesheet" />
    <link href="" rel="stylesheet" />
    <script src=""></script>
    <script src="slide.js" defer="defer"></script>
    <script src="test/animations.js" async="async"></script>
    <p>Just an ordinary paragraph.</p>
    <p>Another paragraph</p>


There also is a template directive. The usage is:

.. template:: filename


.. template::

    template content here.

New Roles


From MDN Web Docs:

The HTML Abbreviation element (<abbr>) represents an abbreviation or acronym; the optional title attribute can provide an expansion or description for the abbreviation. If present, title must contain this full description and nothing else.

To create an abbreviation in rst2html5 use the :abbr: role:

* :abbr:`SPA (Single-Page Application)`
* :abbr:`ASGI (Asynchronous Server Gateway Interface)` is a spiritual successor to :abbr:`WSGI`
* :abbr:`WSGI (Web Server Gateway Interface)`

Resulting in:

        <abbr title="Single-Page Application">SPA</abbr>
        <abbr title="Asynchronous Server Gateway Interface">ASGI</abbr>
    is a spiritual successor to
        <abbr title="Web Server Gateway Interface">WSGI</abbr>

Note that if the abbreviation follows the pattern ABBR (Description for the abbreviation), the description is extracted and becomes the title.

How To Use rst2html5 Programmatically

You should use rst2html5.HTML5Writer with one of the publish_*` methods available in ``docutils.core. In the case that the input and output will be in memory, publish_parts is the best fit:

from docutils.core import publish_parts

from rst2html5 import HTML5Writer

text = r'''The area of a circle is :math:`A_\text{c} = (\pi/4) d^2`.

.. math::

    \frac{ \sum_{t=0}^{N}f(t,k) }{N}

body = publish_parts(writer=HTML5Writer(), source=text)['body']

Resulting in:

<p>The area of a circle is
    <span class="math">\(A_\text{c} = (\pi/4) d^2\)</span>
<div class="math">\(\frac{ \sum_{t=0}^{N}f(t,k) }{N}\)</div>

See also: The Docutils Publisher

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rst2html5-2.0.tar.gz (22.7 kB view hashes)

Uploaded source

Built Distribution

rst2html5-2.0-py3-none-any.whl (19.9 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page