-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcheck_markup.py
More file actions
72 lines (58 loc) · 2.22 KB
/
Copy pathcheck_markup.py
File metadata and controls
72 lines (58 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
"""
scripts/check_markup.py
Verify that Sphinx roles, inline literals, and format placeholders in the
English msgid are preserved verbatim in the Persian msgstr. Catches the most
common review slip: translating or mangling `:class:`int``-style markup,
``code`` spans, %s/{0} placeholders, or |substitution| refs.
Requires: pip install polib
Usage:
python3 scripts/check_markup.py library/functions.po
python3 scripts/check_markup.py tutorial/*.po
python3 scripts/check_markup.py . # recurse a whole directory
"""
import re
import sys
from pathlib import Path
import polib
PATTERNS = [
("sphinx role", re.compile(r":(?:\w+:)?[\w.-]+:`.*?`")),
("literal/code span", re.compile(r"``.*?``")),
("substitution ref", re.compile(r"\|[\w.-]+\|")),
("percent placeholder", re.compile(r"%\(\w+\)[a-zA-Z]|%[a-zA-Z]")),
("brace placeholder", re.compile(r"\{[^{}\s]*\}")),
]
def check_file(path: Path) -> int:
problems = 0
po = polib.pofile(str(path))
for entry in po:
if entry.obsolete or not entry.msgid or not entry.msgstr:
continue # obsolete entry, header, or still untranslated
for label, pattern in PATTERNS:
expected = pattern.findall(entry.msgid)
if not expected:
continue
missing = [tok for tok in expected if tok not in entry.msgstr]
if missing:
problems += 1
loc = f" ({entry.occurrences[0][0]}:{entry.occurrences[0][1]})" if entry.occurrences else ""
tag = " [fuzzy]" if entry.fuzzy else ""
print(f"{path}{loc}{tag}: missing {label}: {missing}")
print(f" msgid : {entry.msgid[:100]}")
print(f" msgstr: {entry.msgstr[:100]}")
return problems
def main():
if len(sys.argv) < 2:
print(__doc__)
sys.exit(1)
files = []
for arg in sys.argv[1:]:
p = Path(arg)
files.extend(sorted(p.rglob("*.po"))) if p.is_dir() else files.append(p)
total = sum(check_file(f) for f in files)
if total:
print(f"\n{total} markup mismatch(es) found.")
sys.exit(1)
print("No markup mismatches found.")
if __name__ == "__main__":
main()