From 40655b1e8e691c2655ee78f1cb12af28813df542 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sun, 9 Jun 2013 11:35:57 +0100 Subject: Implement block extraction from Markdown --- yarnlib/mdparser.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'yarnlib') diff --git a/yarnlib/mdparser.py b/yarnlib/mdparser.py index 5b7f174..ed2693c 100644 --- a/yarnlib/mdparser.py +++ b/yarnlib/mdparser.py @@ -16,10 +16,64 @@ # =*= License: GPL-3+ =*= +import cliapp +import logging +import markdown +import os +import re +import shutil +import StringIO +import sys +import tempfile +import time +import ttystatus +from markdown.treeprocessors import Treeprocessor + + +# +# Classes for Markdown parsing. See python-markdown documentation +# for details. We want to find all top level code blocks (indented +# four spaces in the Markdown), which we'll parse for story test +# stuff later on. We create a Python markdown extension and use +# "tree processor" to analyse the parsed ElementTree at the right +# moment for top level
 blocks.
+#
+
+# This is a Treeprocessor that iterates over the parsed Markdown,
+# as an ElementTree, and finds all top level code blocks.
+
+class GatherCodeBlocks(Treeprocessor):
+
+    def __init__(self, blocks):
+        self.blocks = blocks
+
+    def run(self, root):
+        for child in root.getchildren():
+            if child.tag == 'pre':
+                code = child.find('code')
+                self.blocks.append(code.text)
+        return root
+
+# This is the Python Markdown extension to call the code block
+# gatherer at the right time. It stores the list of top level
+# code blocks as the blocks attribute.
+
+class ParseStoryTestBlocks(markdown.extensions.Extension):
+
+    def extendMarkdown(self, md, md_globals):
+        self.blocks = []
+        self.gatherer = GatherCodeBlocks(self.blocks)
+        md.treeprocessors.add('gathercode', self.gatherer, '_end')
+
+
 class MarkdownParser(object):
 
     def __init__(self):
         self.blocks = []
 
     def parse_string(self, text):
-        pass
+        ext = ParseStoryTestBlocks()
+        f = StringIO.StringIO()
+        markdown.markdown(text, output=f, extensions=[ext])
+        self.blocks = ext.blocks
+
-- 
cgit v1.2.1