Skip to content

Commit

Permalink
closes #50
Browse files Browse the repository at this point in the history
  • Loading branch information
xrotwang committed Dec 1, 2023
1 parent 32eef8b commit 06dc5df
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [Unreleased]

- Fixed bug whereby CLDF examples were not properly HTML escaped when rendered as Markdown.
- Fixed bug whereby the --language-filters option was ignored in `cldfviz.map` when no
parameters were specified.
- Added Python 3.12 to supported version.
Expand Down
4 changes: 3 additions & 1 deletion src/cldfviz/commands/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def register(parser):
parser.add_argument('--templates', type=PathType(type='dir'), default=None)
parser.add_argument('--output', type=PathType(type='file', must_exist=False), default=None)
parser.add_argument('--download-dir', type=PathType(type='dir'), default=None)
parser.add_argument(
'--no-escape', help='Do not HTML escape content.', action='store_true', default=False)


def run(args):
Expand Down Expand Up @@ -83,7 +85,7 @@ def run(args):
text = media.read()
break

res = render(text, dss, args.templates)
res = render(text, dss, args.templates, escape=not args.no_escape)
if args.output:
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(res, encoding='utf8')
Expand Down
38 changes: 27 additions & 11 deletions src/cldfviz/text.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import re
import html
import typing
import pathlib
import functools

from pycldf import Dataset
from pycldf.ext.markdown import CLDFMarkdownText
Expand Down Expand Up @@ -39,7 +43,12 @@ def iter_templates():
yield p, doc, [v for v in vars if v != 'ctx']


def pad_ex(obj, gloss):
def pad_ex(obj: typing.Iterable[str],
gloss: typing.Iterable[str],
escape: typing.Optional[bool] = True):
"""
:param escape: Flag signaling whether to html.escape words and glosses.
"""
out_obj = []
out_gloss = []
for o, g in zip(obj, gloss):
Expand All @@ -49,12 +58,17 @@ def pad_ex(obj, gloss):
o += " "*-diff # noqa E225
else:
g += " " * diff
out_obj.append(o)
out_gloss.append(g)
out_obj.append(html.escape(o, quote=False) if escape else o)
out_gloss.append(html.escape(g, quote=False) if escape else g)
return " ".join(out_obj).strip(), " ".join(out_gloss).strip()


def render(doc, cldf_dict, template_dir=None, loader=None, func_dict=None):
def render(doc: typing.Union[pathlib.Path, str],
cldf_dict: typing.Union[Dataset, typing.Dict[typing.Union[str, None], Dataset]],
template_dir: typing.Optional[typing.Union[str, pathlib.Path]] = None,
loader: typing.Optional[jinja2.BaseLoader] = None,
func_dict: typing.Optional[typing.Dict[str, callable]] = None,
escape: typing.Optional[bool] = True) -> str:
"""
Render CLDF Markdown using customizable jinja2 templates.
Expand All @@ -63,15 +77,17 @@ def render(doc, cldf_dict, template_dir=None, loader=None, func_dict=None):
- Reference list: Include a list of cited references using the link \
`[](Source?cited_only#cldf:__all__)`
:param doc:
:param cldf_dict:
:param template_dir:
:param loader:
:param func_dict:
:return:
:param doc: A CLDF Markdown document specified as string or filepath.
:param cldf_dict: A CLDF dataset or a mapping of prefixes to CLDF datasets.
:param template_dir: Path to custom template directory.
:param loader: As alternative to a custom template directory, a custom jinja2 loader can be \
specified.
:param func_dict: Mapping of names to callables passed to templates as renderer globals, see \
https://jinja.palletsprojects.com/en/3.1.x/api/#jinja2.Environment.globals.
:return: Rendered document as string.
"""
func_dict = func_dict or {}
func_dict.update({"pad_ex": pad_ex})
func_dict.update({"pad_ex": functools.partial(pad_ex, escape=escape)})

if isinstance(cldf_dict, Dataset):
cldf_dict = {None: cldf_dict}
Expand Down
2 changes: 1 addition & 1 deletion tests/StructureDataset/examples.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
ID,Language_ID,Primary_Text,Analyzed_Word,Gloss,Translated_Text,Comment,Source
igt,Ho_NM,The text,The t-ext,The GL-OSS,the translation,See ptonly,Peterson2017
igt,Ho_NM,The text,The t-e<in>xt,The GL-<FOC>OSS,the translation,See ptonly,Peterson2017
ptonly,Ho_NM,Only primary text,,,and translation,,
4 changes: 4 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ def check_output(cmd, *args, **kw):
'StructureDataset',
'-l',
lambda out: 'CodeTable' in out),
(
'StructureDataset',
'--text-string "[ex](ExampleTable#cldf:igt)" --no-escape',
lambda out: '<in>' in out),
(
'Wordlist',
'--media-id 2',
Expand Down
12 changes: 10 additions & 2 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,16 @@ def test_render_example(StructureDataset):
> (igt) Ho (Peterson 2017)
<pre>
The t-ext
The GL-OSS
The t-e&lt;in&gt;xt
The GL-&lt;FOC&gt;OSS
‘the translation’</pre>
"""
assert render('[ex](ExampleTable#cldf:igt)', StructureDataset, escape=False) == """\
> (igt) Ho (Peterson 2017)
<pre>
The t-e<in>xt
The GL-<FOC>OSS
‘the translation’</pre>
"""
assert render('[ex](ExampleTable#cldf:ptonly)', StructureDataset) == """\
Expand Down

0 comments on commit 06dc5df

Please sign in to comment.