Skip to content

Commit 70fe479

Browse files
Add a configuration value to indent the output (aka prettify output) (#112)
Co-authored-by: Jared Dillard <[email protected]>
1 parent 96d0936 commit 70fe479

File tree

5 files changed

+60
-1
lines changed

5 files changed

+60
-1
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
Changelog
44
=========
55

6+
2.9.0
7+
-----
8+
9+
- |:sparkles:| NEW: Add :confval:`sitemap_indent` configuration value to control XML indentation
10+
`#112 <https://github.com/jdillard/sphinx-sitemap/pull/112>`_
11+
612
2.8.0
713
-----
814

docs/source/advanced-configuration.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,20 @@ This produces sitemap entries like:
180180

181181
.. _sitemapindex.xml: https://support.google.com/webmasters/answer/75712?hl=en
182182
.. _sitemaps.org: https://www.sitemaps.org/protocol.html
183+
184+
.. _configuration_indent:
185+
186+
Formatting XML Output
187+
^^^^^^^^^^^^^^^^^^^^^
188+
189+
To add indention to the XML output, set :confval:`sitemap_indent` to the number of spaces for indentation in **conf.py**:
190+
191+
.. code-block:: python
192+
193+
sitemap_indent = 2
194+
195+
Set to ``0`` (the default) to disable indentation:
196+
197+
.. code-block:: python
198+
199+
sitemap_indent = 0

docs/source/configuration-values.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,12 @@ A list of of possible configuration values to configure in **conf.py**:
5151
See :ref:`configuration_lastmod` for more information.
5252

5353
.. versionadded:: 2.7.0
54+
55+
.. confval:: sitemap_indent
56+
57+
- **Type**: integer
58+
- **Default**: ``0``
59+
- **Description**: Number of spaces to use for indentation in the sitemap XML output.
60+
See :ref:`configuration_indent` for more information.
61+
62+
.. versionadded:: 2.9.0

sphinx_sitemap/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from sphinx.errors import ExtensionError
2525
from sphinx.util.logging import getLogger
2626

27-
__version__ = "2.8.0"
27+
__version__ = "2.9.0"
2828

2929
logger = getLogger(__name__)
3030

@@ -49,6 +49,8 @@ def setup(app: Sphinx) -> Dict[str, Any]:
4949

5050
app.add_config_value("sitemap_show_lastmod", default=False, rebuild="")
5151

52+
app.add_config_value("sitemap_indent", default=0, rebuild="")
53+
5254
try:
5355
app.add_config_value("html_baseurl", default=None, rebuild="")
5456
except BaseException:
@@ -258,6 +260,9 @@ def create_sitemap(app: Sphinx, exception):
258260
)
259261

260262
filename = Path(app.outdir) / app.config.sitemap_filename
263+
if isinstance(app.config.sitemap_indent, int) and app.config.sitemap_indent > 0:
264+
ElementTree.indent(root, space=app.config.sitemap_indent * " ")
265+
261266
ElementTree.ElementTree(root).write(
262267
filename, xml_declaration=True, encoding="utf-8", method="xml"
263268
)

tests/test_simple.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,25 @@ def test_pattern_excludes(app, status, warning):
215215
"search",
216216
]
217217
}
218+
219+
220+
@pytest.mark.sphinx(
221+
"html",
222+
freshenv=True,
223+
confoverrides={
224+
"html_baseurl": "https://example.org/docs/",
225+
"language": "en",
226+
"sitemap_indent": 2,
227+
},
228+
)
229+
def test_indent(app, status, warning):
230+
"""Tests that xml output is indented"""
231+
app.warningiserror = True
232+
app.build()
233+
assert "sitemap.xml" in os.listdir(app.outdir)
234+
with open(app.outdir / "sitemap.xml", "r") as fd:
235+
lines = fd.readlines()
236+
237+
assert lines[0][0] == "<"
238+
assert lines[1][0] == "<"
239+
assert lines[2][0:3] == " <"

0 commit comments

Comments
 (0)