From 8e09404520d966847cfef3b13553076e18809753 Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Fri, 11 Nov 2022 12:06:46 +0000 Subject: [PATCH] tag inheritance --- notes2web.py | 36 ++++++++++++++++++++++++++++++------ readme.md | 16 ++++++++++++++++ 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/notes2web.py b/notes2web.py index dde8f18..bd11432 100755 --- a/notes2web.py +++ b/notes2web.py @@ -12,6 +12,7 @@ import shutil import os import regex as re import json +import yaml TEXT_ARTICLE_TEMPLATE_FOOT = None @@ -41,6 +42,25 @@ def get_files(folder): return markdown, plaintext, other +def get_inherited_tags(file, base_folder): + tags = [] + folder = pathlib.Path(file) + + while folder != base_folder.parent: + print(f"get_inherited_tags {folder=}") + folder = pathlib.Path(folder).parent + folder_metadata = folder.joinpath('.n2w.yml') + if not folder_metadata.exists(): + continue + + with open(folder.joinpath('.n2w.yml')) as fp: + folder_properties = yaml.safe_load(fp) + + tags += folder_properties.get('itags') + + print(f"get_inherited_tags {tags=}") + return tags + def git_filehistory(working_dir, filename): print(f"{pathlib.Path(filename).relative_to(working_dir)=}") @@ -178,7 +198,7 @@ def main(args): # extract tags from frontmatter, save to tag_dict fm = frontmatter.load(filename) if isinstance(fm.get('tags'), list): - for tag in fm.get('tags'): + for tag in fm.get('tags') + get_inherited_tags(filename, args.notes): t = { 'path': str(pathlib.Path(output_filename).relative_to(args.output_dir)), 'title': fm.get('title') or pathlib.Path(filename).name @@ -210,10 +230,14 @@ def main(args): # update file if required if update_required(filename, output_filename) or args.force: filehistory = git_filehistory(args.notes, filename) - html = pypandoc.convert_file(filename, 'html', extra_args=[ + with open(filename) as fp: + article = frontmatter.load(fp) + + article['tags'] += get_inherited_tags(filename, args.notes) + article['filehistory'] = filehistory + article['licenseFull'] = notes_license + html = pypandoc.convert_text(frontmatter.dumps(article), 'html', format='md', extra_args=[ f'--template={args.template}', - '-V', f'filehistory={filehistory}', - '-V', f'licenseFull={notes_license}', '--mathjax', '--toc', f'--toc-depth={args.toc_depth}' ]) @@ -248,7 +272,7 @@ def main(args): all_entries.append({ 'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])), 'title': title, - 'tags': [], + 'tags': [get_inherited_tags(filename, args.notes)], 'headers': [] }) @@ -263,7 +287,7 @@ def main(args): all_entries.append({ 'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])), 'title': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])), - 'tags': [], + 'tags': [get_inherited_tags(filename, args.notes)], 'headers': [] }) shutil.copyfile(filename, output_filename) diff --git a/readme.md b/readme.md index e23e896..db7ee86 100644 --- a/readme.md +++ b/readme.md @@ -61,6 +61,22 @@ The included `n2w_add_uuid.py` will add a UUID to a markdown file which does not already. Combine it with `find` to UUIDify all your markdown files (but make a backup first). +### Inherited Properties + +Notes can inherit a some properties from their parent folder(s) by creating a `.n2w.yml` file in a +folder. + +#### Tags + +If you have a folder `uni` with all you university notes, you might want all the files in there to +be tagged `uni`: + +`NOTES_PATH/uni/.n2w.yaml`: + +```yaml +itags: [ university ] +``` + ## CLI Usage ```