5 | 5 |
from collections import OrderedDict
|
6 | 6 |
import re
|
7 | 7 |
|
8 | |
from marko.block import Heading, BlankLine, List, Paragraph, LinkRefDef
|
|
8 |
from marko.block import SetextHeading, Heading, BlankLine, List, Paragraph, LinkRefDef
|
9 | 9 |
from marko.inline import Image, Link
|
10 | 10 |
from marko.parser import Parser as MarkoParser
|
11 | 11 |
|
|
68 | 68 |
|
69 | 69 |
renderer = CleanMarkdownRenderer()
|
70 | 70 |
|
71 | |
document = None
|
|
71 |
document = Document()
|
72 | 72 |
preamble = ""
|
73 | 73 |
section = None
|
74 | 74 |
reading_images = True
|
75 | 75 |
reading_properties = True
|
76 | 76 |
|
77 | 77 |
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):
|
79 | 82 |
title_text = child.children[0].children
|
80 | 83 |
if child.level == 1:
|
81 | |
document = Document(title_text)
|
|
84 |
document.set_title(title_text)
|
82 | 85 |
elif child.level == 3:
|
83 | 86 |
section = Section(title_text)
|
84 | 87 |
reading_images = True
|
85 | 88 |
reading_properties = True
|
86 | |
if not document:
|
87 | |
document = Document("Untitled")
|
88 | 89 |
document.sections.append(section)
|
89 | 90 |
section.document = document
|
|
91 |
elif section:
|
|
92 |
section.add_to_body(renderer.render(child).strip())
|
|
93 |
else:
|
|
94 |
preamble += renderer.render(child)
|
90 | 95 |
elif reading_properties and isinstance(child, List):
|
91 | 96 |
reading_images = False
|
92 | 97 |
reading_properties = False
|
|
94 | 99 |
listitem_text = renderer.render(listitem).strip()
|
95 | 100 |
kind, key, value = parse_property(listitem_text)
|
96 | 101 |
if not section:
|
97 | |
if not document:
|
98 | |
document = Document("Untitled")
|
99 | 102 |
document.add_property(kind, key, value)
|
100 | 103 |
else:
|
101 | 104 |
section.add_property(kind, key, value)
|
102 | 105 |
elif isinstance(child, LinkRefDef):
|
103 | 106 |
# LinkRefDef elements always go in the main document, not sections.
|
104 | |
# print(child.label.text, child.dest, child.title)
|
105 | 107 |
document.link_ref_defs.add(
|
106 | 108 |
strip_square_brackets(child.label.text), child.dest, child.title
|
107 | 109 |
)
|
|
115 | 117 |
if section:
|
116 | 118 |
section.add_to_body(renderer.render(child).strip())
|
117 | 119 |
else:
|
118 | |
preamble += renderer.render(child).strip()
|
|
120 |
preamble += renderer.render(child)
|
119 | 121 |
|
120 | |
if not document:
|
121 | |
document = Document("Untitled")
|
122 | 122 |
document.preamble = preamble.strip()
|
123 | 123 |
|
124 | 124 |
return document
|