Skip to content

Instantly share code, notes, and snippets.

@pydanny
Last active January 26, 2018 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pydanny/6f06a82fe7a2ff96db396490fc1b3f31 to your computer and use it in GitHub Desktop.
Save pydanny/6f06a82fe7a2ff96db396490fc1b3f31 to your computer and use it in GitHub Desktop.
How I convert rst to HTML. Currently fails on images.
This image fails when I run it via the function below:
.. image:: https://raw.githubusercontent.com/pydanny/pydanny.github.com/master/static/bitcointsd111.png
:align: center
:alt: BitCoin and Two Scoops of Django!
:target: https://www.pydanny.com/25-percent-bitcoin-sale-two-scoops-of-django.html
Error provided is::
<h1>System Message: ERROR/3 (&lt;string&gt; line 4)</h1>
<p>Error in "image" directive: no content permitted.</p>
<pre>.. image:: https://raw.githubusercontent.com/pydanny/pydanny.github.com/master/static/bitcointsd111.png
"""
Python version: Python 3.6
packages: rst2html5 docutils
"""
from docutils.core import publish_string
import rst2html5_ # rst2html5 package
def rst2article(content):
# Transform the text into html
post = publish_string(source=content, writer=rst2html5_.HTML5Writer())
# decode the binary output
post = post.decode()
# Strip out the HTML top and bottom
post = post[post.index('<body>')+6:post.index('</body>')]
# clean up the white space
post = '\n'.join([x.strip() for x in post.splitlines()])
return post
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment