aboutsummaryrefslogtreecommitdiffstats
path: root/tools/snippets_translate/main.py
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2023-04-21 11:29:44 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2023-04-27 15:00:07 +0200
commit324ae183534246fc2ae8f122cb7c40bdcf20d0ea (patch)
tree0bc02044387f47e33fcf294dc2a793412941cca7 /tools/snippets_translate/main.py
parent1099bab042cdb31d312bc5502a7e80656092e879 (diff)
snippets_translate: Fix repetitive snippet ID lines
When writing consecutive snippets like // [1] line1 // [1] [2] line2 // [2] the line "// [1] [2]" would appear twice as end marker of snippet 1 and start marker of snippet 2, causing QtXmlToSphinx to extract an empty snippet for [2]. Fix by reconstructing the ID lines to use one ID per line. Task-number: PYSIDE-1106 Pick-to: 6.5 Change-Id: I0d7c0b30ff6bcc51c7d9ea6c9bfc844316a41c67 Reviewed-by: Shyamnath Premnadh <Shyamnath.Premnadh@qt.io>
Diffstat (limited to 'tools/snippets_translate/main.py')
-rw-r--r--tools/snippets_translate/main.py15
1 files changed, 11 insertions, 4 deletions
diff --git a/tools/snippets_translate/main.py b/tools/snippets_translate/main.py
index 43f4f7627..01ea06c5e 100644
--- a/tools/snippets_translate/main.py
+++ b/tools/snippets_translate/main.py
@@ -213,7 +213,9 @@ def get_snippet_override(start_id: str, rel_path: str) -> List[str]:
return overriden_snippet_lines(lines, start_id)
-def _get_snippets(lines: List[str], pattern: re.Pattern) -> Dict[str, List[str]]:
+def _get_snippets(lines: List[str],
+ comment: str,
+ pattern: re.Pattern) -> Dict[str, List[str]]:
"""Helper to extract (potentially overlapping) snippets from a C++ file
indicated by pattern ("//! [1]") and return them as a dict by <id>."""
snippets: Dict[str, List[str]] = {}
@@ -231,8 +233,12 @@ def _get_snippets(lines: List[str], pattern: re.Pattern) -> Dict[str, List[str]]
start_id = start_ids.pop(0)
if start_id in done_snippets:
continue
+
+ # Reconstruct a single ID line to avoid repetitive ID lines
+ # by consecutive snippets with multi-ID lines like "//! [1] [2]"
+ id_line = f"{comment}! [{start_id}]"
done_snippets.append(start_id)
- snippet = [line] # The snippet starts with this id
+ snippet = [id_line] # The snippet starts with this id
# Find the end of the snippet
j = i
@@ -246,6 +252,7 @@ def _get_snippets(lines: List[str], pattern: re.Pattern) -> Dict[str, List[str]]
# Check if the snippet is complete
if start_id in get_snippet_ids(l, pattern):
# End of snippet
+ snippet[len(snippet) - 1] = id_line
snippets[start_id] = snippet
break
@@ -260,7 +267,7 @@ def get_python_example_snippet_override(start_id: str, rel_path: str) -> List[st
return []
path, id = value
file_lines = path.read_text().splitlines()
- snippet_dict = _get_snippets(file_lines, PYTHON_SNIPPET_PATTERN)
+ snippet_dict = _get_snippets(file_lines, '#', PYTHON_SNIPPET_PATTERN)
lines = snippet_dict.get(id)
if not lines:
raise RuntimeError(f'Snippet "{id}" not found in "{os.fspath(path)}"')
@@ -271,7 +278,7 @@ def get_python_example_snippet_override(start_id: str, rel_path: str) -> List[st
def get_snippets(lines: List[str], rel_path: str) -> List[List[str]]:
"""Extract (potentially overlapping) snippets from a C++ file indicated
by '//! [1]'."""
- result = _get_snippets(lines, CPP_SNIPPET_PATTERN)
+ result = _get_snippets(lines, '//', CPP_SNIPPET_PATTERN)
id_list = result.keys()
for snippet_id in id_list:
# Check file overrides and example overrides