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>
.
Installation
$ pip install rst2html5
Usage
$ rst2html5 [options] SOURCE [DEST]
Options:
- --no-indent
Don’t indent output
- --stylesheet=<URL or path>
Specify a stylesheet URL to be included. (This option can be used multiple times)
- --stylesheet-inline=<path>
Specify a stylesheet file whose contents will be included into the output HTML file. (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)
- --html-tag-attr=<attribute>
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=<identifier>
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.
Example
Consider a file called example.rst that contains:
Title
=====
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
text``,
_`hyperlink targets`, and `references <http://www.python.org/>`_
The command to produce an example.html output file is:
$ rst2html5 example.rst example.html
The HTML5 produced is clean and tidy:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<section id="title">
<h1>Title</h1>
<p>Some text and a target to <a href="#title-2">Title 2</a>. <strong>strong emphasis</strong>:</p>
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>
</section>
<section id="title-2">
<h1>Title 2</h1>
<pre>Inline markup is supported, e.g. <em>emphasis</em>, <strong>strong</strong>, <code>literal
text</code>,
<a id="hyperlink-targets">hyperlink targets</a>, and <a href="http://www.python.org/">references</a></pre>
</section>
</body>
</html>
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 https://example.com/css/default.css \ --stylesheet-inline css/simple.css \ --script https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js --script-defer js/test1.js --script-async js/test2.js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://example.com/css/default.css" />
<style>h1 {font-size: 20em}
img.icon {
width: 48px;
height: 48px;
}
h2 {color: red}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="js/test1.js" defer="defer"></script>
<script src="js/test2.js" async="async"></script>
</head>
...
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">
...
Templates
Custom HTML5 template via the --template option. Example:
$ template='<!DOCTYPE html> <html{html_attr}> <head>{head} <!-- custom links and scripts --> <link href="css/default.css" rel="stylesheet" /> <link href="css/pygments.css" rel="stylesheet" /> <script src="http://code.jquery.com/jquery-latest.min.js"></script> </head> <body>{body}</body> </html>' $ echo 'one line' > example.rst $ rst2html5 --template "$template" example.rst
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<!-- custom links and scripts -->
<link href="css/default.css" rel="stylesheet" />
<link href="css/pygments.css" rel="stylesheet" />
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<p>one line</p>
</body>
</html>
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:: https://pronus.io/css/standard.css
.. script:: http://code.jquery.com/jquery-latest.min.js
.. script:: slide.js
:defer:
.. script:: test/animations.js
:async:
Another paragraph
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<link href="css/default.css" rel="stylesheet" />
<link href="https://pronus.io/css/standard.css" rel="stylesheet" />
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="slide.js" defer="defer"></script>
<script src="test/animations.js" async="async"></script>
</head>
<body>
<p>Just an ordinary paragraph.</p>
<p>Another paragraph</p>
</body>
</html>
template
There also is a template
directive. The usage is:
.. template:: filename
or
.. template::
template content here.
New Roles
:abbr:
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:
<ul>
<li>
<abbr title="Single-Page Application">SPA</abbr>
</li>
<li>
<abbr title="Asynchronous Server Gateway Interface">ASGI</abbr>
is a spiritual successor to
<abbr>WSGI</abbr>
</li>
<li>
<abbr title="Web Server Gateway Interface">WSGI</abbr>
</li>
</ul>
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}
'''
override = {
'html_tag_attr': ['lang="pt-BR"'],
'stylesheet': ['https://example.com/css/default.css'],
'script': [('https://blog.pronus.xyz/test.js', 'async')],
}
html = publish_parts(writer=HTML5Writer(), source=text, settings_overrides=override)['whole']
print(html)
Resulting in:
<!DOCTYPE html>
<html lang="pt-BR">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://example.com/css/default.css" />
<script src="https://blog.pronus.xyz/test.js" async="async"></script>
<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
</head>
<body>
<p>The area of a circle is
<span class="math">\(A_\text{c} = (\pi/4) d^2\)</span>
.</p>
<div class="math">\(\frac{ \sum_{t=0}^{N}f(t,k) }{N}\)</div>
</body>
</html>
See also: The Docutils Publisher
Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file rst2html5-2.0.1.tar.gz
.
File metadata
- Download URL: rst2html5-2.0.1.tar.gz
- Upload date:
- Size: 21.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.4 Linux/6.5.0-14-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 309998c85fab028f2fcb01a2ccdc886db0afc4398262e795a02ea9c4d0f32ae9 |
|
MD5 | ec24085384216bbc76c120054ca75049 |
|
BLAKE2b-256 | bb82a85a9664cced2dce7b9d8895f6cace6c2501072a320342d7edcb6d7ce295 |
File details
Details for the file rst2html5-2.0.1-py3-none-any.whl
.
File metadata
- Download URL: rst2html5-2.0.1-py3-none-any.whl
- Upload date:
- Size: 20.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.4 Linux/6.5.0-14-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 755f97255475ffc3f81e87ebe4bc1a182ffd52b753897045098e997e455651cd |
|
MD5 | 4496d1e8baed297224e2bd7747d9d400 |
|
BLAKE2b-256 | 6811af7dbae566c6601595be0ccfad50d397a94b491532d6d2abf0aff5b852b4 |