aboutsummaryrefslogtreecommitdiffstats
path: root/mdx_meta.py
diff options
context:
space:
mode:
authorYuri Takhteyev <yuri@freewisdom.org>2008-05-30 14:59:08 -0700
committerYuri Takhteyev <yuri@freewisdom.org>2008-05-30 14:59:08 -0700
commit22188b4233a79387b49318acc8375da995224c43 (patch)
tree2ef3efc14b91daa6c23d3edb1c3fd2c5a1d75bcd /mdx_meta.py
parentd0d4e56d0d2aeff4a977113c5ec17c9ec4e90714 (diff)
parent80172e52c04683af35ab4dce1f5c285d73754824 (diff)
downloadmarkdown-22188b4233a79387b49318acc8375da995224c43.tar.gz
markdown-22188b4233a79387b49318acc8375da995224c43.tar.bz2
markdown-22188b4233a79387b49318acc8375da995224c43.zip
Merge git://gitorious.org/python-markdown/mainline
Diffstat (limited to 'mdx_meta.py')
-rw-r--r--mdx_meta.py81
1 files changed, 81 insertions, 0 deletions
diff --git a/mdx_meta.py b/mdx_meta.py
new file mode 100644
index 0000000..30dea8a
--- /dev/null
+++ b/mdx_meta.py
@@ -0,0 +1,81 @@
+#!usr/bin/python
+
+'''
+Meta Data Extension for Python-Markdown
+==========================================
+
+This extension adds Meta Data handling to markdown.
+
+ >>> import markdown
+ >>> text = """Title: A Test Doc.
+ ... Author: Waylan Limberg
+ ... John Doe
+ ... Blank_Data:
+ ...
+ ... The body. This is paragraph one.
+ ... """
+ >>> md = markdown.Markdown(text, ['meta'])
+ >>> md.convert()
+ u'<p>The body. This is paragraph one.\\n</p>'
+ >>> md.Meta
+ {u'blank_data': [u''], u'author': [u'Waylan Limberg', u'John Doe'], u'title': [u'A Test Doc.']}
+
+Make sure text without Meta Data still works (markdown < 1.6b returns a <p>).
+
+ >>> text = ' Some Code - not extra lines of meta data.'
+ >>> md = markdown.Markdown(text, ['meta'])
+ >>> md.convert()
+ u'<pre><code>Some Code - not extra lines of meta data.\\n</code></pre>'
+ >>> md.Meta
+ {}
+
+'''
+
+import markdown, re
+
+# Global Vars
+META_RE = re.compile(r'^[ ]{0,3}(?P<key>[A-Za-z0-9_-]+):\s*(?P<value>.*)')
+META_MORE_RE = re.compile(r'^[ ]{4,}(?P<value>.*)')
+
+class MetaExtension (markdown.Extension) :
+ def __init__(self, configs):
+ pass
+
+ def extendMarkdown(self, md, md_globals) :
+ self.md = md
+
+ # Insert meta preprocessor first
+ META_PREPROCESSOR = MetaPreprocessor()
+ META_PREPROCESSOR.md = md
+ md.preprocessors.insert(0, META_PREPROCESSOR)
+
+class MetaPreprocessor(markdown.Preprocessor) :
+ def run(self, lines) :
+ meta = {}
+ key = None
+ while 1:
+ line = lines.pop(0)
+ if line.strip() == '':
+ break # blank line - done
+ m1 = META_RE.match(line)
+ if m1:
+ key = m1.group('key').lower().strip()
+ meta[key] = [m1.group('value').strip()]
+ else:
+ m2 = META_MORE_RE.match(line)
+ if m2 and key:
+ # Add another line to existing key
+ meta[key].append(m2.group('value').strip())
+ else:
+ lines.insert(0, line)
+ break # no meta data - done
+ self.md.Meta = meta
+ return lines
+
+
+def makeExtension(configs=None) :
+ return MetaExtension(configs=configs)
+
+if __name__ == "__main__":
+ import doctest
+ doctest.testmod()