From 85e55e5408095e9e98e3f9fd28b6d812682f0180 Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 6 Dec 2013 00:39:57 +0100 Subject: [PATCH] Add script to fix problematic linebreak/whitespace combinations in strings.xml --- tools/fix_all_strings.sh | 11 ++++++ tools/fix_strings.py | 76 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100755 tools/fix_all_strings.sh create mode 100755 tools/fix_strings.py diff --git a/tools/fix_all_strings.sh b/tools/fix_all_strings.sh new file mode 100755 index 000000000..47d837735 --- /dev/null +++ b/tools/fix_all_strings.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +SCRIPT=$(readlink -f $0) +SCRIPTPATH=`dirname $SCRIPT` +PROJECTROOT=`dirname $SCRIPTPATH` + +cd $PROJECTROOT + +find res/values* -name "strings.xml" -type f ! -wholename "res/values-fr-rCA/strings.xml" -exec ./tools/fix_strings.py {} \; + +cd - diff --git a/tools/fix_strings.py b/tools/fix_strings.py new file mode 100755 index 000000000..ff2ce52a2 --- /dev/null +++ b/tools/fix_strings.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python +# Rewrite a strings file to get rid of line break/whitespace combinations that get stripped when building with Gradle. +# +# Example: +# +# Account \"%s\" shrunk from +# %s +# to +# %s +# +# +# will be rendered as +# +# Account "account" shrunk from10MB to1MB +# +# when built with Gradle, but displays fine when built with Ant. +# +# +# Written for use with K-9 Mail (https://github.com/k9mail/k-9) +# Licensed under the WTFPL (http://www.wtfpl.net/about/) + +import sys +import re +from lxml import etree + + +def fix_text(element): + if element.text is not None: + element.text = re.sub(r'^\n\s*([^\s])', "\\1", element.text) + element.text = re.sub(r'\n\s*$', " ", element.text) + + +def fix_tail(element, is_last): + if element.tail is not None: + if is_last: + replacement = "" + else: + replacement = " " + element.tail = re.sub(r'^\n\s*([^\s])', " \\1", element.tail) + element.tail = re.sub(r'\n\s*$', replacement, element.tail) + + +def cleanup_string_elements(elements): + for element in elements: + if element.tag is None: + continue + + tag = element.tag + children = element.getchildren() + + if tag in ["string", "item"]: + if len(children) > 0: + fix_text(element) + + for child in children: + if isinstance(child.tag, basestring): + fix_text(child) + fix_tail(child, child == children[-1]) + + elif tag == "plurals": + cleanup_string_elements(children) + + +if len(sys.argv) < 2: + print "Usage: fix_strings.py " + print "Example: fix_strings.py res/values/strings.xml" + exit(1) + +strings_file = sys.argv[1] + +parser = etree.XMLParser(strip_cdata=False) +strings = etree.parse(strings_file, parser=parser) + +cleanup_string_elements(strings.getroot().getchildren()) + +strings.write(strings_file, xml_declaration=True, encoding="UTF-8", pretty_print=True)