parsing markdown files
This commit is contained in:
parent
306423cd99
commit
b1879c5a55
1 changed files with 55 additions and 3 deletions
|
|
@ -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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue