add crude implementation of permalinking

This commit is contained in:
Akbar Rahman 2022-03-02 02:19:04 +00:00
parent 334cf824c7
commit dbe652f531
Signed by: alvierahman90
GPG Key ID: 20609519444A1269
6 changed files with 78 additions and 4 deletions

View File

@ -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

51
n2w_add_uuid.py Executable file
View File

@ -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)

View File

@ -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=}")

View File

@ -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

View File

@ -12,6 +12,9 @@
<div id="contentWrapper">
<div id="sidebar">
<div id="header">
<p class="smallText">
<a href="/permalink/$uuid$">permalink</a>
</p>
<p class="smallText"> tags: [
$for(tags)$
<a href="/.tags/$tags$.html">$tags$</a>$sep$,

View File

@ -0,0 +1,5 @@
author: {{ author }}
date: {{ date }}
title: {{ title }}
tags: {{ tags }}
uuid: {{ uuid }}