Bump version to 21.06.18.1
[LibreOffice.git] / bin / ui-converter-skeleton.py
blobc9d6cc0daa1ae6ddc55286108add86701a075459
1 #!/bin/python
2 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
4 # This file is part of the LibreOffice project.
6 # This Source Code Form is subject to the terms of the Mozilla Public
7 # License, v. 2.0. If a copy of the MPL was not distributed with this
8 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 # this should parse a .ui and overwrite it with the same content
11 # for a in `git ls-files "*.ui"`; do bin/ui-converter-skeleton.py $a; done
13 import lxml.etree as etree
14 import sys
16 def add_truncate_multiline(current):
17 use_truncate_multiline = False
18 istarget = current.get('class') == "GtkEntry" or current.get('class') == "GtkSpinButton"
19 insertpos = 0
20 for child in current:
21 add_truncate_multiline(child)
22 insertpos = insertpos + 1;
23 if not istarget:
24 continue
25 if child.tag == "property":
26 attributes = child.attrib
27 if attributes.get("name") == "truncate_multiline" or attributes.get("name") == "truncate-multiline":
28 use_truncate_multiline = True
30 if istarget and not use_truncate_multiline:
31 truncate_multiline = etree.Element("property")
32 attributes = truncate_multiline.attrib
33 attributes["name"] = "truncate-multiline"
34 truncate_multiline.text = "True"
35 current.insert(insertpos - 1, truncate_multiline)
37 def do_replace_button_use_stock(current, use_stock, use_underline, label, insertpos):
38 if not use_underline:
39 underline = etree.Element("property")
40 attributes = underline.attrib
41 attributes["name"] = "use-underline"
42 underline.text = "True"
43 current.insert(insertpos - 1, underline)
44 current.remove(use_stock)
45 attributes = label.attrib
46 attributes["translatable"] = "yes"
47 attributes["context"] = "stock"
48 if label.text == 'gtk-add':
49 label.text = "_Add"
50 elif label.text == 'gtk-apply':
51 label.text = "_Apply"
52 elif label.text == 'gtk-cancel':
53 label.text = "_Cancel"
54 elif label.text == 'gtk-close':
55 label.text = "_Close"
56 elif label.text == 'gtk-delete':
57 label.text = "_Delete"
58 elif label.text == 'gtk-edit':
59 label.text = "_Edit"
60 elif label.text == 'gtk-help':
61 label.text = "_Help"
62 elif label.text == 'gtk-new':
63 label.text = "_New"
64 elif label.text == 'gtk-no':
65 label.text = "_No"
66 elif label.text == 'gtk-ok':
67 label.text = "_OK"
68 elif label.text == 'gtk-remove':
69 label.text = "_Remove"
70 elif label.text == 'gtk-revert-to-saved':
71 label.text = "_Reset"
72 elif label.text == 'gtk-yes':
73 label.text = "_yes"
74 else:
75 raise("unknown label")
77 def replace_button_use_stock(current):
78 use_underline = False
79 use_stock = None
80 label = None
81 isbutton = current.get('class') == "GtkButton"
82 insertpos = 0
83 for child in current:
84 replace_button_use_stock(child)
85 insertpos = insertpos + 1;
86 if not isbutton:
87 continue
88 if child.tag == "property":
89 attributes = child.attrib
90 if attributes.get("name") == "use_underline" or attributes.get("name") == "use-underline":
91 use_underline = True
92 if attributes.get("name") == "use_stock" or attributes.get("name") == "use-stock":
93 use_stock = child
94 if attributes.get("name") == "label":
95 label = child
97 if isbutton and use_stock != None:
98 do_replace_button_use_stock(current, use_stock, use_underline, label, insertpos)
100 def do_replace_image_stock(current, stock):
101 attributes = stock.attrib
102 attributes["name"] = "icon-name"
103 if stock.text == 'gtk-add':
104 stock.text = "list-add"
105 elif stock.text == 'gtk-remove':
106 stock.text = "list-remove"
107 elif stock.text == 'gtk-paste':
108 stock.text = "edit-paste"
109 elif stock.text == 'gtk-index':
110 stock.text = "vcl/res/index.png"
111 elif stock.text == 'gtk-refresh':
112 stock.text = "view-refresh"
113 elif stock.text == 'gtk-dialog-error':
114 stock.text = "dialog-error"
115 elif stock.text == 'gtk-apply':
116 stock.text = "sw/res/sc20558.png"
117 elif stock.text == 'gtk-missing-image':
118 stock.text = "missing-image"
119 elif stock.text == 'gtk-copy':
120 stock.text = "edit-copy"
121 elif stock.text == 'gtk-go-back':
122 stock.text = "go-previous"
123 elif stock.text == 'gtk-go-forward':
124 stock.text = "go-next"
125 elif stock.text == 'gtk-go-down':
126 stock.text = "go-down"
127 elif stock.text == 'gtk-go-up':
128 stock.text = "go-up"
129 elif stock.text == 'gtk-goto-first':
130 stock.text = "go-first"
131 elif stock.text == 'gtk-goto-last':
132 stock.text = "go-last"
133 elif stock.text == 'gtk-new':
134 stock.text = "document-new"
135 elif stock.text == 'gtk-media-stop':
136 stock.text = "media-playback-stop"
137 elif stock.text == 'gtk-media-play':
138 stock.text = "media-playback-start"
139 elif stock.text == 'gtk-media-next':
140 stock.text = "media-skip-forward"
141 elif stock.text == 'gtk-media-previous':
142 stock.text = "media-skip-backward"
143 elif stock.text == 'gtk-close':
144 stock.text = "window-close"
145 elif stock.text == 'gtk-help':
146 stock.text = "help-browser"
147 else:
148 raise("unknown stock name")
150 def replace_image_stock(current):
151 stock = None
152 isimage = current.get('class') == "GtkImage"
153 for child in current:
154 replace_image_stock(child)
155 if not isimage:
156 continue
157 if child.tag == "property":
158 attributes = child.attrib
159 if attributes.get("name") == "stock":
160 stock = child
162 if isimage and stock != None:
163 do_replace_image_stock(current, stock)
165 with open(sys.argv[1], encoding="utf-8") as f:
166 header = f.readline()
167 firstline = f.readline()
168 # the comment after the xml declaration goes missing unless we provide a
169 # custom doctype with tostring that contains the comment as a line after
170 # the true doctype
171 if firstline.startswith("<!--"):
172 header = header + firstline
173 f.seek(0)
174 # remove_blank_text so pretty-printed input doesn't disrupt pretty-printed
175 # output if nodes are added or removed
176 parser = etree.XMLParser(remove_blank_text=True)
177 tree = etree.parse(f, parser)
178 # make sure <property name="label" translatable="no"></property> stays like that
179 # and doesn't change to <property name="label" translatable="no"/>
180 for status_elem in tree.xpath("//property[@name='label' and string() = '']"):
181 status_elem.text = ""
182 root = tree.getroot()
184 # do some targeted conversion here
185 # tdf#138848 Copy-and-Paste in input box should not append an ENTER character
186 if not sys.argv[1].endswith('/multiline.ui'): # let this one alone not truncate multiline pastes
187 add_truncate_multiline(root)
188 replace_button_use_stock(root)
189 replace_image_stock(root)
191 with open(sys.argv[1], 'wb') as o:
192 # without encoding='unicode' (and the matching encode("utf8")) we get &#XXXX replacements for non-ascii characters
193 # which we don't want to see changed in the output
194 o.write(etree.tostring(root, pretty_print=True, method='xml', encoding='unicode', doctype=header[0:-1]).encode("utf8"))
196 # vim: set shiftwidth=4 softtabstop=4 expandtab: