2 changed files with 90 additions and 1 deletions
@ -0,0 +1,89 @@
@@ -0,0 +1,89 @@
|
||||
import click |
||||
import pathlib |
||||
import tempfile |
||||
import subprocess |
||||
|
||||
OSIDIAN_SEPARATOR ="---\n" |
||||
|
||||
|
||||
def get_destination() -> pathlib.Path(): |
||||
scratch = pathlib.Path("/", "mnt", "c", "Users", "Holgi", "Desktop", "Scratch") |
||||
if scratch.is_dir(): |
||||
return scratch |
||||
return pathlib.Path(".") |
||||
|
||||
def split_header(text:str) -> (str, str): |
||||
(left, sep, right) = text.strip().partition(OSIDIAN_SEPARATOR) |
||||
if sep != OSIDIAN_SEPARATOR: |
||||
return ("", left) |
||||
(left, sep, right) = right.strip().partition(OSIDIAN_SEPARATOR) |
||||
if sep != OSIDIAN_SEPARATOR: |
||||
return ("", left) |
||||
return (left.strip(), right.strip()) |
||||
|
||||
|
||||
def _parse_date(iso_date): |
||||
try: |
||||
(year, month, day) = iso_date.split("-") |
||||
return f"{day}.{month}.{year}" |
||||
except ValueError: |
||||
return iso_date |
||||
|
||||
def _parse_traveller(name): |
||||
translation_table = str.maketrans("", "", "[@/]") |
||||
return name.translate(translation_table) |
||||
|
||||
def parse_header(text:str) -> dict[str, str]: |
||||
FIELD_PARSERS = { |
||||
"traveller": _parse_traveller, |
||||
"end": _parse_date, |
||||
"start": _parse_date, |
||||
} |
||||
result = {} |
||||
for line in text.strip().splitlines(): |
||||
key, sep, value = line.strip().partition(":") |
||||
if sep: |
||||
parser_func = FIELD_PARSERS.get(key.lower(), lambda x: x) |
||||
result[key.lower()] = parser_func(value.strip()) |
||||
return {k: v.replace('"', "").strip() for k, v in result.items()} |
||||
|
||||
def fill_fields(body, fields): |
||||
for key, value in fields.items(): |
||||
search = f"`= this.{key}`" |
||||
body = body.replace(search, value) |
||||
return body |
||||
|
||||
|
||||
def convert_file(markdown) -> str: |
||||
path = pathlib.Path(markdown) |
||||
content = path.read_text() |
||||
header, body = split_header(content) |
||||
|
||||
fields = parse_header(header) |
||||
text = fill_fields(body, fields) |
||||
with tempfile.TemporaryDirectory() as tmp_name: |
||||
tempdir = pathlib.Path(tmp_name) |
||||
markdown = pathlib.Path(tempdir) / "obsidian.md" |
||||
markdown.write_text(text) |
||||
|
||||
typist = pathlib.Path(tempdir) / "obsidian.typ" |
||||
cmd = ["pandoc", f"{markdown}", "--to", "typst", "-o", f"{typist}"] |
||||
subprocess.run(cmd) |
||||
|
||||
output = get_destination() / f"Checkliste {path.stem}.pdf" |
||||
cmd = ["typst", "compile", f"{typist}", f"{output}"] |
||||
subprocess.run(cmd) |
||||
|
||||
moved = get_destination() / f"Checkliste {path.name}" |
||||
subprocess.run(["mv", f"{path}", moved]) |
||||
return f"{output}" |
||||
|
||||
|
||||
@click.command() |
||||
@click.argument( |
||||
"markdowns", nargs=-1, type=click.Path(exists=True, file_okay=True, dir_okay=False) |
||||
) |
||||
def convert(markdowns): |
||||
for name in markdowns: |
||||
path = convert_file(name) |
||||
print(path) |
||||
Loading…
Reference in new issue