Skip to content

Commit 95f147a

Browse files
authored
Make sphinx-init better configurable (#77)
* Make sphinx-init better configurable. * Fix template. * Linting. * Also support adding values to html_context and html_theme_options.
1 parent 60c4116 commit 95f147a

File tree

4 files changed

+89
-6
lines changed

4 files changed

+89
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- "Add new options ``--project``, ``--copyright``, ``--title``, ``--html-short-title``, ``--extra-conf``, ``--extra-html-context``, and ``--extra-html-theme-options`` to the ``sphinx-init`` subcommand to allow to customize the generated ``conf.py`` Sphinx configuration (https://github.com/ansible-community/antsibull-docs/pull/77)."

src/antsibull_docs/cli/antsibull_docs.py

+35
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,16 @@ def _normalize_sphinx_init_options(args: argparse.Namespace) -> None:
164164
raise InvalidArgumentError('The rst/index.rst replacement file,'
165165
f' {args.index_rst_source}, is not a file')
166166

167+
for key, option in [
168+
('extra_conf', '--extra-conf'),
169+
('extra_html_context', '--extra-html-context'),
170+
('extra_html_theme_options', '--extra-html-theme-options'),
171+
]:
172+
for entry in getattr(args, key) or []:
173+
if '=' not in entry:
174+
raise InvalidArgumentError(
175+
f'Every `{option}` value must have at least one equal sign (=).')
176+
167177

168178
def parse_args(program_name: str, args: List[str]) -> argparse.Namespace:
169179
"""
@@ -367,6 +377,31 @@ def parse_args(program_name: str, args: List[str]) -> argparse.Namespace:
367377
sphinx_init_parser.add_argument('--index-rst-source',
368378
help='Copy the provided file to rst/index.rst intead of'
369379
' templating a default one.')
380+
sphinx_init_parser.add_argument('--project', default='Ansible collections',
381+
help='Sets the "project" value in the Sphinx configuration.')
382+
sphinx_init_parser.add_argument('--copyright', default='Ansible contributors',
383+
help='Sets the "copyright" value in the Sphinx configuration.')
384+
sphinx_init_parser.add_argument('--title', default='Ansible Collections Documentation',
385+
help='Sets the "title" and "html_short_title" values in the'
386+
' Sphinx configuration. If --html-short-title is also'
387+
' specified, only "title" will be set to the value specified'
388+
' here.')
389+
sphinx_init_parser.add_argument('--html-short-title',
390+
help='Sets the "html_short_title" value in the Sphinx'
391+
' configuration. If not specified, the value of --title will'
392+
' be used.')
393+
sphinx_init_parser.add_argument('--extra-conf', action='append',
394+
help='Add additional configuration entries to the generated'
395+
' conf.py. Use the syntax `key=value` to add an entry'
396+
' `key = "value"`.')
397+
sphinx_init_parser.add_argument('--extra-html-context', action='append',
398+
help='Add additional configuration entries to the generated'
399+
' conf.py in `html_context`. Use the syntax `key=value` to add'
400+
' an entry `"key": "value",`.')
401+
sphinx_init_parser.add_argument('--extra-html-theme-options', action='append',
402+
help='Add additional configuration entries to the generated'
403+
' conf.py in `html_theme_options`. Use the syntax `key=value`'
404+
' to add an entry `"key": "value",`.')
370405

371406
#
372407
# Lint collection docs

src/antsibull_docs/cli/doc_commands/sphinx_init.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ def python_repr(value: t.Any) -> str:
7878
return repr(value)
7979

8080

81+
def split_kv(entries: t.Optional[t.List[str]]) -> t.List[t.Tuple[str, str]]:
82+
result: t.List[t.Tuple[str, str]] = []
83+
for entry in entries or []:
84+
key, value = entry.split('=', 1)
85+
result.append((key, value))
86+
return result
87+
88+
8189
def site_init() -> int:
8290
"""
8391
Initialize a Sphinx site template for a collection docsite.
@@ -109,7 +117,16 @@ def site_init() -> int:
109117
for intersphinx in app_ctx.extra['intersphinx'] or []:
110118
inventory, url = intersphinx.split(':', 1)
111119
intersphinx_parts.append((inventory.rstrip(' '), url.lstrip(' ')))
112-
index_rst_source = app_ctx.extra['index_rst_source']
120+
index_rst_source: t.Optional[str] = app_ctx.extra['index_rst_source']
121+
project: str = app_ctx.extra['project']
122+
conf_copyright: str = app_ctx.extra['copyright']
123+
title: str = app_ctx.extra['title']
124+
html_short_title: t.Optional[str] = app_ctx.extra['html_short_title']
125+
if html_short_title is None:
126+
html_short_title = title
127+
extra_conf = split_kv(app_ctx.extra['extra_conf'])
128+
extra_html_context = split_kv(app_ctx.extra['extra_html_context'])
129+
extra_html_theme_options = split_kv(app_ctx.extra['extra_html_theme_options'])
113130

114131
sphinx_theme = 'sphinx_ansible_theme'
115132
sphinx_theme_package = 'sphinx-ansible-theme >= 0.9.0'
@@ -148,6 +165,13 @@ def site_init() -> int:
148165
collection_url=collection_url,
149166
collection_install=collection_install,
150167
intersphinx=intersphinx_parts,
168+
project=project,
169+
conf_copyright=conf_copyright,
170+
title=title,
171+
html_short_title=html_short_title,
172+
extra_conf=extra_conf,
173+
extra_html_context=extra_html_context,
174+
extra_html_theme_options=extra_html_theme_options,
151175
) + '\n'
152176

153177
destination = os.path.join(dest_dir, filename)

src/antsibull_docs/data/sphinx_init/conf_py.j2

+27-5
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
# documentation:
77
# http://www.sphinx-doc.org/en/master/config
88

9-
project = 'Ansible collections'
10-
copyright = 'Ansible contributors'
9+
project = @{ project | python_repr }@
10+
copyright = @{ conf_copyright | python_repr }@
1111

12-
title = 'Ansible Collections Documentation'
13-
html_short_title = 'Ansible Collections Documentation'
12+
title = @{ title | python_repr }@
13+
html_short_title = @{ html_short_title | python_repr }@
1414

1515
extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx', 'sphinx_antsibull_ext']
1616

1717
pygments_style = 'ansible'
1818

1919
highlight_language = 'YAML+Jinja'
2020

21-
html_theme = '@{ sphinx_theme }@'
21+
html_theme = @{ sphinx_theme | python_repr }@
2222
html_show_sphinx = False
2323

2424
display_version = False
@@ -49,3 +49,25 @@ default_role = 'any'
4949
{% if not lenient %}
5050
nitpicky = True
5151
{% endif %}
52+
{% if extra_conf %}
53+
54+
{% for key, value in extra_conf %}
55+
@{ key }@ = @{ value | python_repr }@
56+
{% endfor %}
57+
{% endif %}
58+
{% if extra_html_context %}
59+
60+
html_context = {
61+
{% for key, value in extra_conf %}
62+
@{ key | python_repr }@: @{ value | python_repr }@,
63+
{% endfor %}
64+
}
65+
{% endif %}
66+
{% if extra_html_theme_options %}
67+
68+
html_theme_options = {
69+
{% for key, value in extra_conf %}
70+
@{ key | python_repr }@: @{ value | python_repr }@,
71+
{% endfor %}
72+
}
73+
{% endif %}

0 commit comments

Comments
 (0)