diff options
-rw-r--r-- | yarnlib/mdparser.py | 56 |
1 files changed, 55 insertions, 1 deletions
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 <pre> 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 + |