From 6705728abadaeb73cc639cc8da104b3f62be8ec8 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 2 Jul 2024 19:15:31 +0200 Subject: [PATCH] QLocaleXML: use attributes for likely subtag tag parts This makes the likely subtag part of the file more compact. Introduces a QLocaleXmlWriter.asTag() for attribute-only elements; this requires the Spacer to recognize self-closing elements as not increasing the indent needed. Change-Id: I1b73b755f9841617a5c002cf624785321e808d0c Reviewed-by: Mate Barany --- util/locale_database/qlocalexml.py | 25 ++++++++++++++----------- util/locale_database/qlocalexml.rnc | 6 +++--- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/util/locale_database/qlocalexml.py b/util/locale_database/qlocalexml.py index d0f5ccbe01f..ae1748fba64 100644 --- a/util/locale_database/qlocalexml.py +++ b/util/locale_database/qlocalexml.py @@ -224,8 +224,8 @@ class QLocaleXmlReader (object): # Likely subtag management: def __likelySubtagsMap(self): - def triplet(element, keys=('language', 'script', 'territory'), kid = self.__firstChildText): - return tuple(int(kid(element, key)) for key in keys) + def triplet(element, keys=('language', 'script', 'territory')): + return tuple(int(element.attributes[key].nodeValue) for key in keys) kid = self.__firstChildElt for elt in self.__eachEltInGroup(self.root, 'likelySubtags', 'likelySubtag'): @@ -341,9 +341,10 @@ class Spacer (object): indent = self.current = indent[:-len(self.__each)] elif line.startswith('<') and line[1:2] not in '!?': cut = line.find('>') - tag = (line[1:] if cut < 0 else line[1 : cut]).strip().split()[0] - if f'' not in line: - self.current += self.__each + if cut < 1 or line[cut - 1] != '/': + tag = (line[1:] if cut < 0 else line[1 : cut]).strip().split()[0] + if f'' not in line: + self.current += self.__each return indent + line + '\n' def __call__(self, line): @@ -475,6 +476,12 @@ class QLocaleXmlWriter (object): head = tag self.__write(f'<{head}>{text}') + def asTag(self, tag, **attrs): + """Similar to inTag(), but with no content for the element.""" + assert attrs, tag # No point to this otherwise + tail = ' '.join(f'{k}="{v}"' for k, v in attrs.items()) + self.__write(f'<{tag} {tail} />') + def close(self, grumble): """Finish writing and grumble about any issues discovered.""" if self.__rawOutput != self.__complain: @@ -521,12 +528,8 @@ class QLocaleXmlWriter (object): self.__closeTag(f'{tag}List') def __likelySubTag(self, tag, likely): - self.__openTag(tag) - self.inTag('language', likely[0]) - self.inTag('script', likely[1]) - self.inTag('territory', likely[2]) - # self.inTag('variant', likely[3]) - self.__closeTag(tag) + self.asTag(tag, language = likely[0], script = likely[1], + territory = likely[2]) # variant = likely[3] def __writeLocale(self, locale, calendars): locale.toXml(self.inTag, calendars) diff --git a/util/locale_database/qlocalexml.rnc b/util/locale_database/qlocalexml.rnc index 44129724ff2..123fe3fd04c 100644 --- a/util/locale_database/qlocalexml.rnc +++ b/util/locale_database/qlocalexml.rnc @@ -34,9 +34,9 @@ LikelySubtag = element likelySubtag { } LocaleTriplet = ( - element language { xsd:nonNegativeInteger }, - element script { xsd:nonNegativeInteger }, - element territory { xsd:nonNegativeInteger } + attribute language { xsd:nonNegativeInteger }, + attribute script { xsd:nonNegativeInteger }, + attribute territory { xsd:nonNegativeInteger } ) # TODO: xsd patterns for IANA IDs and space-joined lists of them