Adding Org Support in Pelican Readers

Motivation

Markdown and Rst are cool. But org is cooler. If I had a static blogging platform with integrated support, it’d save me some grief very often. So I’m trying to add support for Org documents to Pelican.

Setup project and install pelican

Clone my fork of the pelican repo.

#!/bin/sh
mkproject -p /usr/bin/python2.7 2015-01-21_pelican-org
git clone git@github.com:jo-tham/pelican.git
sudo pacman -S libxslt # otherwise dev_requirements didn't install
cd pelican
pip install -r dev_requirements.txt
python setup.py develop
pip install markdown
git checkout -b org-support

Tests

Test out pypandoc.

import pypandoc
org_as_html = pypandoc.convert('test.org', 'html')
with open('test.html', 'w') as out_file:
    for line in org_as_html:
        out_file.writelines(line)

That works, but it misses some of the formatting. Table didn’t have borders. Nested headings weren’t nicely represented. Etc.

#!/bin/python
import pypandoc
org_as_html = pypandoc.convert('test.org', 'html', extra_args=['--highlight-style=pygments', '--base-header-level=1', '--standalone'])
with open('test.html', 'w') as out_file:
    for line in org_as_html:
        out_file.writelines(line)

Pandoc template for metadata

Pandoc uses templates for standalone documents. We need one that doesn’t add funny demarcation in order for the result to play nicely with what Pelican expects of markdown metadata.

So far, I went with this.

$if(title)$Title: $title$$endif$
$for(author)$Author: $author$$endfor$
$if(date)$Date: $date$$endif$
$if(category)$Category: $category$$endif$
$for(tags)$Tags: $tags$$endfor$
$if(toc)$ $toc$$endif$
$body$
$for(include-after)$$include-after$$endfor$

With that template,

pandoc -o post.markdown post.org --template=md_org_template.markdown

gives the desired output.

Tables aren’t quite formatted correctly. But code is highlighted. Whereas with Rst, code is not highlighted but tables are correct. Ugh, markup language dialects.

I could also use pandoc for the document body to html. For now, I just want to get something working.

I will simply add org as part of the markdown reader, converting org to markdown with pypandoc. The additions are visible here.

Currently, the test dont pass as pandoc is looking for the template file in the wrong dir. I think an abspath around the pkgresources call may do the trick.

TODOs

  • fix --data-dir option in pypandoc.convert
    • currently the template is required in the pelican site root dir, as pandoc checks the working directory, the template directory, and absolute paths
  • implement as plugin?
    • org is amazin markup language, i like it native…
  • do the org text to html with pandoc instead of markdown
    • would fix tables
  • use Rst/html classes
    • would also fix tables, and rst doesnt require another library. i like this method. But I’ve had trouble getting code blocks highlighted when using rst.
  • dedicated org reader
    • IRC history indicates this should be done as a plugin
  • specify in SETTINGS which reader subclass to use for org files
    • implement org conversion in markdown, rst, and html, and let the user choose which one to use
  • fix tables in markdown
  • get additional metadata working
  • images example
Go Top
comments powered by Disqus