Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/osw/controller/page_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
group_keys=["file"],
),
"Item strings": RegExPatternExtended(
description="Match: Item:OSW<uuid>",
pattern=r"(Item:OSW[a-f0-9]{32})",
description="Match: Item:OSW<uuid> but not subobject refs after #",
pattern=r"(?<!#)(Item:OSW[a-f0-9]{32})",
group_keys=["item"],
),
}
Expand Down
22 changes: 12 additions & 10 deletions src/osw/utils/code_postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,18 @@ def resolve_osw_id_type_hints(content: str) -> str:
if not osw_id_to_class:
return content

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

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

return content
return "".join(_resolve_line(line) for line in content.splitlines(keepends=True))
10 changes: 9 additions & 1 deletion src/osw/utils/regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,15 @@ def count_match_groups(pattern: Union[str, re.Pattern]):
group_count += 1
unmatched_brackets -= 1

corrected_group_count = group_count - pattern.count("(?:")
# Subtract non-capturing groups and zero-width assertions
non_capturing = (
pattern.count("(?:")
+ pattern.count("(?=")
+ pattern.count("(?!")
+ pattern.count("(?<=")
+ pattern.count("(?<!")
)
corrected_group_count = group_count - non_capturing
return corrected_group_count


Expand Down
Loading