switch to permalink system that redirects to current page location

This commit is contained in:
Akbar Rahman 2022-03-08 11:56:59 +00:00
parent dbe652f531
commit 485022a1a6
Signed by: alvierahman90
GPG Key ID: 20609519444A1269
5 changed files with 45 additions and 12 deletions

View File

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

View File

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

8
permalink.js Normal file
View File

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

View File

@ -13,7 +13,7 @@
<div id="sidebar">
<div id="header">
<p class="smallText">
<a href="/permalink/$uuid$">permalink</a>
<a href="/permalink?uuid=$uuid$">permalink</a>
</p>
<p class="smallText"> tags: [
$for(tags)$

View File

@ -0,0 +1,19 @@
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta charset="utf-8">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Open+Sans&display=swap" />
<link rel="stylesheet" type="text/css" href="/styles.css" />
<title></title>
</head>
<body>
<div id="content">
<p>
You should be being redirected...
Otherwise, click <a id="manual_redirect">here</a>.
</p>
<p class="smallText"> page generated by <a href="https://github.com/alvierahman90/notes2web">notes2web</a></p>
</div>
<script> const data = $data$ </script>
<script src="/permalink.js"> </script>
</body>