1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 """Contains XLIFF-specific placeables."""
22
23 from translate.storage.placeables import base
24 from translate.storage.placeables.strelem import StringElem
25
26 __all__ = ['Bpt', 'Ept', 'X', 'Bx', 'Ex', 'G', 'It', 'Sub', 'Ph', 'UnknownXML', 'parsers', 'to_xliff_placeables']
27
28
31
32
35
36
39
40
43
44
47
48
51
52
55
56
59
60
63
64
66 """Placeable for unrecognized or umimplemented XML nodes. It's main
67 purpose is to preserve all associated XML data."""
68 iseditable = True
69
70
71 - def __init__(self, sub=None, id=None, rid=None, xid=None, xml_node=None, **kwargs):
79
80
81
83 """String representation of the sub-tree with the current node as the
84 root.
85
86 Copied from L{StringElem.__repr__}, but includes C{self.xml_node.tag}."""
87 tag = self.xml_node.tag
88 if tag.startswith('{'):
89 tag = tag[tag.index('}')+1:]
90
91 elemstr = ', '.join([repr(elem) for elem in self.sub])
92
93 return '<%(class)s{%(tag)s}(%(id)s%(rid)s%(xid)s[%(subs)s])>' % {
94 'class': self.__class__.__name__,
95 'tag': tag,
96 'id': self.id is not None and 'id="%s" ' % (self.id) or '',
97 'rid': self.rid is not None and 'rid="%s" ' % (self.rid) or '',
98 'xid': self.xid is not None and 'xid="%s" ' % (self.xid) or '',
99 'subs': elemstr
100 }
101
102
103
105 """Returns a copy of the sub-tree.
106 This should be overridden in sub-classes with more data.
107
108 NOTE: C{self.renderer} is B{not} copied."""
109 from copy import copy
110 cp = self.__class__(id=self.id, rid=self.rid, xid=self.xid, xml_node=copy(self.xml_node))
111 for sub in self.sub:
112 if isinstance(sub, StringElem):
113 cp.sub.append(sub.copy())
114 else:
115 cp.sub.append(sub.__class__(sub))
116 return cp
117
118
120 if not isinstance(tree, StringElem):
121 return tree
122
123 newtree = None
124
125 classmap = {
126 base.Bpt: Bpt,
127 base.Ept: Ept,
128 base.Ph: Ph,
129 base.It: It,
130 base.G: G,
131 base.Bx: Bx,
132 base.Ex: Ex,
133 base.X: X,
134 base.Sub: Sub
135 }
136 for baseclass, xliffclass in classmap.items():
137 if isinstance(tree, baseclass):
138 newtree = xliffclass()
139
140 if newtree is None:
141 newtree = tree.__class__()
142
143 newtree.id = tree.id
144 newtree.rid = tree.rid
145 newtree.xid = tree.xid
146 newtree.sub = []
147
148 for subtree in tree.sub:
149 newtree.sub.append(to_xliff_placeables(subtree))
150
151 return newtree
152
153
154 parsers = []
155