From 8949061c8694679257d1694e8a44a5eb771853c2 Mon Sep 17 00:00:00 2001 From: Mike Fiedler Date: Tue, 10 Oct 2023 09:20:26 -0400 Subject: [PATCH] chore: strip out whitespace added by jinja blocks (#14709) --- tests/unit/test_config.py | 4 +++- warehouse/config.py | 4 ++++ warehouse/templates/robots.txt | 7 +++---- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index 7c4acff56fde..5d206c14258d 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -402,6 +402,8 @@ def __init__(self): assert configurator_obj.add_settings.calls == [ pretend.call({"jinja2.newstyle": True}), pretend.call({"jinja2.i18n.domain": "messages"}), + pretend.call({"jinja2.lstrip_blocks": True}), + pretend.call({"jinja2.trim_blocks": True}), pretend.call({"retry.attempts": 3}), pretend.call( { @@ -413,7 +415,7 @@ def __init__(self): ), pretend.call({"http": {"verify": "/etc/ssl/certs/"}}), ] - add_settings_dict = configurator_obj.add_settings.calls[3].args[0] + add_settings_dict = configurator_obj.add_settings.calls[5].args[0] assert add_settings_dict["tm.manager_hook"](pretend.stub()) is transaction_manager assert configurator_obj.add_tween.calls == [ pretend.call("warehouse.config.require_https_tween_factory"), diff --git a/warehouse/config.py b/warehouse/config.py index a9548430ad6a..63e3915ae63f 100644 --- a/warehouse/config.py +++ b/warehouse/config.py @@ -482,6 +482,10 @@ def configure(settings=None): # Our translation strings are all in the "messages" domain config.add_settings({"jinja2.i18n.domain": "messages"}) + # Trim the Jinja blocks from the output, it's extra whitespace. + config.add_settings({"jinja2.lstrip_blocks": True}) + config.add_settings({"jinja2.trim_blocks": True}) + # We also want to use Jinja2 for .html templates as well, because we just # assume that all templates will be using Jinja. config.add_jinja2_renderer(".html") diff --git a/warehouse/templates/robots.txt b/warehouse/templates/robots.txt index ab0404022123..49ebe32ea223 100644 --- a/warehouse/templates/robots.txt +++ b/warehouse/templates/robots.txt @@ -1,9 +1,9 @@ Sitemap: {{ request.route_url("index.sitemap.xml") }} User-agent: * -{%- if request.registry.settings.get("warehouse.domain") != "pypi.org" %} +{% if request.registry.settings.get("warehouse.domain") != "pypi.org" %} Disallow: / -{%- else %} +{% else %} Disallow: /simple/ Disallow: /packages/ Disallow: /_includes/ @@ -11,5 +11,4 @@ Disallow: /pypi/*/json Disallow: /pypi/*/*/json Disallow: /pypi*? Disallow: /search* -{%- endif %} - +{% endif %}