@@ -24,6 +24,23 @@ class AnsibleDocsPluginConfig(mkdocs.config.base.Config):
2424class 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 """
0 commit comments