Changeset a5f3cc1 in rtems-central for rtemsqual/glossary.py


Ignore:
Timestamp:
May 6, 2020, 8:31:57 AM (10 months ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
master
Children:
833470c
Parents:
fa46b668
git-author:
Sebastian Huber <sebastian.huber@…> (05/06/20 08:31:57)
git-committer:
Sebastian Huber <sebastian.huber@…> (05/28/20 08:37:29)
Message:

spec: Use item mapper for glossary terms

File:
1 edited

Legend:

Unmodified
Added
Removed
  • rtemsqual/glossary.py

    rfa46b668 ra5f3cc1  
    2727import glob
    2828import re
    29 from typing import Dict
     29from typing import Any, Dict, Optional
    3030
    31 from rtemsqual.content import MacroToSphinx, SphinxContent
    32 from rtemsqual.items import Item, ItemCache
     31from rtemsqual.content import SphinxContent, SphinxMapper
     32from rtemsqual.items import Item, ItemCache, ItemMapper
    3333
    3434ItemMap = Dict[str, Item]
     
    5555    with content.indent():
    5656        content.append(":sorted:")
    57     macro_to_sphinx = MacroToSphinx()
    58     macro_to_sphinx.set_terms(terms)
    5957    for item in sorted(terms.values(),
    6058                       key=lambda x: x["glossary-term"].lower()):
    61         text = macro_to_sphinx.substitute(item["text"].strip())
    6259        content.register_license_and_copyrights_of_item(item)
    6360        with content.indent():
     61            text = SphinxMapper(item).substitute(item["text"])
    6462            content.add_definition_item(item["glossary-term"], text)
    6563    content.add_licence_and_copyrights()
     
    8381
    8482
    85 def _resolve_glossary_term(document_terms: ItemMap, project_terms: ItemMap,
    86                            term: Item) -> None:
    87     for match in re.findall(r"@@|@([a-z]+){([^}]+)}", term["text"]):
    88         if match[1] and match[1] not in document_terms:
    89             new_term = project_terms[match[1]]
    90             document_terms[match[1]] = new_term
    91             _resolve_glossary_term(document_terms, project_terms, new_term)
     83class _GlossaryMapper(ItemMapper):
     84    def __init__(self, item: Item, document_terms: ItemMap):
     85        super().__init__(item)
     86        self._document_terms = document_terms
     87
     88    def get_value(self, item: Item, _path: str, _value: Any, key: str,
     89                  _index: Optional[int]) -> Any:
     90        """ Recursively adds glossary terms to the document terms. """
     91        if key == "glossary-term":
     92            if item.uid not in self._document_terms:
     93                self._document_terms[item.uid] = item
     94                _GlossaryMapper(item,
     95                                self._document_terms).substitute(item["text"])
     96        # The value of this substitute is unused.
     97        return ""
    9298
    9399
    94 def _resolve_glossary_terms(document_terms: ItemMap,
    95                             project_terms: ItemMap) -> None:
     100def _resolve_glossary_terms(document_terms: ItemMap) -> None:
    96101    for term in list(document_terms.values()):
    97         _resolve_glossary_term(document_terms, project_terms, term)
     102        _GlossaryMapper(term, document_terms).substitute(term["text"])
    98103
    99104
     
    107112    for path in config["rest-source-paths"]:
    108113        _find_glossary_terms(path, document_terms, project_terms)
    109     _resolve_glossary_terms(document_terms, project_terms)
     114    _resolve_glossary_terms(document_terms)
    110115    content = _generate_glossary_content(document_terms)
    111116    content.write(config["target"])
Note: See TracChangeset for help on using the changeset viewer.