logseq-publish-excalidraw-t.../main.py

106 lines
3.4 KiB
Python
Raw Normal View History

2023-10-10 01:51:11 +00:00
from pathlib import Path
from sys import argv, stderr
import re
excalidraw_re = re.compile(r"\[\[(.*\.excalidraw)\]\]")
def eprint(*args, **kwargs):
print(*args, file=stderr, **kwargs)
def iter_svgs(vault_path, draws):
for draw in draws:
svg = Path("assets/excalidraw_svg").joinpath(Path(draw).with_suffix(".svg").name)
if vault_path.joinpath(svg).is_file():
2023-10-13 16:21:25 +00:00
print("::debug::[found]", svg)
2023-10-10 01:51:11 +00:00
yield draw, svg
else:
2023-10-13 16:21:25 +00:00
eprint("::error::[missing]", svg)
2023-10-10 01:51:11 +00:00
yield draw, None
def iter_refs(vault_path):
pages = [path for path in vault_path.joinpath("pages").glob("*") if path.is_file()]
for page in pages:
with open(page) as f:
content = f.read()
for ref_match in excalidraw_re.finditer(content):
yield page, ref_match
def get_draws(vault_path):
draws = set()
pages = set()
for page, ref_match in iter_refs(vault_path):
draws.add(ref_match[1])
pages.add(page)
return pages, draws
def main() -> None:
"""
Replaces excalidraw file references with svg references
If <drawing_name>.svg exists in assets/excalidraw_svg it will be used,
otherwise it will be replaced with
:return:
"""
vault_path = Path(argv[1]).expanduser()
home_md_file = None
if len(argv) > 2:
home_md_file = Path(argv[2]).expanduser()
if not home_md_file:
home_md_file = None
2023-10-10 01:51:11 +00:00
pages, draws = get_draws(vault_path)
2023-10-13 17:30:11 +00:00
missing_svgs = []
2023-10-10 01:51:11 +00:00
replace_dict = {}
for draw, svg in iter_svgs(vault_path, draws):
if svg is None:
replace_dict[f"[[{draw}]]"] = "<h1 style='color: #ff0000'>MISSING IMAGE</h1>"
2023-10-13 17:30:11 +00:00
missing_svgs.append(draw)
2023-10-10 01:51:11 +00:00
else:
2023-10-10 03:48:40 +00:00
replace_dict[f"[[{draw}]]"] = f"![{draw}](../{svg})"
2023-10-10 01:51:11 +00:00
for page in pages:
with open(page) as f:
content = f.read()
for prev, new in replace_dict.items():
content = content.replace(prev, new)
with open(page, "w") as f:
f.write(content)
2023-10-13 17:30:11 +00:00
if missing_svgs:
if home_md_file is not None:
eprint(f"::notice::Writing error message to {home_md_file}")
2023-10-13 17:30:11 +00:00
file_error = "\n" + f"""
- <h1 style="color: red;">Some drawings were not loaded</h1>
\t- ## How to fix as a Contributor
\t\t- drawings are located in <vault path>/draws
\t\t- each ".excalidraw" drawing file should have an equivalent ".svg" file in <vault path>/assets/excalidraw_svg with the same basename
\t\t- ### Example
\t\t ```
\t\t <vault path>/draws/2023-10-08-21-23-31.excalidraw
\t\t <vault path>/assets/excalidraw_svg/2023-10-08-21-23-31.svg
\t\t ```
\t\t- to create a missing svg file, head to https://excalidraw.com/, paste the .excalidraw file, Click the Hamburger Menu > Export Image
\t\t\t- Enable "Background" and check if you need Dark or Light theme
\t\t\t- Click "SVG" and copy to <vault path>/assets/excalidraw_svgs
\t\t\t- Rename the file so the base names match
2023-10-13 17:30:11 +00:00
\t- ## The svgs for following drawing are missing"""
for draw in missing_svgs:
2023-10-13 17:33:47 +00:00
file_error += f"\n\t\t- `{draw}`"
2023-10-13 17:30:11 +00:00
with open(home_md_file, "a") as f:
f.write(file_error)
else:
eprint("::warning::home-md-file not set for logseq-publish-excalidraw-to-svg, won't write error message to "
"website")
2023-10-10 01:51:11 +00:00
if __name__ == "__main__":
main()