Explorar el Código

Merge pull request #265 from wilsonfreitas/master

Add new plugin rmd_reader
Justin Mayer hace 10 años
padre
commit
3938a8b7c6
Se han modificado 3 ficheros con 71 adiciones y 0 borrados
  1. 21 0
      rmd_reader/Readme.md
  2. 1 0
      rmd_reader/__init__.py
  3. 49 0
      rmd_reader/rmd_reader.py

+ 21 - 0
rmd_reader/Readme.md

@@ -0,0 +1,21 @@
+# RMD Reader
+
+This plugin helps you creating posts with knitr's RMarkdown files.
+[knitr](http://yihui.name/knitr/) is a template engine which executes and displays embedded R code.
+So, being short you can write an executable paper with codes, formulas and graphics.
+
+## Dependency
+
+This plugin needs [rpy2](https://pypi.python.org/pypi/rpy2) to work.
+Install it with:
+
+```
+pip install rpy2
+```
+
+## Usage
+
+The plugin detects RMD files ending with `.Rmd` or `.rmd` so you only have to write a RMarkdown files inside content directory.
+
+This plugin calls R to process these files and generates markdown files that are processed by Python Markdown (with `meta`, `codehilite(css_class=highlight)`, and `extra` extensions) before returning the content and metadata to pelican engine.
+

+ 1 - 0
rmd_reader/__init__.py

@@ -0,0 +1 @@
+from .rmd_reader import *

+ 49 - 0
rmd_reader/rmd_reader.py

@@ -0,0 +1,49 @@
+#-*- conding: utf-8 -*-
+
+import os
+
+from pelican import readers
+from pelican import signals
+from pelican import settings
+from pelican.utils import pelican_open
+from markdown import Markdown
+try:
+    from rpy2 import robjects
+    rmd = True
+except ImportError:
+    rmd = False
+
+class RmdReader(readers.BaseReader):
+    enabled = rmd
+
+    file_extensions = ['Rmd', 'rmd']
+
+    # You need to have a read method, which takes a filename and returns
+    # some content and the associated metadata.
+    def read(self, filename):
+        """Parse content and metadata of markdown files"""
+        # parse Rmd file - generate md file
+        md_filename = filename.replace('.Rmd', '.aux').replace('.rmd', '.aux')
+        robjects.r("""
+require(knitr);
+opts_knit$set(base.dir='{2}/content');
+knit('{0}', '{1}', quiet=TRUE, encoding='UTF-8');
+""".format(filename, md_filename, settings.DEFAULT_CONFIG.get('PATH')))
+        # parse md file
+        md = Markdown(extensions = ['meta', 'codehilite(css_class=highlight)', 'extra'])
+        with pelican_open(md_filename) as text:
+            content = md.convert(text)
+        os.remove(md_filename)
+        # find metadata
+        metadata = {}
+        for name, value in md.Meta.items():
+            name = name.lower()
+            meta = self.process_metadata(name, value[0])
+            metadata[name] = meta
+        return content, metadata
+
+def add_reader(readers):
+    readers.reader_classes['rmd'] = RmdReader
+
+def register():
+    signals.readers_init.connect(add_reader)