Skip to content

Commit 7f33a90

Browse files
authored
Prepare supporting multiple output formats. (#176)
1 parent 589968a commit 7f33a90

36 files changed

+226
-80
lines changed

src/antsibull_docs/cli/doc_commands/_build.py

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
load_ansible_config,
2929
)
3030
from ...extra_docs import load_collections_extra_docs
31+
from ...jinja2.environment import OutputFormat
3132
from ...process_docs import (
3233
get_callback_plugin_contents,
3334
get_collection_contents,
@@ -56,6 +57,8 @@ def generate_docs_for_all_collections(
5657
venv: VenvRunner | FakeVenvRunner,
5758
collection_dir: str | None,
5859
dest_dir: str,
60+
output_format: OutputFormat,
61+
*,
5962
collection_names: list[str] | None = None,
6063
create_indexes: bool = True,
6164
squash_hierarchy: bool = False,
@@ -72,6 +75,7 @@ def generate_docs_for_all_collections(
7275
If ``None``, the collections are assumed to be in the current
7376
search path for Ansible.
7477
:arg dest_dir: The directory into which the documentation is written.
78+
:arg output_format: The output format.
7579
:kwarg collection_names: Optional list of collection names. If specified, only documentation
7680
for these collections will be collected and generated.
7781
:kwarg create_indexes: Whether to create the collection, namespace, and plugin indexes. By
@@ -187,6 +191,7 @@ def generate_docs_for_all_collections(
187191
dest_dir,
188192
collection_url=collection_url,
189193
collection_install=collection_install,
194+
output_format=output_format,
190195
breadcrumbs=breadcrumbs,
191196
for_official_docsite=for_official_docsite,
192197
referable_envvars=referable_envvars,
@@ -199,6 +204,7 @@ def generate_docs_for_all_collections(
199204
dest_dir,
200205
collection_url=collection_url,
201206
collection_install=collection_install,
207+
output_format=output_format,
202208
breadcrumbs=breadcrumbs,
203209
for_official_docsite=for_official_docsite,
204210
referable_envvars=referable_envvars,
@@ -212,6 +218,7 @@ def generate_docs_for_all_collections(
212218
dest_dir,
213219
collection_url=collection_url,
214220
collection_install=collection_install,
221+
output_format=output_format,
215222
for_official_docsite=for_official_docsite,
216223
referable_envvars=referable_envvars,
217224
)
@@ -223,6 +230,7 @@ def generate_docs_for_all_collections(
223230
dest_dir,
224231
collection_url=collection_url,
225232
collection_install=collection_install,
233+
output_format=output_format,
226234
for_official_docsite=for_official_docsite,
227235
referable_envvars=referable_envvars,
228236
)
@@ -239,6 +247,7 @@ def generate_docs_for_all_collections(
239247
squash_hierarchy=squash_hierarchy,
240248
extra_docs_data=extra_docs_data,
241249
link_data=link_data,
250+
output_format=output_format,
242251
breadcrumbs=breadcrumbs,
243252
for_official_docsite=for_official_docsite,
244253
referable_envvars=referable_envvars,
@@ -254,6 +263,7 @@ def generate_docs_for_all_collections(
254263
collection_install=collection_install,
255264
collection_metadata=collection_metadata,
256265
link_data=link_data,
266+
output_format=output_format,
257267
squash_hierarchy=squash_hierarchy,
258268
for_official_docsite=for_official_docsite,
259269
referable_envvars=referable_envvars,
@@ -271,6 +281,7 @@ def generate_docs_for_all_collections(
271281
collection_install=collection_install,
272282
collection_metadata=collection_metadata,
273283
link_data=link_data,
284+
output_format=output_format,
274285
squash_hierarchy=squash_hierarchy,
275286
use_html_blobs=use_html_blobs,
276287
for_official_docsite=for_official_docsite,
@@ -288,6 +299,7 @@ def generate_docs_for_all_collections(
288299
output_environment_variables(
289300
dest_dir,
290301
referenced_env_vars,
302+
output_format=output_format,
291303
squash_hierarchy=squash_hierarchy,
292304
referable_envvars=referable_envvars,
293305
)

src/antsibull_docs/cli/doc_commands/collection.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from antsibull_core.venv import FakeVenvRunner
2222

2323
from ... import app_context
24+
from ...jinja2.environment import OutputFormat
2425
from ._build import generate_docs_for_all_collections
2526

2627
mlog = log.fields(mod=__name__)
@@ -38,7 +39,8 @@ def generate_collection_docs(collection_dir: str | None, squash_hierarchy: bool)
3839
venv,
3940
collection_dir,
4041
app_ctx.extra["dest_dir"],
41-
app_ctx.extra["collections"],
42+
OutputFormat.ANSIBLE_DOCSITE,
43+
collection_names=app_ctx.extra["collections"],
4244
create_indexes=app_ctx.indexes and not squash_hierarchy,
4345
squash_hierarchy=squash_hierarchy,
4446
breadcrumbs=app_ctx.breadcrumbs,

src/antsibull_docs/cli/doc_commands/current.py

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from antsibull_core.venv import FakeVenvRunner
1212

1313
from ... import app_context
14+
from ...jinja2.environment import OutputFormat
1415
from ._build import generate_docs_for_all_collections
1516

1617
mlog = log.fields(mod=__name__)
@@ -37,6 +38,7 @@ def generate_docs() -> int:
3738
venv,
3839
app_ctx.extra["collection_dir"],
3940
app_ctx.extra["dest_dir"],
41+
OutputFormat.ANSIBLE_DOCSITE,
4042
breadcrumbs=app_ctx.breadcrumbs,
4143
use_html_blobs=app_ctx.use_html_blobs,
4244
fail_on_error=app_ctx.extra["fail_on_error"],

src/antsibull_docs/cli/doc_commands/devel.py

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from antsibull_core.venv import FakeVenvRunner, VenvRunner
2323

2424
from ... import app_context
25+
from ...jinja2.environment import OutputFormat
2526
from ._build import generate_docs_for_all_collections
2627

2728
mlog = log.fields(mod=__name__)
@@ -173,6 +174,7 @@ def generate_docs() -> int:
173174
venv,
174175
collection_dir,
175176
app_ctx.extra["dest_dir"],
177+
OutputFormat.ANSIBLE_DOCSITE,
176178
breadcrumbs=app_ctx.breadcrumbs,
177179
use_html_blobs=app_ctx.use_html_blobs,
178180
fail_on_error=app_ctx.extra["fail_on_error"],

src/antsibull_docs/cli/doc_commands/plugin.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from ...collection_links import CollectionLinks
2727
from ...docs_parsing import AnsibleCollectionMetadata
2828
from ...docs_parsing.fqcn import get_fqcn_parts, is_fqcn
29-
from ...jinja2.environment import doc_environment
29+
from ...jinja2.environment import OutputFormat, doc_environment
3030
from ...process_docs import normalize_plugin_info
3131
from ...utils.collection_name_transformer import CollectionNameTransformer
3232
from ...write_docs.plugins import write_plugin_rst
@@ -40,6 +40,7 @@ def generate_plugin_docs(
4040
collection_name: str,
4141
plugin: str,
4242
output_path: str,
43+
output_format: OutputFormat,
4344
) -> int:
4445
"""
4546
Render documentation for a locally installed plugin.
@@ -114,9 +115,9 @@ def generate_plugin_docs(
114115
"ansible-galaxy collection install {namespace}.{name}",
115116
)
116117
env = doc_environment(
117-
("antsibull_docs.data", "docsite"),
118118
collection_url=collection_url,
119119
collection_install=collection_install,
120+
output_format=output_format,
120121
)
121122
# Get the templates
122123
plugin_tmpl = env.get_template("plugin.rst.j2")
@@ -179,5 +180,10 @@ def generate_docs() -> int:
179180
plugin_name = ".".join([namespace, collection, plugin])
180181

181182
return generate_plugin_docs(
182-
plugin_type, plugin_name, collection_name, plugin, output_path
183+
plugin_type,
184+
plugin_name,
185+
collection_name,
186+
plugin,
187+
output_path,
188+
OutputFormat.ANSIBLE_DOCSITE,
183189
)

src/antsibull_docs/cli/doc_commands/stable.py

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from antsibull_core.venv import FakeVenvRunner, VenvRunner
2424

2525
from ... import app_context
26+
from ...jinja2.environment import OutputFormat
2627
from ._build import generate_docs_for_all_collections
2728

2829
mlog = log.fields(mod=__name__)
@@ -175,6 +176,7 @@ def generate_docs() -> int:
175176
venv,
176177
collection_dir,
177178
app_ctx.extra["dest_dir"],
179+
OutputFormat.ANSIBLE_DOCSITE,
178180
breadcrumbs=app_ctx.breadcrumbs,
179181
use_html_blobs=app_ctx.use_html_blobs,
180182
fail_on_error=app_ctx.extra["fail_on_error"],

src/antsibull_docs/jinja2/__init__.py

+32
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,35 @@
22
# https://www.gnu.org/licenses/gpl-3.0.txt)
33
# SPDX-License-Identifier: GPL-3.0-or-later
44
# SPDX-FileCopyrightText: 2020, Ansible Project
5+
"""
6+
Output format enum.
7+
"""
8+
9+
from __future__ import annotations
10+
11+
from enum import Enum
12+
13+
14+
class OutputFormatObj:
15+
def __init__(self, value: str, extension: str):
16+
self._value_ = value
17+
self._value = value
18+
self._extension = extension
19+
20+
@property
21+
def value(self):
22+
return self._value
23+
24+
@property
25+
def extension(self):
26+
return self._extension
27+
28+
def __str__(self):
29+
return self._value
30+
31+
32+
class OutputFormat(OutputFormatObj, Enum):
33+
def __new__(cls, value: str, extension: str):
34+
return OutputFormatObj(value, extension)
35+
36+
ANSIBLE_DOCSITE = ("ansible-docsite", ".rst.j2")

src/antsibull_docs/jinja2/environment.py

+45-15
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,17 @@
1515

1616
from ..markup.rstify import rst_code, rst_escape
1717
from ..utils.collection_name_transformer import CollectionNameTransformer
18+
from . import OutputFormat
1819
from .filters import (
1920
do_max,
2021
documented_type,
2122
extract_options_from_list,
2223
html_ify,
24+
make_rst_ify,
2325
massage_author_name,
2426
move_first,
2527
remove_options_from_list,
2628
rst_fmt,
27-
rst_ify,
2829
rst_xline,
2930
to_ini_value,
3031
to_json,
@@ -53,27 +54,56 @@ def reference_plugin_rst(plugin_name: str, plugin_type: str) -> str:
5354
return f"\\ :ref:`{rst_escape(fqcn)} <ansible_collections.{fqcn}_{plugin_type}>`\\ "
5455

5556

57+
def get_template_location(output_format: OutputFormat) -> tuple[str, str]:
58+
"""
59+
Return template location given the output format.
60+
"""
61+
return ("antsibull_docs.data", f"docsite/{output_format.value}")
62+
63+
64+
def get_template_filename(filename_base: str, output_format: OutputFormat) -> str:
65+
"""
66+
Return template filename given the filename base and the output format.
67+
"""
68+
return f"{filename_base}{output_format.extension}"
69+
70+
71+
def _get_loader(
72+
template_location: str | tuple[str, str] | None, output_format: OutputFormat | None
73+
) -> BaseLoader:
74+
if template_location is None:
75+
if output_format is None:
76+
raise ValueError(
77+
"Either template_location or output_format must be provided"
78+
)
79+
template_location = get_template_location(output_format)
80+
81+
if isinstance(template_location, str) and os.path.exists(template_location):
82+
return FileSystemLoader(template_location)
83+
84+
if isinstance(template_location, str):
85+
template_pkg = template_location
86+
template_path = "templates"
87+
else:
88+
template_pkg = template_location[0]
89+
template_path = template_location[1]
90+
91+
return PackageLoader(template_pkg, template_path)
92+
93+
5694
def doc_environment(
57-
template_location: str | tuple[str, str],
95+
template_location: str | tuple[str, str] | None = None,
5896
*,
5997
extra_filters: Mapping[str, t.Callable] | None = None,
6098
extra_tests: Mapping[str, t.Callable] | None = None,
6199
collection_url: CollectionNameTransformer | None = None,
62100
collection_install: CollectionNameTransformer | None = None,
63101
referable_envvars: set[str] | None = None,
102+
output_format: OutputFormat | None = None,
64103
) -> Environment:
65-
loader: BaseLoader
66-
if isinstance(template_location, str) and os.path.exists(template_location):
67-
loader = FileSystemLoader(template_location)
68-
else:
69-
if isinstance(template_location, str):
70-
template_pkg = template_location
71-
template_path = "templates"
72-
else:
73-
template_pkg = template_location[0]
74-
template_path = template_location[1]
75-
76-
loader = PackageLoader(template_pkg, template_path)
104+
loader = _get_loader(template_location, output_format)
105+
if output_format is None:
106+
output_format = OutputFormat.ANSIBLE_DOCSITE
77107

78108
env = Environment(
79109
loader=loader,
@@ -97,7 +127,7 @@ def doc_environment(
97127

98128
env.globals["reference_plugin_rst"] = reference_plugin_rst
99129
env.globals["referable_envvars"] = referable_envvars
100-
env.filters["rst_ify"] = rst_ify
130+
env.filters["rst_ify"] = make_rst_ify(output_format)
101131
env.filters["html_ify"] = html_ify
102132
env.filters["fmt"] = rst_fmt
103133
env.filters["rst_code"] = rst_code

0 commit comments

Comments
 (0)