1 # -*- coding: utf-8 -*-
3 # This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
7 from collections import defaultdict
11 """Transforms XML to some text.
12 Used instead of XSLT which is difficult and cumbersome.
15 def __init__(self, options=None, state=None):
17 self.state = state or {}
19 self._options.append(options)
20 self.text_filters = []
21 self.escaped_text_filters = []
23 def register_text_filter(self, fun):
24 self.text_filters.append(fun)
26 def register_escaped_text_filter(self, fun):
27 self.escaped_text_filters.append(fun)
29 def filter_text(self, text):
30 for flt in self.text_filters:
35 # TODO: just work on the tree and let lxml handle escaping.
36 e = etree.Element("x")
38 # This whole mixing text with ML is so wrong.
39 output = etree.tostring(e, encoding=unicode)[3:-4]
40 for flt in self.escaped_text_filters:
44 def generate(self, document):
45 """Generate text from node using handlers defined in class."""
46 output = self._handle_element(document)
47 return u''.join([x for x in flatten(output) if x is not None])
51 """Returnes merged scoped options for current node.
53 # Here we can see how a decision not to return the modified map
54 # leads to a need for a hack.
55 return reduce(lambda a, b: a.update(b) or a, self._options, defaultdict(lambda: None))
58 def options(self, opts):
59 """Sets options overrides for current and child nodes
61 self._options.append(opts)
63 def _handle_for_element(self, element):
66 # from nose.tools import set_trace
68 if element.tag[0] == '{':
69 for nshort, nhref in element.nsmap.items():
71 if element.tag.index('{%s}' % nhref) == 0:
73 tagname = element.tag[len('{%s}' % nhref):]
78 raise ValueError("Strange ns for tag: %s, nsmap: %s" %
79 (element.tag, element.nsmap))
84 meth_name = "handle_%s__%s" % (ns, tagname)
86 meth_name = "handle_%s" % (tagname,)
88 handler = getattr(self, meth_name, None)
91 def next(self, element):
96 sibling = element.getnext()
97 if sibling is not None:
98 return sibling # found a new branch to dig into
99 element = element.getparent()
101 return None # end of tree
103 def _handle_element(self, element):
104 if isinstance(element, etree._Comment):
107 handler = self._handle_for_element(element)
108 if self.state.get('mute') and not getattr(handler, 'unmuter', False):
111 options_scopes = len(self._options)
114 pre = [self.filter_text(element.text)]
115 post = [self.filter_text(element.tail)]
117 vals = handler(element)
118 # depending on number of returned values, vals can be None, a value, or a tuple.
119 # how poorly designed is that? 9 lines below are needed just to unpack this.
121 return [self.filter_text(element.tail)]
123 if not isinstance(vals, tuple):
124 return [vals, self.filter_text(element.tail)]
126 pre = [vals[0], self.filter_text(element.text)]
127 post = [vals[1], self.filter_text(element.tail)]
129 out = pre + [self._handle_element(child) for child in element] + post
131 # clean up option scopes if necessary
132 self._options = self._options[0:options_scopes]
137 def tag_open_close(name_, classes_=None, **attrs):
138 u"""Creates tag beginning and end.
140 >>> tag_open_close("a", "klass", x=u"ą<")
141 (u'<a x="\\u0105<" class="klass">', u'</a>')
145 if isinstance(classes_, (tuple, list)):
146 classes_ = ' '.join(classes_)
147 attrs['class'] = classes_
149 e = etree.Element(name_)
151 for k, v in attrs.items():
153 pre, post = etree.tostring(e, encoding=unicode).split(u"> <")
154 return pre + u">", u"<" + post
157 def tag(name_, classes_=None, **attrs):
158 """Returns a handler which wraps node contents in tag `name', with class attribute
159 set to `classes' and other attributes according to keyword paramters
161 def _hnd(self, element):
162 return tag_open_close(name_, classes_, **attrs)
166 def tagged(name, classes=None, **attrs):
167 """Handler decorator which wraps handler output in tag `name', with class attribute
168 set to `classes' and other attributes according to keyword paramters
171 if isinstance(classes, (tuple, list)):
172 classes = ' '.join(classes)
173 attrs['class'] = classes
174 a = ''.join([' %s="%s"' % (k, v) for (k, v) in attrs.items()])
177 def _wrap(self, element):
182 prepend = "<%s%s>" % (name, a)
183 append = "</%s>" % name
185 if isinstance(r, tuple):
186 return prepend + r[0], r[1] + append
187 return prepend + r + append
192 def ifoption(**options):
193 """Decorator which enables node only when options are set
196 def _handler(self, *args, **kw):
198 for k, v in options.items():
201 return f(self, *args, **kw)
206 def flatten(l, ltypes=(list, tuple)):
207 """flatten function from BasicPropery/BasicTypes package
213 while isinstance(l[i], ltypes):