Skip to content

Commit

Permalink
Add support for reStructuredText using docutils
Browse files Browse the repository at this point in the history
  • Loading branch information
polybuildr committed Jun 26, 2015
1 parent 2de3aca commit 242845f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Depending on your setup, this could be a different file, such as `~/.profile` or
```
At any point if you want to replace your skin or change the configuration, run `petroglyph init` again.

3. Write a new post in the `/posts` directory with a `.md` extension. Each post's filename will be used as the post's slug when the blog is generated. Markdown support is provided using [Mistune](/~https://github.com/lepture/mistune). Include post metadata by writing posts as follows:
3. Write a new post in the `/posts` directory with a `.md` extension. (reStructuredText is also supported, give the file an `.rst` extension.) Each post's filename will be used as the post's slug when the blog is generated. Markdown support is provided using [Mistune](/~https://github.com/lepture/mistune). Include post metadata by writing posts as follows:

```
---
Expand Down
14 changes: 11 additions & 3 deletions petroglyph/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@

class Post:
def __init__(self, file):
if file[-4:] == '.rst':
self.text_type = 'rst'
else:
self.text_type = 'md'
f = open(file, 'r')
text = f.read()
import yaml
import re
match = re.search(r'^\s*---(.*)---\s*(.*)$', text, re.DOTALL | re.MULTILINE)
match = re.search(r'^\s*---(.*?)---\s*(.*)$', text, re.DOTALL | re.MULTILINE)
if match is None:
raise ValueError("YAML front-matter missing from '%s'." % file)
self.front_matter = match.group(1)
Expand Down Expand Up @@ -45,8 +49,12 @@ def __repr__(self):
return str(self)

def get_html(self):
import mistune
return mistune.markdown(self.text)
if self.text_type == 'rst':
from docutils.core import publish_parts
return publish_parts(self.text, writer_name='html')['html_body']
else:
import mistune
return mistune.markdown(self.text)

def get_preview(self):
import re
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
license='MIT',
keywords='static blog',
packages=['petroglyph'],
install_requires=['mistune', 'pyyaml'],
install_requires=['mistune', 'pyyaml', 'docutils'],
include_package_data=True,
scripts=['petroglyph/petroglyph']
)

0 comments on commit 242845f

Please sign in to comment.