Skip to content

Instantly share code, notes, and snippets.

@franalbani
Created July 10, 2022 02:48
Show Gist options
  • Save franalbani/656cd60d7501d20a68a0f23bfd13da23 to your computer and use it in GitHub Desktop.
Save franalbani/656cd60d7501d20a68a0f23bfd13da23 to your computer and use it in GitHub Desktop.
Script for fixing PDF image references in exported logseq graph.
#!/usr/bin/env python3
from re import sub, Match
from pathlib import Path
from typer import Typer
from functools import partial
from itertools import chain
from typing import List
from sh import cp
main = Typer(add_completion=False)
PATTERN = '\(\([0-9a-z\-]+\)\)'
# matches things like ((62a6a8e3-1b2c-42de-aaab-0b495459b61f))
def fixer(assets : List[Path], match : Match):
# Removes double parenthesis:
ref_id = match.group(0)[2:-2]
# Takes first matching filename:
asset = next(chain(filter(lambda a: ref_id in a.name, assets), [None]))
if asset:
# Copy asset to public assets folder:
cp(asset, Path('./assets/'))
new_path = Path('../assets') / asset.name
# If no asset was found, we return the same string:
token = f'![{asset.name}]({new_path})' if asset else match.group(0)
print(f'* {ref_id = }\n* {asset = }\n* {token = }\n')
return token
@main.command()
def _main(assets_dir : Path):
try:
html = open('./index.html').read()
except FileNotFoundError:
print('You must run this in the exported folder.')
raise SystemExit(1)
assets = list(assets_dir.glob('*.png'))
fixed_html = sub(PATTERN, partial(fixer, assets), html)
open('./index.html', 'w').write(fixed_html)
'main' in __name__ and main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment