add tag browser generation

This commit is contained in:
Akbar Rahman 2023-09-17 20:18:24 +01:00 committed by Akbar Rahman
parent 29529cfd6a
commit 5bb40a57d1
Signed by: alvierahman90
GPG Key ID: 6217899F07CA2BDF
6 changed files with 75 additions and 34 deletions

View File

@ -1,6 +1,6 @@
install:
cp n2w_add_uuid.py /usr/local/bin
sed "s/N2W_COMMIT = \"\"/N2W_COMMIT = \"$$(git rev-parse --short HEAD)\"/" notes2web.py > /usr/local/bin/notes2web.py
sed "s/N2W_COMMIT = \"dev\"/N2W_COMMIT = \"$$(git rev-parse --short HEAD)\"/" notes2web.py > /usr/local/bin/notes2web.py
mkdir -p /opt/notes2web
cp -r templates js css /opt/notes2web
pip3 install -r requirements.txt

View File

@ -107,7 +107,7 @@ class FileMap:
#print(f"FileMap._get_index_entries({filepath=}): {entry=}")
entries.sort(key=lambda entry: str(entry['title']).lower())
entries.sort(key=lambda entry: str(entry.get('title', '')).lower())
entries.sort(key=lambda entry: entry['is_dir'], reverse=True)
return entries
@ -160,6 +160,10 @@ class FileMap:
def to_list(self):
return [ val for _, val in self._map.items() ]
def to_search_data(self):
"""
returns list of every file in map
"""

View File

@ -21,7 +21,7 @@ import requests
from fileproperties import FileMap
N2W_COMMIT = ""
N2W_COMMIT = "dev"
PANDOC_SERVER_URL = os.getenv("PANDOC_SERVER_URL", r"http://localhost:3030/")
PANDOC_TIMEOUT = int(os.getenv("PANDOC_TIMEOUT", "120"))
@ -38,7 +38,7 @@ JINJA_ENV = jinja2.Environment(
JINJA_TEMPLATES = {}
JINJA_TEMPLATE_TEXTARTICLE = JINJA_ENV.get_template("textarticle.html")
JINJA_TEMPLATE_HOME_INDEX = JINJA_ENV.get_template("home_index.html")
JINJA_TEMPLATE_DIRECTORY_INDEX = JINJA_ENV.get_template("index.html")
JINJA_TEMPLATE_INDEX = JINJA_ENV.get_template("index.html")
JINJA_TEMPLATE_ARTICLE = JINJA_ENV.get_template("article.html")
@ -172,43 +172,71 @@ def render_markdown(content):
def process_home_index(output_dir, search_data, notes_git_head_sha1=None):
def process_home_index(args, notes_git_head_sha1=None):
"""
create home index.html in output_dir
"""
post = {
'title': 'gronk',
'content': ''
}
custom_content_file = args.notes.joinpath('index.md')
print(f'{custom_content_file=}')
if custom_content_file.is_file():
fmpost = frontmatter.loads(custom_content_file.read_text()).to_dict()
for key, val in fmpost.items():
post[key] = val
post['content'] = render_markdown(post['content'])
html = JINJA_TEMPLATE_HOME_INDEX.render(
n2w_commit = N2W_COMMIT,
search_data=search_data,
search_data = FILEMAP.to_search_data(),
notes_git_head_sha1 = notes_git_head_sha1,
post=post
)
with open(output_dir.joinpath('index.html'), 'w+', encoding='utf-8') as file_pointer:
file_pointer.write(html)
args.output_dir.joinpath('index.html').write_text(html)
def generate_variable_browser(output_dir, posts, variable_name) :
def generate_tag_browser(output_dir) :
"""
generate a directory that lets you groub by and browse by any given tag. e.g. tags, authors
"""
groups = {}
tags = {}
for key, post in posts.iter():
group_val = post.get(variable_name, None)
if group_val is None:
for post in FILEMAP.to_list():
post['path'] = post['dst_path']['web']
if 'tags' not in post.keys():
continue
if group_val not in groups.keys():
groups[group_val] = []
for tag in post['tags']:
if tag not in tags.keys():
tags[tag] = []
groups[group_val].append(post)
tags[tag].append(post)
for group_val, index_entries in groups.iter():
post = {
'index_entries': index_entries,
'title': group_val,
}
# TODO finish writing function, write page to disk
for tag, index_entries in tags.items():
output_file = output_dir.joinpath(tag, 'index.html')
output_file.parent.mkdir(exist_ok=True, parents=True)
output_file.write_text(JINJA_TEMPLATE_INDEX.render(
automatic_index=True,
search_bar=True,
title=tag,
index_entries = index_entries
))
output_file = output_dir.joinpath('index.html')
output_file.parent.mkdir(exist_ok=True, parents=True)
output_file.write_text(JINJA_TEMPLATE_INDEX.render(
automatic_index=True,
search_bar=True,
title='tags',
index_entries = [{ 'path': tag, 'title': tag, 'is_dir': False, } for tag in tags.keys()]
))
def main(args):
@ -244,9 +272,8 @@ def main(args):
root_properties = FILEMAP.get(root)
root_properties['dst_path']['raw'].mkdir(parents=True, exist_ok=True)
pprint.pprint(root_properties)
print(JINJA_TEMPLATE_DIRECTORY_INDEX)
html = JINJA_TEMPLATE_DIRECTORY_INDEX.render(**root_properties)
#pprint.pprint(root_properties)
html = JINJA_TEMPLATE_INDEX.render(**root_properties)
with open(root_properties['dst_path']['raw'].joinpath('index.html'), 'w+', encoding='utf-8') as file_pointer:
file_pointer.write(html)
@ -254,20 +281,18 @@ def main(args):
for file in files:
render_file(root.joinpath(file))
process_home_index(args.output_dir, search_data=FILEMAP.to_list())
process_home_index(args)
# copy styling and js scripts necessary for function
shutil.copytree(CSS_DIR, args.output_dir.joinpath('css'), dirs_exist_ok=True)
shutil.copytree(JS_DIR, args.output_dir.joinpath('js'), dirs_exist_ok=True)
generate_tag_browser(args.output_dir.joinpath('tags'))
return 0
# TODO implement useful logging and debug printing
# TODO build tag/metadata pages
if __name__ == '__main__':
try:

View File

@ -81,6 +81,19 @@ a file called `styles.css`.
To add additional styling, the default styling will attempt to import `styles.css` from the root of the notes
directory.
To add additional content to the homepage, create a file called `index.md` at the top level of your notes directory.
To set the HTML `title` tag, set `title` in the frontmatter of `index.md`:
```markdown
---
title: "alv's notes"
---
# alv's notes
these notes are probably wrong
```
## CLI Usage
```

View File

@ -40,7 +40,7 @@
<p class="smallText metadata"> tags: [
{% for tag in tags %}
<a href="/.tags/{{ tag }}.html">{{ tag }}</a>{% if loop.nextitem %},{% endif %}
<a href="/tags/{{ tag }}">{{ tag }}</a>{% if loop.nextitem %},{% endif %}
{% endfor %}
]</p>
<p class="smallText metadata">

View File

@ -3,13 +3,12 @@
<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="/css/styles.css" />
<title>{{ title }}</title>
<title>{{ post['content'] }}</title>
</head>
<body>
<div id="content">
<h1> {{ h1title }}</>
{{ post['content']|safe }}
<p>
These are my personal notes. Correctness is not guaranteed.
Browse <a href="/notes">here</a> or by tag <a href="/tags">here</a>.
</p>