Compare commits
2 Commits
1c4dad1b2e
...
b050b36747
Author | SHA1 | Date | |
---|---|---|---|
b050b36747
|
|||
8e09404520
|
49
notes2web.py
49
notes2web.py
@@ -12,6 +12,7 @@ import shutil
|
|||||||
import os
|
import os
|
||||||
import regex as re
|
import regex as re
|
||||||
import json
|
import json
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
|
||||||
TEXT_ARTICLE_TEMPLATE_FOOT = None
|
TEXT_ARTICLE_TEMPLATE_FOOT = None
|
||||||
@@ -21,6 +22,9 @@ INDEX_TEMPLATE_HEAD = None
|
|||||||
EXTRA_INDEX_CONTENT = None
|
EXTRA_INDEX_CONTENT = None
|
||||||
|
|
||||||
|
|
||||||
|
def is_plaintext(filename):
|
||||||
|
return re.match(r'^text/', magic.from_file(str(filename), mime=True)) is not None
|
||||||
|
|
||||||
def get_files(folder):
|
def get_files(folder):
|
||||||
markdown = []
|
markdown = []
|
||||||
plaintext = []
|
plaintext = []
|
||||||
@@ -33,7 +37,7 @@ def get_files(folder):
|
|||||||
name = os.path.join(root, filename)
|
name = os.path.join(root, filename)
|
||||||
if pathlib.Path(name).suffix == '.md':
|
if pathlib.Path(name).suffix == '.md':
|
||||||
markdown.append(name)
|
markdown.append(name)
|
||||||
elif re.match(r'^text/', magic.from_file(name, mime=True)):
|
elif is_plaintext(name):
|
||||||
plaintext.append(name)
|
plaintext.append(name)
|
||||||
other.append(name)
|
other.append(name)
|
||||||
else:
|
else:
|
||||||
@@ -41,6 +45,25 @@ def get_files(folder):
|
|||||||
|
|
||||||
return markdown, plaintext, other
|
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):
|
def git_filehistory(working_dir, filename):
|
||||||
print(f"{pathlib.Path(filename).relative_to(working_dir)=}")
|
print(f"{pathlib.Path(filename).relative_to(working_dir)=}")
|
||||||
@@ -178,7 +201,7 @@ def main(args):
|
|||||||
# extract tags from frontmatter, save to tag_dict
|
# extract tags from frontmatter, save to tag_dict
|
||||||
fm = frontmatter.load(filename)
|
fm = frontmatter.load(filename)
|
||||||
if isinstance(fm.get('tags'), list):
|
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 = {
|
t = {
|
||||||
'path': str(pathlib.Path(output_filename).relative_to(args.output_dir)),
|
'path': str(pathlib.Path(output_filename).relative_to(args.output_dir)),
|
||||||
'title': fm.get('title') or pathlib.Path(filename).name
|
'title': fm.get('title') or pathlib.Path(filename).name
|
||||||
@@ -210,10 +233,14 @@ def main(args):
|
|||||||
# update file if required
|
# update file if required
|
||||||
if update_required(filename, output_filename) or args.force:
|
if update_required(filename, output_filename) or args.force:
|
||||||
filehistory = git_filehistory(args.notes, filename)
|
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}',
|
f'--template={args.template}',
|
||||||
'-V', f'filehistory={filehistory}',
|
|
||||||
'-V', f'licenseFull={notes_license}',
|
|
||||||
'--mathjax',
|
'--mathjax',
|
||||||
'--toc', f'--toc-depth={args.toc_depth}'
|
'--toc', f'--toc-depth={args.toc_depth}'
|
||||||
])
|
])
|
||||||
@@ -248,7 +275,7 @@ def main(args):
|
|||||||
all_entries.append({
|
all_entries.append({
|
||||||
'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])),
|
'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])),
|
||||||
'title': title,
|
'title': title,
|
||||||
'tags': [],
|
'tags': [get_inherited_tags(filename, args.notes)],
|
||||||
'headers': []
|
'headers': []
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -259,11 +286,12 @@ def main(args):
|
|||||||
pathlib.Path(filename).relative_to(args.notes)
|
pathlib.Path(filename).relative_to(args.notes)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
title = os.path.basename(filename)
|
||||||
pathlib.Path(output_filename).parent.mkdir(parents=True, exist_ok=True)
|
pathlib.Path(output_filename).parent.mkdir(parents=True, exist_ok=True)
|
||||||
all_entries.append({
|
all_entries.append({
|
||||||
'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])),
|
'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])),
|
||||||
'title': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])),
|
'title': title,
|
||||||
'tags': [],
|
'tags': [get_inherited_tags(filename, args.notes)],
|
||||||
'headers': []
|
'headers': []
|
||||||
})
|
})
|
||||||
shutil.copyfile(filename, output_filename)
|
shutil.copyfile(filename, output_filename)
|
||||||
@@ -307,6 +335,7 @@ def main(args):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
fullpath = directory.joinpath(path)
|
fullpath = directory.joinpath(path)
|
||||||
|
title = path.name
|
||||||
if path.suffix == '.html':
|
if path.suffix == '.html':
|
||||||
with open(fullpath) as fp:
|
with open(fullpath) as fp:
|
||||||
soup = bs(fp.read(), 'html.parser')
|
soup = bs(fp.read(), 'html.parser')
|
||||||
@@ -317,7 +346,7 @@ def main(args):
|
|||||||
title = pathlib.Path(path).stem
|
title = pathlib.Path(path).stem
|
||||||
elif fullpath.is_dir():
|
elif fullpath.is_dir():
|
||||||
title = path
|
title = path
|
||||||
else:
|
elif is_plaintext(fullpath):
|
||||||
# don't add plaintext files to index, since they have a html wrapper
|
# don't add plaintext files to index, since they have a html wrapper
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -326,7 +355,7 @@ def main(args):
|
|||||||
|
|
||||||
indexentries.append({
|
indexentries.append({
|
||||||
'title': str(title),
|
'title': str(title),
|
||||||
'path': str(path),
|
'path': './' + str(path),
|
||||||
'isdirectory': fullpath.is_dir()
|
'isdirectory': fullpath.is_dir()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
16
readme.md
16
readme.md
@@ -61,6 +61,22 @@ The included `n2w_add_uuid.py` will add a UUID to a markdown file which does not
|
|||||||
already.
|
already.
|
||||||
Combine it with `find` to UUIDify all your markdown files (but make a backup first).
|
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
|
## CLI Usage
|
||||||
|
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user