2023-05-14 20:09:02 +03:00
|
|
|
from pelican import signals
|
2023-05-14 17:13:00 +03:00
|
|
|
from jinja2 import Environment, FileSystemLoader
|
|
|
|
|
import os
|
|
|
|
|
from pprint import pprint
|
2023-05-14 20:09:02 +03:00
|
|
|
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'
|
2023-05-14 17:13:00 +03:00
|
|
|
|
|
|
|
|
def generate_micron(generator):
|
2023-05-14 20:09:02 +03:00
|
|
|
# print(markdown_content)
|
2023-05-14 17:13:00 +03:00
|
|
|
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:
|
2023-05-14 20:09:02 +03:00
|
|
|
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
|
2023-05-14 17:13:00 +03:00
|
|
|
filename = obj.slug + ".mu"
|
|
|
|
|
template = environment.get_template( obj.template + '.mu')
|
|
|
|
|
context = dict()
|
|
|
|
|
context.update({ 'article' : obj })
|
|
|
|
|
context.update(obj.settings)
|
|
|
|
|
content = template.render(context)
|
|
|
|
|
output_micron = os.path.join(out_path, filename)
|
|
|
|
|
with open(output_micron, 'w') as mf:
|
2023-05-14 20:09:02 +03:00
|
|
|
mf.write(raw)
|
2023-05-14 17:13:00 +03:00
|
|
|
|
|
|
|
|
def register():
|
|
|
|
|
signals.article_generator_finalized.connect(generate_micron)
|