Browse Source

Add textile_reader plugin

Joey Coleman 10 years ago
parent
commit
57b42a2af7
3 changed files with 90 additions and 0 deletions
  1. 16 0
      textile_reader/Readme.textile
  2. 1 0
      textile_reader/__init__.py
  3. 73 0
      textile_reader/textile_reader.py

+ 16 - 0
textile_reader/Readme.textile

@@ -0,0 +1,16 @@
+h1. Textile Reader Plugin for Pelican
+
+p. This plugin adds support for "Textile markup":https://github.com/netcarver/textile via the "textile egg on pypi":https://pypi.python.org/pypi/textile .
+
+p. Input files are similar in format to Markdown files, in that they start with the post metadata in "Key: value" pairs, one per line. However, the metadata is ended by a line containing only "----", and then all that follows is the body content.  If the separator line is missing then the whole file is assumed to be content.
+
+h2. Example Input File
+
+<pre>Title: An Example Textile-formatted Input for Pelican
+Date: 2013-08-12
+Category: Plugins
+Tags: textile, pelican
+Author: Joey Coleman
+----
+p. Lorem ipsum dolor sit amet...
+</pre>

+ 1 - 0
textile_reader/__init__.py

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

+ 73 - 0
textile_reader/textile_reader.py

@@ -0,0 +1,73 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals, print_function
+
+from pelican import signals
+from pelican.readers import BaseReader
+from pelican.utils import pelican_open
+
+try:
+    from textile import textile
+except ImportError:
+    textile = False
+
+
+class TextileReader(BaseReader):
+    """Reader for Textile files.  Written using the core MarkdownReader as
+a template.  Textile input files must be of the form:
+
+Title: An example
+Date: 2013-08-10
+----
+p. Lorem ipsum dolar sit amet...
+
+Specifically, the header values as with Markdown files, then four
+dashes, then the body.
+
+    """
+
+    enabled = bool(textile)
+    file_extensions = ['textile']
+
+    def __init__(self, *args, **kwargs):
+        super(TextileReader, self).__init__(*args, **kwargs)
+
+    def _parse_metadata(self, meta):
+        """Process the metadata dict, lowercasing the keys and textilizing the
+value of the 'summary' key (if present).  Keys that share the same
+lowercased form will be overridden in some arbitrary order.
+
+        """
+        output = {}
+        for name, value in meta.items():
+            name = name.lower()
+            if name == "summary":
+                value = textile(value)
+            output[name] = self.process_metadata(name, value)
+        return output
+
+    def read(self, source_path):
+        """Parse content and metadata of textile files."""
+
+        with pelican_open(source_path) as text:
+            parts = text.split('----', 1)
+            if len(parts) == 2:
+                headerlines = parts[0].splitlines()
+                headerpairs = map(lambda l: l.split(':', 1), headerlines)
+                headerdict = {pair[0]: pair[1].strip()
+                              for pair in headerpairs
+                              if len(pair) == 2}
+                metadata = self._parse_metadata(headerdict)
+                content = textile(parts[1])
+            else:
+                metadata = {}
+                content = textile(text)
+
+        return content, metadata
+
+
+def add_reader(readers):
+    readers.reader_classes['textile'] = TextileReader
+
+
+def register():
+    signals.readers_init.connect(add_reader)