SampleKey: SampleValue Metadata1: Metadata1Value
First line of sloka
Second line
and a third line
vlah blah
First line split here
Words of second line
and a *3rd* line
first line's information
and that of the second line
and a third line
| # -*- coding: utf-8 -*- | |
| import CommonMark | |
| if __name__ == "__main__": | |
| with open("trial.md") as f: | |
| parser = CommonMark.DocParser() | |
| renderer = CommonMark.HTMLRenderer() | |
| ast = parser.parse(f.read()) | |
| html = renderer.render(ast) | |
| json = CommonMark.ASTtoJSON(ast) | |
| #CommonMark.dumpAST(ast) # pretty print generated AST structure | |
| print(html) # <p>Hello <em>World</em><p/> | |
| print(json) | |
| #html = markdown.markdown(text=f.read(), extensions=[PrajnaExtension()]) | |
| print("--------------------") | |
| print(html) |
| # -*- coding: utf-8 -*- | |
| import markdown | |
| import re | |
| from markdown.blockprocessors import BlockProcessor | |
| from markdown.preprocessors import Preprocessor | |
| from markdown.treeprocessors import Treeprocessor | |
| from markdown.postprocessors import Postprocessor | |
| class PrajnaExtension(markdown.Extension): | |
| def __init__(self, *args, **kwargs): | |
| print("init") | |
| pass | |
| def extendMarkdown(self, md, md_globals): | |
| print("extendMarkdown") | |
| md.parser.blockprocessors.add('prajna', MyBlockProcessor(md.parser), | |
| ">indent") | |
| md.preprocessors.add('prajna', MyPreprocessor(self), "<reference") | |
| #md.treeprocessors.add('prajna', MyTreeprocessor(self), "_begin") | |
| md.postprocessors.add('prajna', MyPostprocessor(self), ">amp_substitute") | |
| class MyPreprocessor(Preprocessor): | |
| MYREGEX = re.compile(r'.*') | |
| def run(self, lines): | |
| new_lines = [] | |
| for line in lines: | |
| m = self.MYREGEX.match(line) | |
| if m: | |
| print("pre: line: " + line) | |
| new_lines.append(line) | |
| return new_lines | |
| class MyTreeprocessor(Treeprocessor): | |
| def run(self, root): | |
| print("tree: ") | |
| import pdb; pdb.set_trace() | |
| for node in root: | |
| print("\t> child: " + node) | |
| class MyPostprocessor(Postprocessor): | |
| MYREGEX = re.compile(r'.*') | |
| def run(self, text): | |
| print("post: " + text) | |
| return text | |
| class MyBlockProcessor(BlockProcessor): | |
| SLOKA_RE = re.compile(r'^~~~(\w+)(?:\n|\r\n?)((.|\r\n?)*)(~~~)?', re.MULTILINE | re.DOTALL) | |
| SLOKA_RE_END = re.compile(r'~~~$') | |
| block_started = False | |
| valid_block = True | |
| def test(self, parent, block): | |
| if self.SLOKA_RE.search(block) is not None: | |
| self.valid_block = True | |
| self.block_started = True | |
| elif self.SLOKA_RE_END.search(block) is not None: | |
| self.valid_block = True | |
| self.block_started = False | |
| else: | |
| self.valid_block = False | |
| print("block: test: " + str(self.valid_block) + ": " + block) | |
| return self.valid_block | |
| def run(self, parent, blocks): | |
| raw_block = blocks.pop(0) | |
| print("block: parent: " + str(parent.text)) | |
| print("block: raw: " + raw_block) | |
| if self.block_started is True: | |
| from markdown.util import etree | |
| match = self.SLOKA_RE.match(raw_block) | |
| sloka = etree.SubElement(parent, match.group(1)) | |
| sloka.text = match.group(2) | |
| if __name__ == "__main__": | |
| with open("trial.md") as f: | |
| html = markdown.markdown(text=f.read(), extensions=[PrajnaExtension()]) | |
| print("--------------------") | |
| print(html) |