All checks were successful
Deploy Quartz site to GitHub Pages / build (push) Successful in 1m44s
57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
import pathlib
|
|
import re
|
|
|
|
import jinja2
|
|
from markdown.core import Markdown
|
|
from markdown.extensions import Extension
|
|
from markdown.preprocessors import Preprocessor
|
|
from obsidian_parser import Vault
|
|
|
|
|
|
root_dir = pathlib.Path(__file__).parent
|
|
vault = Vault(root_dir / ".." / "content")
|
|
note = vault.get_note("Biokemi/Cellulära processer/Transport över cellmembran/Anteckningar.md")
|
|
loader = jinja2.FileSystemLoader(root_dir / "templates")
|
|
env = jinja2.Environment(loader=loader)
|
|
|
|
|
|
class ObsidianImage(Preprocessor):
|
|
def run(self, lines):
|
|
new_lines = []
|
|
for line in lines:
|
|
m = re.search(r"!\[\[(.*)\]\]", line)
|
|
if m:
|
|
if "|" in m.group(1):
|
|
img, width = m.group(1).split("|")
|
|
new_lines.append("<img src='../content/attachments/" + img + "' style='width:" + width + ";'/>")
|
|
else:
|
|
new_lines.append("<img src='../content/attachments/" + m.group(1) + "'/>")
|
|
else:
|
|
new_lines.append(line)
|
|
return new_lines
|
|
|
|
|
|
class MyExtension(Extension):
|
|
def extendMarkdown(self, md):
|
|
md.preprocessors.register(ObsidianImage(md), 'mypattern', 175)
|
|
|
|
m = Markdown(extensions=[MyExtension()])
|
|
env.filters["markdown"] = m.convert
|
|
|
|
output = root_dir / "test.html"
|
|
template = env.get_template("base.html")
|
|
|
|
def out_content(self):
|
|
content = note.content
|
|
if content.startswith("---\n"):
|
|
content = content.split("---\n", 2)[2]
|
|
return content
|
|
|
|
note.__class__.our_content = property(out_content)
|
|
with output.open("w", encoding="utf-8") as f:
|
|
data = template.render(note=note, vault=vault)
|
|
f.write(data)
|
|
|
|
import webbrowser
|
|
webbrowser.open(output.as_uri())
|
|
print(f"Written to {output}") |