Last active
January 26, 2018 21:23
-
-
Save pydanny/6f06a82fe7a2ff96db396490fc1b3f31 to your computer and use it in GitHub Desktop.
How I convert rst to HTML. Currently fails on images.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (<string> 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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