Skip to content

Commit a76007a

Browse files
committed
Refactor page generation and add complete nav structure
1 parent 6e8b0a9 commit a76007a

File tree

5 files changed

+94
-37
lines changed

5 files changed

+94
-37
lines changed

mkdocs_ansible_collection/plugin.py

Lines changed: 77 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ class AnsibleDocsPluginConfig(mkdocs.config.base.Config):
2424
class AnsibleDocsPlugin(mkdocs.plugins.BasePlugin[AnsibleDocsPluginConfig]):
2525
"""MkDocs Plugin Class."""
2626

27+
# TODO: Remove once all plugin types have a corresponding jinja template
28+
PLUGIN_MAP = {"filter": "filter"}
29+
30+
def __init__(self, *args, **kwargs):
31+
"""Instantiation."""
32+
super().__init__(*args, **kwargs)
33+
34+
# Load templates from package and initialize Jinja environment
35+
log.debug(
36+
f"Jinja templates path {package_files('mkdocs_ansible_collection') / 'templates'}"
37+
)
38+
self.jinja_env = Environment(
39+
loader=FileSystemLoader(package_files("mkdocs_ansible_collection") / "templates"),
40+
autoescape=select_autoescape(default=True),
41+
trim_blocks=True,
42+
)
43+
2744
def on_pre_build(self, config):
2845
"""
2946
Event handler for the pre_build stage.
@@ -53,52 +70,58 @@ def on_files(self, files, config):
5370
See:
5471
https://www.mkdocs.org/dev-guide/plugins/#events
5572
"""
56-
# Load templates from package and initialize Jinja environment
57-
log.debug(
58-
f"Jinja templates path {package_files('mkdocs_ansible_collection') / 'templates'}"
59-
)
60-
jinja_env = Environment(
61-
loader=FileSystemLoader(package_files("mkdocs_ansible_collection") / "templates"),
62-
autoescape=select_autoescape(default=True),
63-
trim_blocks=True,
64-
)
65-
6673
for fqcn in self.config.collections:
6774
# Get collection metadata by running ansible-doc
6875
collection_metadata = AnsibleDocsPlugin._get_ansible_doc_metadata(fqcn)
6976

7077
# Generate the index for the collection sub-path
71-
nf = mkdocs.structure.files.File(
72-
f"{fqcn}/index.md", src_dir=None, dest_dir=config.site_dir, use_directory_urls=False
73-
)
74-
nf.generated_by = "ansible-docs"
75-
76-
jinja_template = jinja_env.get_template("collection_index.md.jinja")
77-
nf.content_string = jinja_template.render(
78-
plugin_types=collection_metadata["all"], fqcn=fqcn
78+
files.append(
79+
self._generate_page(
80+
path=f"{fqcn}/index.md",
81+
site_dir=config.site_dir,
82+
template="collection_index.md.jinja",
83+
fqcn=fqcn,
84+
plugin_types=collection_metadata["all"],
85+
)
7986
)
80-
81-
files.append(nf)
8287
collection_nav = {f"{fqcn}": [f"{fqcn}/index.md"]}
8388

8489
for plugin_type in collection_metadata["all"]:
8590
plugins = collection_metadata["all"][plugin_type]
8691
if len(plugins) == 0:
8792
continue
88-
89-
nf = mkdocs.structure.files.File(
90-
f"{fqcn}/{plugin_type}.md",
91-
src_dir=None,
92-
dest_dir=config.site_dir,
93-
use_directory_urls=False,
93+
sub_nav = {f"{plugin_type}": [f"{fqcn}/{plugin_type}/index.md"]}
94+
95+
files.append(
96+
self._generate_page(
97+
path=f"{fqcn}/{plugin_type}/index.md",
98+
site_dir=config.site_dir,
99+
template="plugin_list.md.jinja",
100+
fqcn=fqcn,
101+
plugin_type=plugin_type,
102+
plugins=plugins,
103+
)
94104
)
95-
nf.generated_by = "ansible-docs"
96105

97-
jinja_template = jinja_env.get_template("plugin_list.md.jinja")
98-
nf.content_string = jinja_template.render(plugin_type=plugin_type, plugins=plugins)
99-
100-
files.append(nf)
101-
collection_nav[fqcn].append({f"{plugin_type}": f"{fqcn}/{plugin_type}.md"})
106+
for plugin in plugins:
107+
plugin_name = plugin.removeprefix(fqcn + ".")
108+
files.append(
109+
self._generate_page(
110+
path=f"{fqcn}/{plugin_type}/{plugin_name}.md",
111+
site_dir=config.site_dir,
112+
# TODO: replace line once the mapping is not needed
113+
# template=f"{plugin_type}.md.jinja",
114+
template=f"{AnsibleDocsPlugin.PLUGIN_MAP.get(plugin_type, 'default')}.md.jinja", # noqa
115+
plugin=plugin,
116+
plugin_data=plugins[plugin],
117+
)
118+
)
119+
120+
sub_nav[plugin_type].append(
121+
{f"{plugin_name}": f"{fqcn}/{plugin_type}/{plugin_name}.md"}
122+
)
123+
124+
collection_nav[fqcn].append(sub_nav)
102125

103126
config.nav.append(collection_nav)
104127

@@ -123,6 +146,28 @@ def on_nav(self, nav, config, files):
123146
log.debug(f"config.nav = {config.nav}")
124147
# breakpoint()
125148

149+
def _generate_page(self, path, site_dir, template, **kwargs):
150+
"""Generates a new file in memory from a Jinja template.
151+
152+
Args:
153+
path (str): relative path from the docs root with filename.md
154+
site_dir (str): project config.site_dir
155+
template (str): name of jinja template to use for rendering
156+
kwargs (dict): data to pass to jinja.render
157+
158+
Returns:
159+
mkdocs.structure.files.File: file object with generated content
160+
"""
161+
nf = mkdocs.structure.files.File(
162+
path, src_dir=None, dest_dir=site_dir, use_directory_urls=False
163+
)
164+
nf.generated_by = "ansible-collection"
165+
166+
jinja_template = self.jinja_env.get_template(template)
167+
nf.content_string = jinja_template.render(**kwargs)
168+
169+
return nf
170+
126171
@staticmethod
127172
def _get_ansible_doc_metadata(fqcn):
128173
"""

mkdocs_ansible_collection/templates/collection_index.md.jinja

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
This collection contains the following plugin types:
44

5+
| Plugin Type | Count |
6+
| ----------- | ----- |
57
{% for plugin_type in plugin_types %}
68
{% if plugin_types[plugin_type] | length > 0 %}
7-
- [{{ plugin_type }}](./{{ plugin_type }}.md)
9+
| [{{ plugin_type }}](./{{ plugin_type }}/index.md) | {{ plugin_types[plugin_type] | length }}
810
{% endif %}
911
{% endfor %}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# {{ plugin }}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# {{ plugin }}
2+
3+
## Synopsis
4+
5+
{% for line in plugin_data['doc']['description'] %}
6+
- {{ line }}
7+
{% endfor %}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
# {{plugin_type}} list
1+
# {{ plugin_type }} list
22

3-
This is the list of {{plugin_type}} plugins:
3+
This is the list of {{ plugin_type }} plugins:
44

5-
{% for plugin in plugins %}
6-
- {{ plugin }}
5+
| Plugin | Author |
6+
| ------ | ------ |
7+
{% for plugin, plugin_data in plugins.items() %}
8+
| {{ plugin }} | {{ plugin_data["doc"] | default("-") }}
79
{% endfor %}

0 commit comments

Comments
 (0)