From 0174da56742c82322d5c04167bf76b217903247d Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Wed, 12 May 2021 14:05:05 +0200 Subject: content: Improve plural of terms --- rtemsspec/content.py | 5 ++++- rtemsspec/sphinxcontent.py | 8 +++----- rtemsspec/tests/spec-sphinx/z.yml | 4 ++++ rtemsspec/tests/test_content_sphinx.py | 3 ++- 4 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 rtemsspec/tests/spec-sphinx/z.yml diff --git a/rtemsspec/content.py b/rtemsspec/content.py index bffcacff..98cb5ebe 100644 --- a/rtemsspec/content.py +++ b/rtemsspec/content.py @@ -1042,7 +1042,10 @@ def get_value_plural(ctx: ItemGetValueContext) -> Any: try: return ctx.value[ctx.key] except KeyError: - return f"{ctx.value['term']}s" + term = ctx.value["term"] + if term.endswith("y"): + return f"{term[:-1]}ies" + return f"{term}s" class ExpressionMapper: diff --git a/rtemsspec/sphinxcontent.py b/rtemsspec/sphinxcontent.py index 5e530ff3..1d72ae7c 100644 --- a/rtemsspec/sphinxcontent.py +++ b/rtemsspec/sphinxcontent.py @@ -27,7 +27,8 @@ from contextlib import contextmanager from typing import Any, Iterable, Iterator, List, Optional, Sequence, Union -from rtemsspec.content import Content, make_lines, to_camel_case +from rtemsspec.content import Content, get_value_plural, make_lines, \ + to_camel_case from rtemsspec.items import Item, ItemGetValue, ItemGetValueContext, ItemMapper GenericContent = Union[str, List[str], "Content"] @@ -203,10 +204,7 @@ def _get_ref_term(ctx: ItemGetValueContext) -> Any: def _get_ref_term_plural(ctx: ItemGetValueContext) -> Any: - try: - return f":term:`{ctx.value[ctx.key]} <{ctx.value['term']}>`" - except KeyError: - return f":term:`{ctx.value['term']}s <{ctx.value['term']}>`" + return f":term:`{get_value_plural(ctx)} <{ctx.value['term']}>`" def _get_appl_config_option(ctx: ItemGetValueContext) -> Any: diff --git a/rtemsspec/tests/spec-sphinx/z.yml b/rtemsspec/tests/spec-sphinx/z.yml new file mode 100644 index 00000000..d8fcbe80 --- /dev/null +++ b/rtemsspec/tests/spec-sphinx/z.yml @@ -0,0 +1,4 @@ +term: z +glossary-type: term +links: [] +type: glossary diff --git a/rtemsspec/tests/test_content_sphinx.py b/rtemsspec/tests/test_content_sphinx.py index 85d1e151..20fa258a 100644 --- a/rtemsspec/tests/test_content_sphinx.py +++ b/rtemsspec/tests/test_content_sphinx.py @@ -296,7 +296,8 @@ def test_substitute(tmpdir): with pytest.raises(ValueError, match=match): mapper.substitute("${x:/y}") assert mapper.substitute("${x:/term}") == ":term:`y`" - assert mapper.substitute("${x:/plural}") == ":term:`ys `" + assert mapper.substitute("${x:/plural}") == ":term:`ies `" + assert mapper.substitute("${z:/plural}") == ":term:`zs `" mapper.add_get_value("other:/name", lambda ctx: ctx.value[ctx.key]) assert mapper.substitute("${y:/name}") == "foobar" assert mapper.substitute("${a:/name}") == ":c:data:`a`" -- cgit v1.2.3