From 485022a1a64ca8a29d26641107b0a41b4dee0644 Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Tue, 8 Mar 2022 11:56:59 +0000 Subject: [PATCH] switch to permalink system that redirects to current page location --- Makefile | 1 + notes2web.py | 27 ++++++++++++++++----------- permalink.js | 8 ++++++++ templates/article.html | 2 +- templates/permalink_index.html | 19 +++++++++++++++++++ 5 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 permalink.js create mode 100644 templates/permalink_index.html diff --git a/Makefile b/Makefile index 326b892..b8eb4b7 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ install: cp fuse.js /opt/notes2web cp search.js /opt/notes2web cp toc_search.js /opt/notes2web + cp permalink.js /opt/notes2web uninstall: rm -rf /usr/local/bin/notes2web.py /usr/local/bin/n2w_add_uuid.py /opt/notes2web diff --git a/notes2web.py b/notes2web.py index 984a4d3..b3a582e 100755 --- a/notes2web.py +++ b/notes2web.py @@ -113,11 +113,13 @@ def get_args(): parser.add_argument('-I', '--template-index-foot', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/templates/indexfoot.html')) parser.add_argument('-s', '--stylesheet', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/styles.css')) parser.add_argument('--home_index', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/templates/home_index.html')) + parser.add_argument('--permalink_index', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/templates/permalink_index.html')) parser.add_argument('-e', '--extra-index-content', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/templates/extra_index_content.html')) parser.add_argument('-n', '--index-article-names', action='append', default=['index.md']) parser.add_argument('-F', '--force', action="store_true", help="Generate new output html even if source file was modified before output html") parser.add_argument('--fuse', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/fuse.js')) parser.add_argument('--searchjs', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/search.js')) + parser.add_argument('--permalinkjs', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/permalink.js')) parser.add_argument('--tocsearchjs', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/toc_search.js')) parser.add_argument('--toc-depth', type=int, default=6, dest='toc_depth') return parser.parse_args() @@ -158,6 +160,7 @@ def main(args): all_entries=[] dirs_with_index_article = [] tag_dict = {} + permalink_to_filepath = {} print(f"{markdown_files=}") for filename in markdown_files: @@ -185,10 +188,6 @@ 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') @@ -198,12 +197,16 @@ def main(args): header_lines.append(" ".join(line.split(" ")[1:])) all_entries.append({ - 'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])), + 'path': '/' + str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])), 'title': fm.get('title') or pathlib.Path(filename).name, 'tags': fm.get('tags'), - 'headers': header_lines + 'headers': header_lines, + 'uuid': fm.get('uuid') }) + if 'uuid' in fm.keys(): + permalink_to_filepath[fm['uuid']] = all_entries[-1]['path'] + # update file if required if update_required(filename, output_filename) or args.force: filehistory = git_filehistory(args.notes, filename) @@ -219,11 +222,6 @@ 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) @@ -359,6 +357,7 @@ def main(args): shutil.copyfile(args.fuse, args.output_dir.joinpath('fuse.js')) shutil.copyfile(args.searchjs, args.output_dir.joinpath('search.js')) shutil.copyfile(args.tocsearchjs, args.output_dir.joinpath('toc_search.js')) + shutil.copyfile(args.permalinkjs, args.output_dir.joinpath('permalink.js')) with open(args.output_dir.joinpath('index.html'), 'w+') as fp: with open(args.home_index) as fp2: html = re.sub(r'\$title\$', args.output_dir.parts[0], fp2.read()) @@ -366,6 +365,12 @@ def main(args): html = re.sub(r'\$data\$', json.dumps(all_entries), html) + fp.write(html) + permalink_dir = args.output_dir.joinpath('permalink') + permalink_dir.mkdir(exist_ok=True) + with open(args.permalink_index) as fp: + html = re.sub(r'\$data\$', json.dumps(permalink_to_filepath), fp.read()) + with open(permalink_dir.joinpath('index.html'), 'w+') as fp: fp.write(html) print(tag_dict) diff --git a/permalink.js b/permalink.js new file mode 100644 index 0000000..3421084 --- /dev/null +++ b/permalink.js @@ -0,0 +1,8 @@ +'use strict'; + +const MANUAL_REDIRECT = document.getElementById('manual_redirect'); + +const newLocation = data[new URLSearchParams(window.location.search).get('uuid')]; + +MANUAL_REDIRECT.href = newLocation; +window.location = newLocation; diff --git a/templates/article.html b/templates/article.html index 4d808ed..ba1295d 100644 --- a/templates/article.html +++ b/templates/article.html @@ -13,7 +13,7 @@