Last active
December 30, 2015 12:19
-
-
Save utaal/7828374 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import markdown | |
| import sys | |
| option_defaults = { | |
| 'html_replacement_text' : '[HTML_REMOVED]', | |
| 'tab_length' : 4, | |
| 'enable_attributes' : True, | |
| 'smart_emphasis' : True, | |
| 'lazy_ol' : True, | |
| 'doc_tag' : 'document', | |
| 'safeMode' : False, | |
| } | |
| class Bunch(object): | |
| def __init__(self, d): | |
| self.__dict__ = d | |
| self.inlinePatterns = markdown.inlinepatterns.build_inlinepatterns(self) | |
| self.references = {} | |
| md_object = Bunch(option_defaults) | |
| parser = markdown.blockprocessors.build_block_parser(md_object) | |
| treeprocessors = markdown.treeprocessors.build_treeprocessors(md_object) | |
| md_object.treeprocessors = treeprocessors | |
| lines = sys.stdin.readlines() | |
| #lines = open('test.md', 'r').readlines() | |
| root = parser.parseDocument(lines).getroot() | |
| for treeprocessor in treeprocessors.values(): | |
| newRoot = treeprocessor.run(root) | |
| if newRoot: | |
| root = newRoot | |
| def print_tree(item, depth): | |
| print(" " * depth + "{}: {}--{}".format(item.tag, item.text, item.tail)) | |
| for c in item.getchildren(): | |
| print_tree(c, depth+1) | |
| def render(item): | |
| def render_children(): | |
| a = "" | |
| if item.text: | |
| a += item.text | |
| for c in item.getchildren(): | |
| a += render(c) | |
| if c.tail: | |
| a += c.tail | |
| return a | |
| if item.tag == 'document': | |
| return ('\\documentclass{report}\n\\begin{document}\n' + | |
| render_children() + | |
| '\\end{document}') | |
| elif item.tag == 'p': | |
| return ('\\paragraph{}\n' + render_children() + '\n') | |
| elif item.tag == 'h2': | |
| return ('\\section{' + render_children() + '}\n') | |
| elif item.tag == 'h3' or item.tag == 'h4' or item.tag == 'h5': | |
| return ('\\subsection{' + render_children() + '}\n') | |
| elif item.tag == 'ul': | |
| return ('\\begin{itemize}\n' + | |
| render_children() + | |
| '\\end{itemize}\n') | |
| elif item.tag == 'li': | |
| return ('\\item ' + render_children() + '\n') | |
| elif item.tag == 'a': | |
| return render_children() | |
| else: | |
| return render_children() | |
| print(render(root)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment