From b1879c5a5583a60db6ecbfeb5d5cf6d8c312eaa3 Mon Sep 17 00:00:00 2001 From: NN Solex Date: Sun, 14 May 2023 20:09:02 +0300 Subject: [PATCH] parsing markdown files --- web/plugins/micron/micron.py | 58 ++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 3 deletions(-) diff --git a/web/plugins/micron/micron.py b/web/plugins/micron/micron.py index 9a978d7..073e6ec 100644 --- a/web/plugins/micron/micron.py +++ b/web/plugins/micron/micron.py @@ -1,16 +1,68 @@ -from pelican import signals, generators -from itertools import chain +from pelican import signals from jinja2 import Environment, FileSystemLoader import os from pprint import pprint +import mistune +import re + +class Markdown2Micron(mistune.HTMLRenderer): + def text(self, text) -> str: + return text + def emphasis(self, text: str) -> str: + return '`*' + text + '`*' + def strong(self, text: str) -> str: + return '`!' + text + '`!' + def codespan(self, text: str) -> str: + return '`=' + text + '`=' + def heading(self, text, level, **attrs) -> str: + if level > 3: + level = 3 + return '>' * level + text + '\n' + def link(self, text: str, url: str, title=None) -> str: + s = self.safe_url(url) + if text: + s = text + '`' + s + return f'`[{s}]' + def image(self, text: str, url: str, title=None) -> str: + s = self.safe_url(url) + if title: + s = title + '`' + s + return f'`[{s}]' + def blank_line(self) -> str: + return '' + '\n' + def linebreak(self) -> str: + return '\n' + def softbreak(self) -> str: + return '\n' + def inline_html(self, html: str) -> str: + return '`=\n' + html + '`=\n' + def thematic_break(self): + return '-' + '\n' + def paragraph(self, text): + return text + '\n' + def block_quote(self, text: str) -> str: + return '>>>>' + text + def list(self, text: str, ordered: bool, **attrs) -> str: + return text + '\n' + def list_item(self, text: str) -> str: + return '+ ' + text + '\n' + def block_code(self, code: str, info=None) -> str: + return '`=\n' + code + '`=\n' def generate_micron(generator): + # print(markdown_content) template_path = os.path.join(generator.settings.get('THEME'), 'templates') out_path = generator.settings.get('MICRON_PATH', 'micron/') environment = Environment(loader=FileSystemLoader(template_path)) if not os.path.exists(out_path): os.mkdir(out_path) for obj in generator.articles: + with open(str(obj), 'r') as mdf: + raw = mdf.read() + print(raw.split('\n')) + nometa = [re.sub(r'(\w+):\s*(?:"([^"]*)"|(.+))$', '', line) for line in raw.split('\n')] + raw = '\n'.join([item for item in nometa if item]) + # to do: put raw content into context filename = obj.slug + ".mu" template = environment.get_template( obj.template + '.mu') context = dict() @@ -19,7 +71,7 @@ def generate_micron(generator): content = template.render(context) output_micron = os.path.join(out_path, filename) with open(output_micron, 'w') as mf: - mf.write(content) + mf.write(raw) def register(): signals.article_generator_finalized.connect(generate_micron) \ No newline at end of file