git @ Cat's Eye Technologies Feedmark / e433e00
Get a lot closer to round-tripping with reasonable reformatting. Chris Pressey 3 months ago
3 changed file(s) with 20 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
7474
7575 md = "{}\n{}\n\n".format(document.title, "=" * len(document.title))
7676 md += markdownize_properties(document.properties, property_priority_order)
77 md += document.preamble
77 md += document.preamble + "\n"
7878 for section in document.sections:
7979 md += "\n"
80 md += "### {}\n\n".format(section.title)
80 md += "### {}\n\n".format(section.title.strip())
8181 if section.images:
8282 for entry in section.images:
8383 if "link" in entry:
9393 )
9494 md += "\n"
9595 md += markdownize_properties(section.properties, property_priority_order)
96 md += section.body
96 md += section.body.lstrip() # FIXME lstrip this earlier
9797 md += markdownize_link_ref_defs(document.link_ref_defs)
9898 md += "\n"
9999 return md
4141
4242
4343 class Document(object):
44 def __init__(self, title):
45 self.title = title
44 def __init__(self):
45 self.title = "Untitled"
4646 self.properties = OrderedDict()
4747
4848 self.preamble = None
5151
5252 def __str__(self):
5353 return "document '{}'".format(self.title.encode("utf-8"))
54
55 def set_title(self, title):
56 self.title = title
5457
5558 def add_property(self, kind, key, value):
5659 if kind == ":":
55 from collections import OrderedDict
66 import re
77
8 from marko.block import Heading, BlankLine, List, Paragraph, LinkRefDef
8 from marko.block import SetextHeading, Heading, BlankLine, List, Paragraph, LinkRefDef
99 from marko.inline import Image, Link
1010 from marko.parser import Parser as MarkoParser
1111
6868
6969 renderer = CleanMarkdownRenderer()
7070
71 document = None
71 document = Document()
7272 preamble = ""
7373 section = None
7474 reading_images = True
7575 reading_properties = True
7676
7777 for child in marko_document.children:
78 if isinstance(child, Heading):
78 if isinstance(child, SetextHeading):
79 title_text = child.children[0].children
80 document.set_title(title_text)
81 elif isinstance(child, Heading):
7982 title_text = child.children[0].children
8083 if child.level == 1:
81 document = Document(title_text)
84 document.set_title(title_text)
8285 elif child.level == 3:
8386 section = Section(title_text)
8487 reading_images = True
8588 reading_properties = True
86 if not document:
87 document = Document("Untitled")
8889 document.sections.append(section)
8990 section.document = document
91 elif section:
92 section.add_to_body(renderer.render(child).strip())
93 else:
94 preamble += renderer.render(child)
9095 elif reading_properties and isinstance(child, List):
9196 reading_images = False
9297 reading_properties = False
9499 listitem_text = renderer.render(listitem).strip()
95100 kind, key, value = parse_property(listitem_text)
96101 if not section:
97 if not document:
98 document = Document("Untitled")
99102 document.add_property(kind, key, value)
100103 else:
101104 section.add_property(kind, key, value)
102105 elif isinstance(child, LinkRefDef):
103106 # LinkRefDef elements always go in the main document, not sections.
104 # print(child.label.text, child.dest, child.title)
105107 document.link_ref_defs.add(
106108 strip_square_brackets(child.label.text), child.dest, child.title
107109 )
115117 if section:
116118 section.add_to_body(renderer.render(child).strip())
117119 else:
118 preamble += renderer.render(child).strip()
120 preamble += renderer.render(child)
119121
120 if not document:
121 document = Document("Untitled")
122122 document.preamble = preamble.strip()
123123
124124 return document