Skip to content

Commit

Permalink
Use Jinja2 indent filter instead of custom code
Browse files Browse the repository at this point in the history
* Use Jinja2 filter block

* Delete indent helper function

* Update comments and whitespace
  • Loading branch information
pushfoo committed May 13, 2024
1 parent 115f626 commit 7f1b878
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 21 deletions.
18 changes: 2 additions & 16 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,30 +507,16 @@ def _append_return(s: str) -> str:
print()


def prefix_nonblank_lines(raw_source: str, prefix: str = ' ') -> str:
"""Prefix non-blank lines for use in a .. code-block:: directive."""

# This approach is so clean it's worth the overhead for now
in_stream = StringIO(raw_source)
out_stream = StringIO()

for line in in_stream.readlines():
if not (len(line) == 0 or line.isspace()):
out_stream.write(prefix)
out_stream.write(line)

return out_stream.getvalue()


# -- Begin HTML output configuration and writing --

# This might be a mandatory name for access from extensions? Setting
# custom values didn't seem to work well with the custom jinja_my_rst
# plugin.
html_context = dict(
substitution_rules=prefix_nonblank_lines(substitution_rules)
substitution_rules=substitution_rules
)


def setup(app):
# app.add_config_value('substitution_rules', substitution_rules, 'env')
# app.add_config_value('substitution_rules', substitution_rules, 'env')
Expand Down
9 changes: 4 additions & 5 deletions docs/contributing/substitutions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,13 @@ good idea.

.. tip:: Be sure to read the comment at the top!

.. # The double curly braces below are a Jinja templating marker.
.. # We leave the Jinja substitution unindented because:
.. # 1. The first line is already indented by the preprocessor
.. # 2. Leaving it this way makes the preprocessing logic simpler
.. # The curly braces below are Jinja2 templating.
.. code-block:: rst
{{ substitution_rules }}
{% filter indent(width=3) %}
{{ substitution_rules }}
{% endfilter %}
Adding New Substitutions
Expand Down

0 comments on commit 7f1b878

Please sign in to comment.