Skip to content

Commit d81b5f0

Browse files
committed
fix: repair transifex markup
1 parent 1a74aed commit d81b5f0

1 file changed

Lines changed: 48 additions & 3 deletions

File tree

scripts/pull-translations.sh

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,26 @@ rename_or_merge() {
6969
# Validate XML file is well-formed before processing
7070
validate_xml() {
7171
local file="$1"
72-
# Basic validation: check for opening and closing resources tags
72+
if command -v python3 &> /dev/null; then
73+
python3 - "$file" <<'PY'
74+
import sys
75+
import xml.etree.ElementTree as ET
76+
77+
try:
78+
ET.parse(sys.argv[1])
79+
except Exception as e:
80+
print(e)
81+
raise SystemExit(1)
82+
PY
83+
return $?
84+
fi
85+
86+
# Fallback validation: check for opening and closing resources tags
7387
if ! grep -q '<resources' "$file" 2>/dev/null || ! grep -q '</resources>' "$file" 2>/dev/null; then
74-
echo " Warning: $file appears to be malformed XML, skipping normalization"
88+
echo "missing resources root"
7589
return 1
7690
fi
91+
7792
return 0
7893
}
7994

@@ -164,10 +179,33 @@ echo ""
164179
echo "Normalizing XML formatting..."
165180

166181
# Normalize XML
182+
MARKUP_FIXED_COUNT=0
167183
NORMALIZED_COUNT=0
184+
XML_ERROR_COUNT=0
168185
while IFS= read -r file; do
186+
if sed \
187+
-e 's#<accent&gt;#\&lt;accent\&gt;#g' \
188+
-e 's#</accent&gt;#\&lt;/accent\&gt;#g' \
189+
-e 's#<bold&gt;#\&lt;bold\&gt;#g' \
190+
-e 's#</bold&gt;#\&lt;/bold\&gt;#g' \
191+
"$file" > "$file.markup.tmp"; then
192+
if cmp -s "$file" "$file.markup.tmp"; then
193+
rm -f "$file.markup.tmp"
194+
elif mv "$file.markup.tmp" "$file"; then
195+
MARKUP_FIXED_COUNT=$((MARKUP_FIXED_COUNT + 1))
196+
else
197+
echo " Warning: Failed to fix markup in $file"
198+
rm -f "$file.markup.tmp"
199+
fi
200+
else
201+
echo " Warning: Failed to scan markup in $file"
202+
rm -f "$file.markup.tmp"
203+
fi
204+
169205
# Validate XML before processing
170-
if ! validate_xml "$file"; then
206+
if ! validation_error=$(validate_xml "$file" 2>&1); then
207+
echo " Error: $file is malformed XML: $validation_error"
208+
XML_ERROR_COUNT=$((XML_ERROR_COUNT + 1))
171209
continue
172210
fi
173211

@@ -196,8 +234,14 @@ while IFS= read -r file; do
196234
fi
197235
done < <(find "$RES_DIR" -type f -path "*/values-*/strings.xml" 2>/dev/null)
198236

237+
echo " Fixed markup in $MARKUP_FIXED_COUNT files"
199238
echo " Normalized $NORMALIZED_COUNT files"
200239

240+
if [ "$XML_ERROR_COUNT" -ne 0 ]; then
241+
echo "Error: Found $XML_ERROR_COUNT malformed XML file(s) after pull"
242+
exit 1
243+
fi
244+
201245
echo ""
202246
echo "Cleaning up empty files and directories..."
203247

@@ -239,3 +283,4 @@ echo "Complete!"
239283
echo " Renamed: $RENAMED_COUNT, Removed: $REMOVED_COUNT"
240284
echo " Normalized: $NORMALIZED_COUNT"
241285
echo " Deleted files: $EMPTY_COUNT, Deleted dirs: $DELETED_DIRS"
286+
echo "Pull complete!"

0 commit comments

Comments
 (0)