X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/3ffce80e2f1c58f741cd6a3d31aa86199e8874f1..d9766370d8eee5c067756af3c06a4b6ab6583d0e:/librarian/xmlutils.py?ds=sidebyside diff --git a/librarian/xmlutils.py b/librarian/xmlutils.py index 97a3039..bbcc884 100644 --- a/librarian/xmlutils.py +++ b/librarian/xmlutils.py @@ -12,21 +12,35 @@ class Xmill(object): Used instead of XSLT which is difficult and cumbersome. """ - def __init__(self, options=None): + def __init__(self, options=None, state=None): self._options = [] + self.state = state or {} if options: self._options.append(options) self.text_filters = [] + self.escaped_text_filters = [] def register_text_filter(self, fun): self.text_filters.append(fun) + def register_escaped_text_filter(self, fun): + self.escaped_text_filters.append(fun) + def filter_text(self, text): for flt in self.text_filters: if text is None: return None - text = flt(text) - return text + else: + text = flt(text) + # TODO: just work on the tree and let lxml handle escaping. + e = etree.Element("x") + e.text = text + # This whole mixing text with ML is so wrong. + output = etree.tostring(e, encoding=unicode)[3:-4] + for flt in self.escaped_text_filters: + output = flt(output) + return output + def generate(self, document): """Generate text from node using handlers defined in class.""" @@ -39,7 +53,7 @@ class Xmill(object): """ # Here we can see how a decision not to return the modified map # leads to a need for a hack. - return reduce(lambda a, b: a.update(b) or a, self._options, defaultdict(lambda: False)) + return reduce(lambda a, b: a.update(b) or a, self._options, defaultdict(lambda: None)) @options.setter def options(self, opts): @@ -90,6 +104,7 @@ class Xmill(object): if isinstance(element, etree._Comment): return None handler = self._handle_for_element(element) + if self.state.get('mute') and not getattr(handler, 'unmuter', False): return None # How many scopes try: options_scopes = len(self._options) @@ -114,6 +129,7 @@ class Xmill(object): finally: # clean up option scopes if necessary self._options = self._options[0:options_scopes] + return out