Dynamically create tag pages
This commit is contained in:
parent
c8eb405165
commit
b04042a839
7 changed files with 78 additions and 0 deletions
|
@ -1 +1,6 @@
|
||||||
|
---
|
||||||
|
tags:
|
||||||
|
- tag1
|
||||||
|
- tag2
|
||||||
|
---
|
||||||
# Nested
|
# Nested
|
||||||
|
|
1
content/tags/tag1.md
Normal file
1
content/tags/tag1.md
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Tag 1 content
|
54
hooks/tags.py
Normal file
54
hooks/tags.py
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
from mkdocs.utils import meta
|
||||||
|
from mkdocs.structure.files import File, Files
|
||||||
|
import tempfile
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
TAGS_TEMP_DIR = os.path.join(tempfile.gettempdir(), "tags")
|
||||||
|
|
||||||
|
shutil.rmtree(TAGS_TEMP_DIR, ignore_errors=True)
|
||||||
|
os.mkdir(TAGS_TEMP_DIR)
|
||||||
|
|
||||||
|
def on_files(files: Files, config):
|
||||||
|
tags = set()
|
||||||
|
|
||||||
|
template = config["theme"].get_env().get_template("tag-template.md")
|
||||||
|
list_template = config["theme"].get_env().get_template("tag-list-template.md")
|
||||||
|
|
||||||
|
for file in files.documentation_pages():
|
||||||
|
with open(file.abs_src_path) as f:
|
||||||
|
_, metadata = meta.get_data(f.read())
|
||||||
|
tags.update(metadata.get("tags", []))
|
||||||
|
|
||||||
|
for tag in tags:
|
||||||
|
found_tag_page = files.get_file_from_path(f"tags/{tag}.md")
|
||||||
|
|
||||||
|
if found_tag_page:
|
||||||
|
with open(found_tag_page.abs_src_path) as f:
|
||||||
|
content, _ = meta.get_data(f.read())
|
||||||
|
else:
|
||||||
|
content = ""
|
||||||
|
|
||||||
|
|
||||||
|
filename = f"{tag}.md"
|
||||||
|
with open(os.path.join(TAGS_TEMP_DIR, filename), "w") as f:
|
||||||
|
f.write(template.render(tag=tag, content=content))
|
||||||
|
|
||||||
|
files.append(File(
|
||||||
|
path=filename,
|
||||||
|
src_dir=TAGS_TEMP_DIR,
|
||||||
|
dest_dir=os.path.join(config["site_dir"], "tags"),
|
||||||
|
use_directory_urls=True
|
||||||
|
))
|
||||||
|
|
||||||
|
with open(os.path.join(TAGS_TEMP_DIR, "tags.md"), "w") as f:
|
||||||
|
f.write(list_template.render())
|
||||||
|
|
||||||
|
files.append(File(
|
||||||
|
path="tags.md",
|
||||||
|
src_dir=TAGS_TEMP_DIR,
|
||||||
|
dest_dir=config["site_dir"],
|
||||||
|
use_directory_urls=True
|
||||||
|
))
|
||||||
|
|
||||||
|
return files
|
|
@ -8,3 +8,4 @@ theme:
|
||||||
|
|
||||||
hooks:
|
hooks:
|
||||||
- hooks/nav.py
|
- hooks/nav.py
|
||||||
|
- hooks/tags.py
|
||||||
|
|
6
theme/tag-list-template.md
Normal file
6
theme/tag-list-template.md
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
template: tag.html
|
||||||
|
title: All tags
|
||||||
|
---
|
||||||
|
|
||||||
|
All the tags
|
6
theme/tag-template.md
Normal file
6
theme/tag-template.md
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
---
|
||||||
|
template: tag.html
|
||||||
|
title: {{ tag }}
|
||||||
|
---
|
||||||
|
|
||||||
|
{{ content }}
|
5
theme/tag.html
Normal file
5
theme/tag.html
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
I'm a tag
|
||||||
|
|
||||||
|
{{ page.title }}
|
||||||
|
|
||||||
|
{{ page.content }}
|
Loading…
Reference in a new issue