From dbe652f53172c51f1253020f52e39af89d7457e7 Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Wed, 2 Mar 2022 02:19:04 +0000 Subject: [PATCH] add crude implementation of permalinking --- Makefile | 4 +- n2w_add_uuid.py | 51 +++++++++++++++++++++ notes2web.py | 15 +++++- requirements.txt | 4 ++ templates/article.html | 3 ++ templates/n2w_add_uuid_frontmatter_template | 5 ++ 6 files changed, 78 insertions(+), 4 deletions(-) create mode 100755 n2w_add_uuid.py create mode 100644 templates/n2w_add_uuid_frontmatter_template diff --git a/Makefile b/Makefile index 9ca97b7..326b892 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ install: - cp notes2web.py /usr/local/bin + cp notes2web.py n2w_add_uuid.py /usr/local/bin pip3 install -r requirements.txt mkdir -p /opt/notes2web cp -r templates /opt/notes2web @@ -9,4 +9,4 @@ install: cp toc_search.js /opt/notes2web uninstall: - rm -rf /usr/local/bin/notes2web.py /opt/notes2web + rm -rf /usr/local/bin/notes2web.py /usr/local/bin/n2w_add_uuid.py /opt/notes2web diff --git a/n2w_add_uuid.py b/n2w_add_uuid.py new file mode 100755 index 0000000..8832c19 --- /dev/null +++ b/n2w_add_uuid.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +import editfrontmatter +import frontmatter +import pathlib +import sys +import uuid + +def get_args(): + """ Get command line arguments """ + + import argparse + parser = argparse.ArgumentParser() + parser.add_argument('filename', type=pathlib.Path) + parser.add_argument('--template', + default=pathlib.Path("/opt/notes2web/templates/n2w_add_uuid_frontmatter_template"), + type=pathlib.Path + ) + parser.add_argument('-w', '--write', action='store_true', + help='write to file instead of stdout') + return parser.parse_args() + + +def main(args): + """ Entry point for script """ + with open(args.template) as fp: + template_str=fp.read() + + with open(args.filename) as fp: + fm_pre = frontmatter.load(fp) + + processor = editfrontmatter.EditFrontMatter(file_path=args.filename, template_str=template_str) + fm_data = fm_pre.metadata + if 'uuid' not in fm_data.keys(): + fm_data['uuid'] = str(uuid.uuid4()) + + processor.run(fm_data) + + if args.write: + with open(args.filename, 'w') as fp: + fp.write(processor.dumpFileData()) + else: + print(processor.dumpFileData()) + return 0 + + +if __name__ == '__main__': + try: + sys.exit(main(get_args())) + except KeyboardInterrupt: + sys.exit(0) diff --git a/notes2web.py b/notes2web.py index 15f3a5d..984a4d3 100755 --- a/notes2web.py +++ b/notes2web.py @@ -84,10 +84,12 @@ def git_filehistory(working_dir, filename): return filehistory -def get_dirs(folder): +def get_dirs_to_index(folder): r = [] for root, folders, files in os.walk(folder): + if pathlib.Path(os.path.join(root, folder)).is_relative_to(folder.joinpath('permalink')): + continue [r.append(os.path.join(root, folder)) for folder in folders] return r @@ -183,6 +185,10 @@ def main(args): else: tag_dict[tag] = [t] + permalink_filename = None + if 'uuid' in fm.keys(): + permalink_filename = args.output_dir.joinpath('permalink').joinpath(fm['uuid']).joinpath('index.html') + # find headers in markdown with open(filename) as fp: lines = fp.read().split('\n') @@ -213,6 +219,11 @@ def main(args): with open(output_filename, 'w+') as fp: fp.write(html) + if permalink_filename is not None: + permalink_filename.parent.mkdir(parents=True, exist_ok=True) + with open(permalink_filename, 'w+') as fp: + fp.write(html) + print(f"{plaintext_files=}") for filename in plaintext_files: filehistory = git_filehistory(args.notes, filename) @@ -276,7 +287,7 @@ def main(args): - dirs_to_index = [args.output_dir.name] + get_dirs(args.output_dir) + dirs_to_index = [args.output_dir.name] + get_dirs_to_index(args.output_dir) print(f"{dirs_to_index=}") print(f"{dirs_with_index_article=}") diff --git a/requirements.txt b/requirements.txt index 728d73a..0a0fa6b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,8 @@ beautifulsoup4==4.9.3 +editfrontmatter==0.0.1 +Jinja2==3.0.3 +MarkupSafe==2.1.0 +oyaml==1.0 pypandoc==1.5 python-frontmatter==1.0.0 python-magic==0.4.24 diff --git a/templates/article.html b/templates/article.html index 13df54e..4d808ed 100644 --- a/templates/article.html +++ b/templates/article.html @@ -12,6 +12,9 @@