tag inheritance

This commit is contained in:
Akbar Rahman 2022-11-11 12:06:46 +00:00
parent 1c4dad1b2e
commit 8e09404520
Signed by: alvierahman90
GPG Key ID: 20609519444A1269
2 changed files with 46 additions and 6 deletions

View File

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

View File

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