Keep pushing renderer down, although this is not entirely pretty.
Chris Pressey
3 months ago
7 | 7 | |
8 | 8 | import atomize |
9 | 9 | |
10 | from feedmark.feeds import ( | |
10 | from ..feeds import ( | |
11 | 11 | extract_feed_properties, |
12 | 12 | extract_sections, |
13 | 13 | construct_entry_url, |
14 | 14 | ) |
15 | from feedmark.formats.markdown import markdown_to_html5 | |
15 | from ..renderer import CleanMarkdownRenderer | |
16 | from .markdown import markdown_to_html5 | |
16 | 17 | |
17 | 18 | |
18 | 19 | def convert_section_to_entry(section, properties, markdown_links_base=None): |
20 | renderer = CleanMarkdownRenderer() | |
19 | 21 | |
20 | 22 | guid = properties["url"] + "/" + section.title |
21 | 23 | updated = section.publication_date |
22 | 24 | |
23 | summary = atomize.Summary(markdown_to_html5(section.body), content_type="html") | |
25 | summary = atomize.Summary( | |
26 | markdown_to_html5(section.body_markdown(renderer)), content_type="html" | |
27 | ) | |
24 | 28 | |
25 | 29 | links = [] |
26 | 30 | entry_url = construct_entry_url(section) |
100 | 100 | ) |
101 | 101 | md += "\n" |
102 | 102 | md += markdownize_properties(section.properties, property_priority_order) |
103 | md += re.sub(r"^\n+", "", section.body) | |
103 | md += re.sub(r"^\n+", "", section.body_markdown(renderer)) | |
104 | 104 | md = re.sub(r"\n+$", "\n", md) |
105 | 105 | md += "\n" |
106 | 106 | md = re.sub(r"\n+$", "\n", md) |
5 | 5 | import json |
6 | 6 | import sys |
7 | 7 | |
8 | from feedmark.loader import ( | |
8 | from .loader import ( | |
9 | 9 | read_document_from, |
10 | 10 | read_refdex_from, |
11 | 11 | convert_refdex_to_single_filename_refdex, |
12 | 12 | ) |
13 | from .renderer import CleanMarkdownRenderer | |
13 | 14 | |
14 | 15 | |
15 | 16 | def sys_main(): |
237 | 238 | if options.by_publication_date: |
238 | 239 | from feedmark.feeds import construct_entry_url |
239 | 240 | |
241 | renderer = CleanMarkdownRenderer() | |
242 | ||
240 | 243 | dated_items = [] |
241 | 244 | for document in documents: |
242 | 245 | for section in document.sections: |
244 | 247 | "title": section.title, |
245 | 248 | "images": section.images, |
246 | 249 | "properties": section.properties, |
247 | "body": section.body, | |
250 | "body": section.body_markdown(renderer), | |
248 | 251 | "url": construct_entry_url(section), |
249 | 252 | } |
250 | 253 | dated_items.append((section.publication_date, section_json)) |
6 | 6 | import re |
7 | 7 | |
8 | 8 | from marko.block import LinkRefDefs |
9 | from marko.element import Element | |
9 | 10 | |
10 | 11 | from .formats.markdown import markdown_to_html5, markdown_to_html5_deep |
11 | 12 | from .renderer import CleanMarkdownRenderer |
46 | 47 | self.title = "Untitled" |
47 | 48 | self.properties = OrderedDict() |
48 | 49 | |
49 | self.header_comment = [] | |
50 | self.preamble = [] | |
50 | self.header_comment: list(Element) = [] | |
51 | self.preamble: list(Element) = [] | |
51 | 52 | self.sections: list(Section) = [] |
52 | 53 | self.link_ref_defs = LinkRefDefs() |
53 | 54 | |
124 | 125 | self.document = None |
125 | 126 | self.title = title |
126 | 127 | self.properties = OrderedDict() |
127 | self._body_lines = [] | |
128 | self.body: list(Element) = [] | |
128 | 129 | self.images = [] |
129 | 130 | |
130 | 131 | def __str__(self): |
143 | 144 | else: |
144 | 145 | raise NotImplementedError(kind) |
145 | 146 | |
146 | def add_to_body(self, line): | |
147 | self._body_lines.append(line) | |
148 | ||
149 | 147 | def add_image(self, image_record): |
150 | 148 | self.images.append(image_record) |
151 | 149 | |
152 | @property | |
153 | def body(self): | |
154 | return "\n".join(self._body_lines) | |
150 | def body_markdown(self, renderer): | |
151 | md = "" | |
152 | for element in self.body: | |
153 | md += renderer.render(element) | |
154 | md += "\n" | |
155 | md = re.sub(r"^\n+", "", md) | |
156 | md = re.sub(r"\n+$", "\n", md) | |
157 | return md | |
155 | 158 | |
156 | 159 | @property |
157 | 160 | def publication_date(self): |
176 | 179 | return re.sub(r"[\s\/\.\'-]+", "-", title) |
177 | 180 | |
178 | 181 | def to_json_data(self, **kwargs): |
182 | renderer = CleanMarkdownRenderer() | |
183 | ||
184 | body = self.body_markdown(renderer) | |
185 | properties = self.properties | |
179 | 186 | |
180 | 187 | if kwargs.get("htmlize", False): |
181 | body = markdown_to_html5(self.body, link_ref_defs=kwargs["link_ref_defs"]) | |
188 | body = markdown_to_html5(body, link_ref_defs=kwargs["link_ref_defs"]) | |
182 | 189 | properties = markdown_to_html5_deep( |
183 | 190 | self.properties, link_ref_defs=kwargs["link_ref_defs"] |
184 | 191 | ) |
185 | else: | |
186 | body = self.body | |
187 | properties = self.properties | |
188 | 192 | |
189 | 193 | if kwargs.get("ordered", False): |
190 | 194 | properties_list = [] |
104 | 104 | document.sections.append(section) |
105 | 105 | section.document = document |
106 | 106 | elif section: |
107 | section.add_to_body(renderer.render(child).strip()) | |
107 | section.body.append(child) | |
108 | 108 | else: |
109 | 109 | document.preamble.append(child) |
110 | 110 | elif isinstance(child, HTMLBlock) and not section: |
132 | 132 | reading_images = False |
133 | 133 | reading_properties = False |
134 | 134 | if section: |
135 | text = renderer.render(child) | |
136 | text = re.sub(r"^\n+", "", text) | |
137 | text = re.sub(r"\n+$", "", text) | |
138 | section.add_to_body(text) | |
135 | section.body.append(child) | |
139 | 136 | else: |
140 | 137 | document.preamble.append(child) |
141 | 138 |
354 | 354 | "sections": [ |
355 | 355 | { |
356 | 356 | "anchor": "lo-logic-most-unintuitive-application-of-the-axiom-of-choice-mathoverflow", |
357 | "body": "\n", | |
357 | "body": "", | |
358 | 358 | "images": [], |
359 | 359 | "properties": { |
360 | 360 | "url": "https://mathoverflow.net/questions/20882/most-unintuitive-application-of-the-axiom-of-choice" |
363 | 363 | }, |
364 | 364 | { |
365 | 365 | "anchor": "set-theory-set-theories-without-junk-theorems-mathoverflow", |
366 | "body": "\n", | |
366 | "body": "", | |
367 | 367 | "images": [], |
368 | 368 | "properties": { |
369 | 369 | "url": "http://mathoverflow.net/questions/90820/set-theories-without-junk-theorems/90945#90945" |
372 | 372 | }, |
373 | 373 | { |
374 | 374 | "anchor": "the-origin-of-the-number-zero-history-smithsonian", |
375 | "body": "\n", | |
375 | "body": "", | |
376 | 376 | "images": [], |
377 | 377 | "properties": { |
378 | 378 | "url": "https://www.smithsonianmag.com/history/origin-number-zero-180953392/" |
381 | 381 | }, |
382 | 382 | { |
383 | 383 | "anchor": "zajo-appler-apple-emulator-for-ms-dos-written-in-8088-assembly", |
384 | "body": "\n", | |
384 | "body": "", | |
385 | 385 | "images": [], |
386 | 386 | "properties": {"url": "https://github.com/zajo/appler"}, |
387 | 387 | "title": r"zajo/appler: Apple \]\[ emulator for MS-DOS, written in 8088 assembly", |
388 | 388 | }, |
389 | 389 | { |
390 | 390 | "anchor": "computational-complexitytheory", |
391 | "body": "\nThat is not a link.", | |
391 | "body": "That is not a link.\n", | |
392 | 392 | "images": [], |
393 | 393 | "properties": {}, |
394 | 394 | "title": r"Computational \[Complexity\]\(Theory\)", |
439 | 439 | "sections": [ |
440 | 440 | { |
441 | 441 | "anchor": "bubble-squeak", |
442 | "body": "\n\nHave you heard, [Bubble & Squeak]()?\n\n* property3: three\n* property4: four\n", | |
442 | "body": "Have you heard, [Bubble & Squeak]()?\n\n* property3: three\n* property4: four\n", | |
443 | 443 | "images": [], |
444 | 444 | "properties": {"property1": "one", "property2": "two"}, |
445 | 445 | "title": "Bubble & Squeak", |
446 | 446 | }, |
447 | 447 | { |
448 | 448 | "anchor": "filthy-rich-catflap", |
449 | "body": "\n\nIt seems that you have not heard.\n\n* property3: four\n", | |
449 | "body": "It seems that you have not heard.\n\n* property3: four\n", | |
450 | 450 | "images": [], |
451 | 451 | "properties": {"property3": "three"}, |
452 | 452 | "title": "Filthy Rich & Catflap", |