From bae270eb71648936b5272502e61cb9ddd6890bf2 Mon Sep 17 00:00:00 2001 From: Jotham Apaloo Date: Mon, 26 Jan 2015 22:33:28 -0500 Subject: [PATCH 1/4] Add support for org via pypandoc to markdown. --- pelican/pandoc_templates/md_org_template.markdown | 12 ++++++++++++ pelican/readers.py | 15 +++++++++++++++ pelican/settings.py | 1 + 3 files changed, 28 insertions(+) create mode 100644 pelican/pandoc_templates/md_org_template.markdown diff --git a/pelican/pandoc_templates/md_org_template.markdown b/pelican/pandoc_templates/md_org_template.markdown new file mode 100644 index 00000000..b8946f5d --- /dev/null +++ b/pelican/pandoc_templates/md_org_template.markdown @@ -0,0 +1,12 @@ +$if(title)$Title: $title$$endif$ +$for(author)$Author: $author$$endfor$ +$if(date)$Date: $date$$endif$ +$if(category)$Category: $category$$endif$ +$if(modified)$Modified: $modified$$endif$ +$if(slug)$Slug: $slug$$endif$ +$if(summary)$Summary: $summary$$endif$ +$if(modified)$Modified: $modified$$endif$ +$for(tags)$Tags: $tags$$endfor$ +$if(toc)$ $toc$$endif$ +$body$ +$for(include-after)$$include-after$$endfor$ diff --git a/pelican/readers.py b/pelican/readers.py index 85147e3e..8a019164 100644 --- a/pelican/readers.py +++ b/pelican/readers.py @@ -4,6 +4,7 @@ from __future__ import unicode_literals, print_function import logging import os import re +import pkg_resources import docutils import docutils.core @@ -21,6 +22,10 @@ try: from html import escape except ImportError: from cgi import escape +try: + import pypandoc +except ImportError: + pandoc = False # NOQA from six.moves.html_parser import HTMLParser from pelican import signals @@ -195,6 +200,10 @@ class MarkdownReader(BaseReader): enabled = bool(Markdown) file_extensions = ['md', 'markdown', 'mkd', 'mdown'] + enable_pypandoc = bool(pypandoc) + if enable_pypandoc is True: + file_extensions.append('org') + logger.info('Using pandoc to convert org to markdown') def __init__(self, *args, **kwargs): super(MarkdownReader, self).__init__(*args, **kwargs) @@ -235,6 +244,12 @@ class MarkdownReader(BaseReader): self._source_path = source_path self._md = Markdown(extensions=self.extensions) with pelican_open(source_path) as text: + if self._source_path[-4:] == '.org' and \ + self.enable_pypandoc is True: + pandoc_data_dir = '='.join(['--data-dir', pkg_resources.resource_filename('pelican', 'pandoc_templates')]) + text = pypandoc.convert(text, 'markdown', \ + format='org',\ + extra_args=['--template=md_org_template.markdown', pandoc_data_dir]) content = self._md.convert(text) metadata = self._parse_metadata(self._md.Meta) diff --git a/pelican/settings.py b/pelican/settings.py index 794733d7..2dfb438f 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -135,6 +135,7 @@ DEFAULT_CONFIG = { 'LOAD_CONTENT_CACHE': True, 'AUTORELOAD_IGNORE_CACHE': False, 'WRITE_SELECTED': [], + 'PANDOC_TEMPLATES_PATH': ['pandoc_templates'], } PYGMENTS_RST_OPTIONS = None From 4f466bffebd4b0c77072bc1f9687268c84facd13 Mon Sep 17 00:00:00 2001 From: Jotham Apaloo Date: Mon, 26 Jan 2015 22:55:52 -0500 Subject: [PATCH 2/4] Make working link to pandoc templates. --- MANIFEST.in | 2 +- pelican/settings.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/MANIFEST.in b/MANIFEST.in index dcf9ea45..4ac02c9f 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,3 +1,3 @@ include *.rst -recursive-include pelican *.html *.css *png *.in *.rst *.md *.mkd *.xml *.py +recursive-include pelican *.html *.css *png *.in *.rst *.md *.mkd *.xml *.py *.markdown include LICENSE THANKS docs/changelog.rst diff --git a/pelican/settings.py b/pelican/settings.py index 2dfb438f..794733d7 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -135,7 +135,6 @@ DEFAULT_CONFIG = { 'LOAD_CONTENT_CACHE': True, 'AUTORELOAD_IGNORE_CACHE': False, 'WRITE_SELECTED': [], - 'PANDOC_TEMPLATES_PATH': ['pandoc_templates'], } PYGMENTS_RST_OPTIONS = None From afe2393d2fc885dd2aa593a5b3966b73f17eaf0e Mon Sep 17 00:00:00 2001 From: Jotham Apaloo Date: Mon, 26 Jan 2015 23:01:16 -0500 Subject: [PATCH 3/4] Added org file to tests. Pandoc looking in wrong place for template. pandoc: Could not find data file templates/md_org_template.markdown --- .../content/article_with_org_extension.org | 143 ++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 pelican/tests/content/article_with_org_extension.org diff --git a/pelican/tests/content/article_with_org_extension.org b/pelican/tests/content/article_with_org_extension.org new file mode 100644 index 00000000..1bb5bfaf --- /dev/null +++ b/pelican/tests/content/article_with_org_extension.org @@ -0,0 +1,143 @@ +#+Title: test org in md reader +#+Date: 2014-01-26 +#+Author: Joth +#+Category: pelican +#+Tags: pelican readers, pypandoc +#+Slug: test-org-md-reader +#+Summary: Testing org reader +#+OPTIONS: H:2 +#+OPTIONS: toc:2 +#+TOC: headlines 2 + +* Org Metadata + +The following metadata are exporting properly + +#+begin_src +#+Title: test org in md reader +#+Date: 2014-01-26 +#+Author: Joth +#+Category: pelican +#+end_src + +These metadata are not exporting properly. It may be in the +pandoc template. It may be in the pandoc convert call. + +#+begin_src +#+Tags: pelican readers, pypandoc +#+Slug: test-org-md-reader +#+Summary: Testing org reader +#+OPTIONS: toc:2 +#+TOC: headlines 2 +#+end_src + + +* Markup + +Quotes +#+BEGIN_QUOTE +Everything should be made as simple as possible, +but not any simpler -- Albert Einstein +#+END_QUOTE + +are included with +#+begin_src +#+BEGIN_QUOTE +... +#+END_QUOTE +#+end_src + +Centred text and verse are not working in markdown export. + +#+begin_src +#+BEGIN_CENTER +... +#+END_CENTER +#+end_src + + +* Fonts + +*bold* =*bold*= + +/italic/ =/italic/= + +_underlined_ =_underlined_= + +=verbatim= ~=verbatim=~ + + ~code~ =~code~= + ++strike-through+ =+strike-through+= + + +* Code + +#+begin_src python :noeval +class BaseReader(object): + """Base class to read files. + + This class is used to process static files, and it can be inherited for + other types of file. A Reader class must have the following attributes: + + - enabled: (boolean) tell if the Reader class is enabled. It + generally depends on the import of some dependency. + - file_extensions: a list of file extensions that the Reader will process. + - extensions: a list of extensions to use in the reader (typical use is + Markdown). + + """ + enabled = True + file_extensions = ['static'] + extensions = None + + def __init__(self, settings): + self.settings = settings + + def process_metadata(self, name, value): + if name in METADATA_PROCESSORS: + return METADATA_PROCESSORS[name](value, self.settings) + return value + + def read(self, source_path): + "No-op parser" + content = None + metadata = {} + return content, metadata +#+end_src + + +* Tables + +Not working nicely. They work with pandoc to rst. + +| header 1 | header 2 | +|----------+----------| +| and | with | +| a | two | +| table | columns | + + +* Misc + +Five or more dashes make a line + +------ + + # lines with whitespace infront of '# ' will be treated as comments + +#+begin_src sh + # lines with whitespace infront of '# ' will be treated as comments +#+end_src + + +* Alternatives and TODOs + - implement as plugin? + - org is amazin markup language, i like it native... + - do the org text to html with pandoc instead of markdown + - use Rst/html classes + - dedicated org class + - specify in SETTINGS which reader subclass to use for org files + - fix tables + - images example + - tests From 1852b4ed8602a59e80df7e55f688a1a6db090823 Mon Sep 17 00:00:00 2001 From: Jotham Apaloo Date: Sun, 22 Mar 2015 22:11:10 -0400 Subject: [PATCH 4/4] Fix pypandoc import exception and pandoc templates --- pelican/pandoc_templates/md_org_template.markdown | 5 ++--- pelican/readers.py | 13 +++++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pelican/pandoc_templates/md_org_template.markdown b/pelican/pandoc_templates/md_org_template.markdown index b8946f5d..f5a607f6 100644 --- a/pelican/pandoc_templates/md_org_template.markdown +++ b/pelican/pandoc_templates/md_org_template.markdown @@ -2,11 +2,10 @@ $if(title)$Title: $title$$endif$ $for(author)$Author: $author$$endfor$ $if(date)$Date: $date$$endif$ $if(category)$Category: $category$$endif$ -$if(modified)$Modified: $modified$$endif$ -$if(slug)$Slug: $slug$$endif$ $if(summary)$Summary: $summary$$endif$ -$if(modified)$Modified: $modified$$endif$ +$if(slug)$Slug: $slug$$endif$ $for(tags)$Tags: $tags$$endfor$ +$if(modified)$Modified: $modified$$endif$ $if(toc)$ $toc$$endif$ $body$ $for(include-after)$$include-after$$endfor$ diff --git a/pelican/readers.py b/pelican/readers.py index 8a019164..0c89f1f9 100644 --- a/pelican/readers.py +++ b/pelican/readers.py @@ -25,7 +25,7 @@ except ImportError: try: import pypandoc except ImportError: - pandoc = False # NOQA + pypandoc = False # NOQA from six.moves.html_parser import HTMLParser from pelican import signals @@ -91,7 +91,7 @@ class _FieldBodyTranslator(HTMLTranslator): def depart_field_body(self, node): pass - + def render_node_to_html(document, node): visitor = _FieldBodyTranslator(document) @@ -246,10 +246,15 @@ class MarkdownReader(BaseReader): with pelican_open(source_path) as text: if self._source_path[-4:] == '.org' and \ self.enable_pypandoc is True: - pandoc_data_dir = '='.join(['--data-dir', pkg_resources.resource_filename('pelican', 'pandoc_templates')]) + pandoc_data_dir = '='.join(['--data-dir', + pkg_resources.resource_filename( + 'pelican', 'pandoc_templates')]) + logger.info('Using %s as pandoc_data_dir', + pandoc_data_dir) text = pypandoc.convert(text, 'markdown', \ format='org',\ - extra_args=['--template=md_org_template.markdown', pandoc_data_dir]) + extra_args=['--template=md_org_template.markdown', + pandoc_data_dir]) content = self._md.convert(text) metadata = self._parse_metadata(self._md.Meta)