Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions pygeometa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
from xml.dom import minidom

import click
from jinja2 import Environment, FileSystemLoader
from jinja2 import Environment, FileSystemLoader, select_autoescape
from jinja2.exceptions import TemplateNotFound
from jsonschema import validate as jsonschema_validate
from jsonschema.exceptions import ValidationError
Expand Down Expand Up @@ -435,7 +435,14 @@ def render_j2_template(mcf: dict, template_dir: str = None) -> str:
raise RuntimeError(msg)

LOGGER.debug(f'Setting up template environment {template_dir}')
env = Environment(loader=FileSystemLoader([template_dir, SCHEMAS]))
env = Environment(
loader=FileSystemLoader([template_dir, SCHEMAS]),
autoescape=select_autoescape(
enabled_extensions=('xml', 'html'),
default_for_string=True,
default=True
)
)

LOGGER.debug('Adding template filters')
env.filters['normalize_datestring'] = normalize_datestring
Expand Down
8 changes: 8 additions & 0 deletions tests/run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ def test_render_j2_template(self):
s_os = SampleOutputSchema()
_ = s_os.write(read_mcf(get_abspath(mcf_path)))

# MCF with bare '&' character
xml = render_j2_template(
read_mcf(get_abspath('sample-xml-escape.mcf.yml')),
iso_os.template_dir)
self.assertIsInstance(xml, str, 'Expected unicode string')
self.assertNotIn('in English & ', xml, 'Did not expect bare "&"')
self.assertIn('in English &', xml, 'Expected escaped "&"')

def itest_nested_mcf(self):
"""test nested mcf support"""

Expand Down
Loading