Skip to content

Commit 60e8d54

Browse files
authored
Merge pull request #122 from OpenSemanticLab/dev
Fix missing page detection and OSW-ID replacement in code generation
2 parents 397da7d + 316ce65 commit 60e8d54

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

‎src/osw/controller/page_package.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171
group_keys=["file"],
7272
),
7373
"Item strings": RegExPatternExtended(
74-
description="Match: Item:OSW<uuid>",
75-
pattern=r"(Item:OSW[a-f0-9]{32})",
74+
description="Match: Item:OSW<uuid> but not subobject refs after #",
75+
pattern=r"(?<!#)(Item:OSW[a-f0-9]{32})",
7676
group_keys=["item"],
7777
),
7878
}

‎src/osw/utils/code_postprocessing.py‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,18 @@ def resolve_osw_id_type_hints(content: str) -> str:
8989
if not osw_id_to_class:
9090
return content
9191

92-
# Find bare OSW IDs used as identifiers (not inside "Category:OSW..." strings)
92+
# Replace bare OSW IDs used as identifiers with the resolved class name.
93+
# Skip comment lines: the datamodel-codegen header
94+
# "# filename: OSW....json" contains an OSW ID that is NOT a type hint
95+
# and must stay intact (the lookbehind/lookahead alone do not exclude it,
96+
# since it is preceded by a space and followed by ".json").
9397
bare_osw_pattern = re.compile(r"(?<![:\w])OSW[0-9a-f]{32}(?!\w)")
94-
unresolved = set(bare_osw_pattern.findall(content))
9598

96-
for osw_id in unresolved:
97-
if osw_id in osw_id_to_class:
98-
content = re.sub(
99-
r"(?<![:\w])" + re.escape(osw_id) + r"(?!\w)",
100-
osw_id_to_class[osw_id],
101-
content,
102-
)
99+
def _resolve_line(line: str) -> str:
100+
if line.lstrip().startswith("#"):
101+
return line # never rewrite comments
102+
return bare_osw_pattern.sub(
103+
lambda m: osw_id_to_class.get(m.group(0), m.group(0)), line
104+
)
103105

104-
return content
106+
return "".join(_resolve_line(line) for line in content.splitlines(keepends=True))

‎src/osw/utils/regex.py‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,15 @@ def count_match_groups(pattern: Union[str, re.Pattern]):
181181
group_count += 1
182182
unmatched_brackets -= 1
183183

184-
corrected_group_count = group_count - pattern.count("(?:")
184+
# Subtract non-capturing groups and zero-width assertions
185+
non_capturing = (
186+
pattern.count("(?:")
187+
+ pattern.count("(?=")
188+
+ pattern.count("(?!")
189+
+ pattern.count("(?<=")
190+
+ pattern.count("(?<!")
191+
)
192+
corrected_group_count = group_count - non_capturing
185193
return corrected_group_count
186194

187195

0 commit comments

Comments
 (0)