Skip to content

Commit

Permalink
Add context variable to avoid escaping lines starting with given values
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Sep 26, 2018
1 parent 0040028 commit 742e023
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/shellman/templates/data/wikipage.md
@@ -1,3 +1,4 @@
{% if do_not_escape_lines_that_start_with is not defined %}{% set do_not_escape_lines_that_start_with = None %}{% endif %}
{% if shellman.doc.brief %}
## {{ shellman.filename }}
{{ shellman.doc.brief[0].text|e }}
Expand All @@ -15,7 +16,7 @@
{% if shellman.doc.desc %}
## Description
{% for desc in shellman.doc.desc %}
{{ desc.text|e }}
{{ desc.text|escape(except_starts_with=do_not_escape_lines_that_start_with) }}
{% if not loop.last %}{{ "\n" }}{% endif %}
{% endfor %}

Expand Down
10 changes: 7 additions & 3 deletions src/shellman/templates/filters.py
Expand Up @@ -165,9 +165,13 @@ def do_groupby(environment, value, attribute, sort=True):
return [_GroupTuple(group, grouped[group]) for group in unique_groups]


def do_escapetextonly(value):
def do_escape(value, except_starts_with=None):
if except_starts_with is not None:
condition = lambda l: any(l.startswith(s) for s in except_starts_with)
else:
condition = False
return "\n".join(
line if line == "" or line[0] in " \t" else escape(line)
line if line == "" or condition(line) else escape(line)
for line in value.split("\n")
)

Expand All @@ -185,5 +189,5 @@ def do_escapetextonly(value):
"body": do_body,
"smartwrap": do_smartwrap,
"format": do_format,
"escapetextonly": do_escapetextonly,
"escape": do_escape,
}

0 comments on commit 742e023

Please sign in to comment.