Created
February 15, 2018 11:46
-
-
Save MaxwellBo/097ccbc2623932f60a674dd3ca621fc2 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 json | |
| class JSONToMarkdownParser(object): | |
| def parse(self, filename_in, filename_out): | |
| with open(filename_in, 'r') as f_in, open(filename_out, 'w') as f_out: | |
| python_type = json.loads(f_in.read()) | |
| lines = python_type['root']['children'] | |
| for line in lines: | |
| tag = line.get('type') | |
| text = line.get("text") | |
| formats = line.get("formats") | |
| checked = line.get("checked") | |
| parsed_line = self.format_text(tag, text, formats, checked) | |
| print(parsed_line, file=f_out) | |
| def format_text(self, tag, text, formats, checked): | |
| conversion_table = { "h1" : ("# ", '') | |
| , "h2" : ("## ", '') | |
| , "h3" : ("### ", '') | |
| , "p" : ('', '') | |
| , "cl" : ("[x] " if checked else "[ ] ", "") | |
| , "b" : ('*', '*') | |
| , "i" : ('_', '_') | |
| , "code" : ('`', '`') | |
| , "pre" : ("``` \n", "\n```") | |
| , "ul": ("- ", '') | |
| , "ol": ("1. ", '') | |
| } | |
| if formats: | |
| chars = list(text) | |
| position_list = [] | |
| for (i_tag, indexes) in formats.items(): | |
| for index in indexes: | |
| position_list.append((i_tag, index)) | |
| position_list.sort(key=lambda x: x[1]) | |
| offset_counter = 0 | |
| for (i_tag, index) in position_list: | |
| chars.insert(index + offset_counter, conversion_table[i_tag][0]) | |
| offset_counter += 1 | |
| text = "".join(chars) | |
| opening = conversion_table[tag][0] | |
| closing = conversion_table[tag][1] | |
| return "{}{}{}".format(opening, text, closing) | |
| if __name__ == "__main__": | |
| p = JSONToMarkdownParser() | |
| p.parse("../different.json", "../result.md") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment