diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2020-05-27 14:42:09 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2020-05-28 10:38:23 +0200 |
commit | e720d8418a2154a0786b62d77e2859624aff5a9e (patch) | |
tree | 4fddeb73dcdee633b0ad142176b2b4979b1bbbc4 /rtemsqual/sphinxcontent.py | |
parent | sphinxcontent: Make label/reference functions (diff) | |
download | rtems-central-e720d8418a2154a0786b62d77e2859624aff5a9e.tar.bz2 |
sphinxcontent: Add SphinxMapper.add_get_reference()
Diffstat (limited to 'rtemsqual/sphinxcontent.py')
-rw-r--r-- | rtemsqual/sphinxcontent.py | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/rtemsqual/sphinxcontent.py b/rtemsqual/sphinxcontent.py index 670b32b5..e2b2bbc8 100644 --- a/rtemsqual/sphinxcontent.py +++ b/rtemsqual/sphinxcontent.py @@ -25,8 +25,10 @@ # POSSIBILITY OF SUCH DAMAGE. from contextlib import contextmanager +import os import re -from typing import Any, Iterable, Iterator, List, Optional, Union +from typing import Any, Dict, Callable, Iterable, Iterator, List, Optional, \ + Union from rtemsqual.content import Content, make_lines from rtemsqual.items import Item, ItemMapper @@ -191,15 +193,28 @@ class SphinxContent(Content): self.prepend([f".. SPDX-License-Identifier: {self._license}", ""]) +def _get_ref_term(value: Any, key: str) -> str: + return f":term:`{value[key]}`" + + class SphinxMapper(ItemMapper): """ Sphinx mapper. """ def __init__(self, item: Item): super().__init__(item) + self._get_ref = { + "glossary:/term": _get_ref_term + } # type: Dict[str, Callable[[Any, str], str]] + + def add_get_reference(self, type_name: str, path: str, + get_ref: Callable[[Any, str], str]) -> None: + """ + Adds a function to get a reference to the specified path for items of + the specified type. + """ + self._get_ref[f"{type_name}:{path}"] = get_ref - def get_value(self, _item: Item, _path: str, value: Any, key: str, + def get_value(self, item: Item, path: str, value: Any, key: str, _index: Optional[int]) -> Any: """ Gets a value by key and optional index. """ - # pylint: disable=no-self-use - if key == "term": - return f":term:`{value[key]}`" - raise KeyError + return self._get_ref[f"{item['type']}:{os.path.join(path, key)}"]( + value, key) |