Skip to content

Commit

Permalink
use markdown support from clldutils when available
Browse files Browse the repository at this point in the history
  • Loading branch information
xrotwang committed Jul 4, 2024
1 parent 412def5 commit c66ad29
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changes


## [Unreleased]

- Use markdown support from `clldutils.source` if available.


## [v1.2.0] - 2024-07-03

- Support more flexible citation labels in `cldfviz.text`.
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ package_dir =
= src
python_requires = >=3.8
install_requires =
packaging
clldutils>=3.17
pycldf>=1.32.0
cldfbench>=1.11.0
Expand Down
6 changes: 2 additions & 4 deletions src/cldfviz/templates/text/util.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,8 @@
{%- endmacro %}

{% macro source(src, with_anchor=False, with_link=False) -%}
{% if with_anchor %}<a id="source-{{ src.id }}"> </a>{% endif %}{{ src.text() }}{% if with_link %}
{% if src.get("doi") %}
[DOI: {{ src["doi"] }}](https://doi.org/{{ src["doi"] }}){% else %}{% if src.get("url") %}
{% if with_anchor %}<a id="source-{{ src.id }}"> </a>{% endif %}{{ source_markdown(src, with_link) }}{% if with_link %}
{% if src.get("url") %}
[{{ src["url"] }}]({{ src["url"] }}){% endif %}
{% endif %}
{% endif %}
{%- endmacro %}
11 changes: 10 additions & 1 deletion src/cldfviz/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from pycldf.ext.markdown import CLDFMarkdownText
import jinja2
import jinja2.meta
from packaging.version import Version
import clldutils
from clldutils.misc import nfilter
from clldutils.markup import MarkdownLink, MarkdownImageLink

Expand All @@ -18,6 +20,11 @@
TEMPLATE_DIR = cldfviz.PKG_DIR.joinpath('templates', 'text')


def source_markdown(src, with_link=False):
with_link = with_link and Version(clldutils.__version__) >= Version('3.21.0')
return src.text(**{'markdown': True} if with_link else {})


def _add_filters(env):
def paragraphs(s):
return '\n\n'.join(s.split('\n'))
Expand Down Expand Up @@ -87,7 +94,9 @@ def render(doc: typing.Union[pathlib.Path, str],
:return: Rendered document as string.
"""
func_dict = func_dict or {}
func_dict.update({"pad_ex": functools.partial(pad_ex, escape=escape)})
func_dict.update({
"pad_ex": functools.partial(pad_ex, escape=escape),
"source_markdown": source_markdown})

if isinstance(cldf_dict, Dataset):
cldf_dict = {None: cldf_dict}
Expand Down
6 changes: 3 additions & 3 deletions tests/test_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def test_render_metadata(StructureDataset):
('ParameterTable', None, 'C', 'sd', 'Codes'),
('Source', None, 'Peterson2017', 'sd', 'Peterson, John. 2017.'),
('Source', None, '__all__', 'sd', '- '),
('Source', 'with_link', 'Peterson2017', 'sd', '[DOI:'),
('Source', 'with_link', 'Peterson2017', 'sd', 'doi: ['),
]
)
def test_templates(Wordlist, Generic, StructureDataset, comp, query, oid, ds, expected):
Expand All @@ -130,14 +130,14 @@ def test_templates(Wordlist, Generic, StructureDataset, comp, query, oid, ds, ex


def test_reference_list(StructureDataset):
text = """# [](ParameterTable?__template__=property.md&name=name#cldf:B)
text = """# [](ParameterTable?__template__=property.md&property=name#cldf:B)
See [](Source?ref&with_internal_ref_link#cldf:Peterson2017) and [ex](http://example.com)
[References](Source?cited_only#cldf:__all__)
"""
res = render(text, StructureDataset)
assert "Gender/Noun classes" in res
assert "# Gender/Noun classes" in res
assert 'Peterson 2017' in res
assert "Fitting the pieces together" in res

Expand Down

0 comments on commit c66ad29

Please sign in to comment.