From 1e0135a8e5dcca1d604a46fadbf412bfea8338c6 Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Thu, 19 Aug 2021 13:41:19 +0100 Subject: [PATCH] move notes into notes subdirectory for cleaner look --- notes2web.py | 20 +++++++++++++++----- readme.md | 6 +++++- templates/home_index.html | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 templates/home_index.html diff --git a/notes2web.py b/notes2web.py index fd5cf53..9a6664b 100755 --- a/notes2web.py +++ b/notes2web.py @@ -11,12 +11,14 @@ import shutil import os import re + TEXT_ARTICLE_TEMPLATE_FOOT = None TEXT_ARTICLE_TEMPLATE_HEAD = None INDEX_TEMPLATE_FOOT = None INDEX_TEMPLATE_HEAD = None EXTRA_INDEX_CONTENT = None + def get_files(folder): markdown = [] plaintext = [] @@ -35,6 +37,7 @@ def get_files(folder): return markdown, plaintext, other + def get_dirs(folder): r = [] @@ -43,6 +46,7 @@ def get_dirs(folder): return r + def update_required(src_filename, output_filename): return not os.path.exists(output_filename) or os.path.getmtime(src_filename) > os.path.getmtime(output_filename) @@ -60,6 +64,7 @@ def get_args(): parser.add_argument('-i', '--template-index-head', type=pathlib.Path, default=pathlib.Path('/opt/notes2web/templates/indexhead.html')) 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('-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") @@ -105,12 +110,12 @@ def main(args): if os.path.basename(filename) in args.index_article_names: output_filename = os.path.join( - os.path.dirname(re.sub(f"^{args.notes.name}", args.output_dir.name, filename)), + os.path.dirname(re.sub(f"^{args.notes.name}", os.path.join(args.output_dir.name, 'notes', filename))), 'index.html' ) - dirs_with_index_article.append(os.path.dirname(re.sub(f"^{args.notes.name}", args.output_dir.name, filename))) + dirs_with_index_article.append(os.path.dirname(re.sub(f"^{args.notes.name}", os.path.join(args.output_dir.name, 'notes'), filename))) else: - output_filename = os.path.splitext(re.sub(f"^{args.notes.name}", args.output_dir.name, filename))[0] + '.html' + output_filename = os.path.splitext(re.sub(f"^{args.notes.name}", os.path.join(args.output_dir.name, 'notes'), filename))[0] + '.html' fm = frontmatter.load(filename) if isinstance(fm.get('tags'), list): @@ -137,7 +142,7 @@ def main(args): print(f"{plaintext_files=}") for filename in plaintext_files: title = os.path.basename(re.sub(f"^{args.notes.name}", args.output_dir.name, filename)) - output_filename = re.sub(f"^{args.notes.name}", args.output_dir.name, filename) + '.html' + output_filename = re.sub(f"^{args.notes.name}", os.path.join(args.output_dir.name, 'notes'), filename) + '.html' os.makedirs(os.path.dirname(output_filename), exist_ok=True) html = re.sub(r'\$title\$', title, TEXT_ARTICLE_TEMPLATE_HEAD) html = re.sub(r'\$h1title\$', title, html) @@ -151,7 +156,7 @@ def main(args): print(f"{other_files=}") for filename in other_files: - output_filename = re.sub(f"^{args.notes.name}", args.output_dir.name, filename) + output_filename = re.sub(f"^{args.notes.name}", os.path.join(args.output_dir.name, 'notes'), filename) os.makedirs(os.path.dirname(output_filename), exist_ok=True) shutil.copyfile(filename, output_filename) @@ -231,6 +236,11 @@ def main(args): fp.write(html) shutil.copyfile(args.stylesheet, os.path.join(args.output_dir.name, 'styles.css')) + with open(os.path.join(args.output_dir.name, 'index.html'), 'w+') as fp: + with open(args.home_index) as fp2: + html = re.sub(r'\$title\$', args.output_dir.parts[0], fp2.read()) + html = re.sub(r'\$h1title\$', args.output_dir.parts[0], html) + fp.write(html) print(tag_dict) return 0 diff --git a/readme.md b/readme.md index 3b19872..2a455db 100644 --- a/readme.md +++ b/readme.md @@ -40,7 +40,8 @@ Output of `notes2web.py --help`: usage: notes2web.py [-h] [-o OUTPUT_DIR] [-t TEMPLATE] [-H TEMPLATE_TEXT_HEAD] [-f TEMPLATE_TEXT_FOOT] [-i TEMPLATE_INDEX_HEAD] [-I TEMPLATE_INDEX_FOOT] [-s STYLESHEET] - [-e EXTRA_INDEX_CONTENT] [-n INDEX_ARTICLE_NAMES] + [--home_index HOME_INDEX] [-e EXTRA_INDEX_CONTENT] + [-n INDEX_ARTICLE_NAMES] [-F] notes positional arguments: @@ -55,8 +56,11 @@ optional arguments: -i TEMPLATE_INDEX_HEAD, --template-index-head TEMPLATE_INDEX_HEAD -I TEMPLATE_INDEX_FOOT, --template-index-foot TEMPLATE_INDEX_FOOT -s STYLESHEET, --stylesheet STYLESHEET + --home_index HOME_INDEX -e EXTRA_INDEX_CONTENT, --extra-index-content EXTRA_INDEX_CONTENT -n INDEX_ARTICLE_NAMES, --index-article-names INDEX_ARTICLE_NAMES + -F, --force Generate new output html even if source file was + modified before output html ``` The command will generate a website in the `output-dir` directory (`./web` by default). diff --git a/templates/home_index.html b/templates/home_index.html new file mode 100644 index 0000000..396f16d --- /dev/null +++ b/templates/home_index.html @@ -0,0 +1,16 @@ + + + + + +$title$ + + +

$h1title$

+

+These are my personal notes. Correctness is not guaranteed. +Browse here or by tag here. +

+ +

page generated by notes2web

+