Debian Codegraph @

citeproc/data/locales/util/add-locale-terms.py

raw
1 # MIT license
2
3 # Copy new terms of `locales-en-US.xml` to other locals.
4
5 # Step 1: Add new terms in `locales-en-US.xml`. Make sure the "short", "verb",
6 # "verb-short" forms of each new term are also included.
7 # Step 2: Run `python3 add-locale-terms.py`
8
9
10 import glob
11 import os
12 import re
13
14 from lxml import etree
15
16
17 LOCALES_DIR = '.'
18 NSMAP = {'cs': 'http://purl.org/net/xbiblio/csl'}
19
20
21 def get_term_id(term) -> str:
22 # e.g., `editor|short`
23 term_id = term.attrib['name']
24 if 'form' in term.attrib:
25 term_id += '|' + term.attrib['form']
26 # ignore "gender" and "gender-form"
27 return term_id
28
29
30 def add_new_terms_to_locale(path, element_tree, new_terms, english_term_ids,
31 english_term_dict, locale_terms_el,
32 locale_term_ids, locale_term_list):
33 for term_id in new_terms:
34 common_terms = [
35 tid for tid in english_term_ids if tid in locale_term_ids
36 ]
37
38 previous_terms = english_term_ids[:english_term_ids.index(term_id)]
39 previous_common_term = [
40 tid for tid in previous_terms if tid in common_terms
41 ][-1]
42 insert_index = locale_terms_el.index(
43 locale_term_list[locale_term_ids.index(previous_common_term)])
44 locale_terms_el.insert(insert_index, english_term_dict[term_id])
45
46 et_str = etree.tostring(element_tree,
47 pretty_print=True,
48 xml_declaration=True,
49 encoding='utf-8').decode('utf-8')
50
51 # https://github.com/citation-style-language/utilities/blob/master/csl-reindenting-and-info-reordering.py
52 et_str = et_str.replace("'", '"', 4) # replace quotes on XML declaration
53
54 et_str = et_str.replace(' ', ' ') # no-break space
55 # et_str = et_str.replace('ᵉ', 'ᵉ')
56 et_str = et_str.replace(' ', ' ') # em space
57 et_str = et_str.replace(' ', ' ') # thin space
58 et_str = et_str.replace('‑', '‑') # non-breaking hyphen
59 # # et_str = et_str.replace('–', "–") # en dash
60 # et_str = et_str.replace('—', '—') # em dash
61 et_str = et_str.replace(' ', ' ') # narrow no-break space
62
63 et_str = re.sub(r'<term (.*?)/>', r'<term \1></term>', et_str)
64 et_str = et_str.replace('<single/>', '<single></single>')
65 et_str = et_str.replace('<multiple/>', '<multiple></multiple>')
66
67 with open(path, 'w') as f:
68 f.write(et_str.strip())
69 f.write('\n')
70
71
72 def main():
73 english_locale = 'locales-en-US.xml'
74 english_path = os.path.join(LOCALES_DIR, english_locale)
75 english_term_dict = dict()
76 english_term_ids = []
77
78 for term in etree.parse(english_path).findall('.//cs:term', NSMAP):
79 term_id = get_term_id(term)
80 english_term_ids.append(term_id)
81 english_term_dict[term_id] = term
82
83 for path in sorted(glob.glob(os.path.join(LOCALES_DIR, 'locales-*.xml'))):
84 locale_file = os.path.split(path)[1]
85 if locale_file == english_locale:
86 continue
87
88 element_tree = etree.parse(path)
89 locale_terms_el = element_tree.find('.//cs:terms', NSMAP)
90
91 locale_term_ids = []
92 locale_term_list = []
93 for term in locale_terms_el.findall('.//cs:term', NSMAP):
94 term_id = get_term_id(term)
95 locale_term_ids.append(term_id)
96 locale_term_list.append(term)
97
98 new_terms = [
99 term_id for term_id in english_term_ids
100 if term_id not in locale_term_ids and 'ordinal-' not in term_id
101 ]
102 new_terms = [
103 term_id for term_id in new_terms
104 if term_id.split('|')[0] in new_terms
105 ]
106 if new_terms:
107 add_new_terms_to_locale(path, element_tree, new_terms,
108 english_term_ids, english_term_dict,
109 locale_terms_el, locale_term_ids,
110 locale_term_list)
111
112
113 if __name__ == '__main__':
114 main()