1 | # SPDX-License-Identifier: BSD-2-Clause |
---|
2 | """ This module provides functions for glossary of terms generation. """ |
---|
3 | |
---|
4 | # Copyright (C) 2019, 2020 embedded brains GmbH (http://www.embedded-brains.de) |
---|
5 | # |
---|
6 | # Redistribution and use in source and binary forms, with or without |
---|
7 | # modification, are permitted provided that the following conditions |
---|
8 | # are met: |
---|
9 | # 1. Redistributions of source code must retain the above copyright |
---|
10 | # notice, this list of conditions and the following disclaimer. |
---|
11 | # 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | # notice, this list of conditions and the following disclaimer in the |
---|
13 | # documentation and/or other materials provided with the distribution. |
---|
14 | # |
---|
15 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
16 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
19 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | # POSSIBILITY OF SUCH DAMAGE. |
---|
26 | |
---|
27 | import glob |
---|
28 | import re |
---|
29 | from typing import Dict |
---|
30 | |
---|
31 | from rtemsqual.content import MacroToSphinx, SphinxContent |
---|
32 | from rtemsqual.items import Item, ItemCache |
---|
33 | |
---|
34 | ItemMap = Dict[str, Item] |
---|
35 | |
---|
36 | |
---|
37 | def _gather_glossary_groups(item: Item, glossary_groups: ItemMap) -> None: |
---|
38 | for child in item.children(): |
---|
39 | _gather_glossary_groups(child, glossary_groups) |
---|
40 | if item["type"] == "glossary" and item["glossary-type"] == "group": |
---|
41 | glossary_groups[item.uid] = item |
---|
42 | |
---|
43 | |
---|
44 | def _gather_glossary_terms(item: Item, glossary_terms: ItemMap) -> None: |
---|
45 | for child in item.children(): |
---|
46 | _gather_glossary_terms(child, glossary_terms) |
---|
47 | if item["type"] == "glossary" and item["glossary-type"] == "term": |
---|
48 | glossary_terms[item.uid] = item |
---|
49 | |
---|
50 | |
---|
51 | def _generate_glossary_content(terms: ItemMap) -> SphinxContent: |
---|
52 | content = SphinxContent() |
---|
53 | content.add_header("Glossary", level="*") |
---|
54 | content.add(".. glossary::") |
---|
55 | with content.indent(): |
---|
56 | content.append(":sorted:") |
---|
57 | macro_to_sphinx = MacroToSphinx() |
---|
58 | macro_to_sphinx.set_terms(terms) |
---|
59 | for item in sorted(terms.values(), |
---|
60 | key=lambda x: x["glossary-term"].lower()): |
---|
61 | text = macro_to_sphinx.substitute(item["text"].strip()) |
---|
62 | item.register_license_and_copyrights(content) |
---|
63 | with content.indent(): |
---|
64 | content.add_definition_item(item["glossary-term"], text) |
---|
65 | content.add_licence_and_copyrights() |
---|
66 | return content |
---|
67 | |
---|
68 | |
---|
69 | def _make_glossary_term_uid(term: str) -> str: |
---|
70 | return "/glos/term/" + re.sub(r"[^a-zA-Z0-9]+", "", term.replace( |
---|
71 | "+", "X")).lower() |
---|
72 | |
---|
73 | |
---|
74 | def _find_glossary_terms(path: str, document_terms: ItemMap, |
---|
75 | project_terms: ItemMap) -> None: |
---|
76 | for src in glob.glob(path + "/**/*.rst", recursive=True): |
---|
77 | if src.endswith("glossary.rst"): |
---|
78 | continue |
---|
79 | with open(src, "r") as out: |
---|
80 | for term in re.findall(":term:`([^`]+)`", out.read()): |
---|
81 | uid = _make_glossary_term_uid(term) |
---|
82 | document_terms[uid] = project_terms[uid] |
---|
83 | |
---|
84 | |
---|
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) |
---|
92 | |
---|
93 | |
---|
94 | def _resolve_glossary_terms(document_terms: ItemMap, |
---|
95 | project_terms: ItemMap) -> None: |
---|
96 | for term in list(document_terms.values()): |
---|
97 | _resolve_glossary_term(document_terms, project_terms, term) |
---|
98 | |
---|
99 | |
---|
100 | def _generate_project_glossary(target: str, project_terms: ItemMap) -> None: |
---|
101 | content = _generate_glossary_content(project_terms) |
---|
102 | content.write(target) |
---|
103 | |
---|
104 | |
---|
105 | def _generate_document_glossary(config: dict, project_terms: ItemMap) -> None: |
---|
106 | document_terms = {} # type: ItemMap |
---|
107 | for path in config["rest-source-paths"]: |
---|
108 | _find_glossary_terms(path, document_terms, project_terms) |
---|
109 | _resolve_glossary_terms(document_terms, project_terms) |
---|
110 | content = _generate_glossary_content(document_terms) |
---|
111 | content.write(config["target"]) |
---|
112 | |
---|
113 | |
---|
114 | def generate(config: dict, item_cache: ItemCache) -> None: |
---|
115 | """ |
---|
116 | Generates glossaries of terms according to the configuration. |
---|
117 | |
---|
118 | :param config: A dictionary with configuration entries. |
---|
119 | :param item_cache: The specification item cache containing the glossary |
---|
120 | groups and terms. |
---|
121 | """ |
---|
122 | groups = {} # type: ItemMap |
---|
123 | for item in item_cache.top_level.values(): |
---|
124 | _gather_glossary_groups(item, groups) |
---|
125 | |
---|
126 | project_terms = {} # type: ItemMap |
---|
127 | for group in config["project-groups"]: |
---|
128 | _gather_glossary_terms(groups[group], project_terms) |
---|
129 | |
---|
130 | _generate_project_glossary(config["project-target"], project_terms) |
---|
131 | |
---|
132 | for document_config in config["documents"]: |
---|
133 | _generate_document_glossary(document_config, project_terms) |
---|