Created
December 17, 2015 13:53
-
-
Save simahawk/68ed9f7312b8e67053ad to your computer and use it in GitHub Desktop.
transforms to replace elements tag on the fly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <adapter | |
| provides="plone.outputfilters.interfaces.IFilter" | |
| name="b_to_strong" | |
| for="* *" | |
| factory=".transforms.BoldReplacer" | |
| /> | |
| <adapter | |
| provides="plone.outputfilters.interfaces.IFilter" | |
| name="i_to_em" | |
| for="* *" | |
| factory=".transforms.ItalicReplacer" | |
| /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| ############################################################# | |
| # For accessibility reason we cannot use <b> abd <i> elements | |
| # and our version of TinyMCE use them. | |
| # We replace them on the fly here. | |
| ############################################################# | |
| from lxml import html | |
| from lxml import etree | |
| from zope.interface import implementer | |
| from plone.outputfilters.interfaces import IFilter | |
| @implementer(IFilter) | |
| class ElementReplacer(object): | |
| """ replaces an element | |
| """ | |
| order = 600 | |
| to_replace_tag = '' | |
| replace_with_tag = '' | |
| def __init__(self, context=None, request=None): | |
| self.context = context | |
| self.request = request | |
| def is_enabled(self): | |
| return True | |
| def __call__(self, data): | |
| if not isinstance(data, unicode): | |
| data = data.decode("utf-8") | |
| tree = html.fragment_fromstring(data, create_parent=1) | |
| for node in tree.xpath('//' + self.to_replace_tag): | |
| # replace element | |
| replacement = etree.Element(self.replace_with_tag) | |
| has_children = False | |
| # keep children if any | |
| for child in node.getchildren(): | |
| replacement.append(child) | |
| has_children = True | |
| # no children? keep text | |
| if not has_children: | |
| replacement.text = node.text_content() | |
| # trailing stuff, preserve it! | |
| if node.tail: | |
| replacement.tail = node.tail | |
| node.getparent().replace(node, replacement) | |
| return html.tostring(tree) | |
| @implementer(IFilter) | |
| class BoldReplacer(ElementReplacer): | |
| """ replace `<b />` elements w/ `<strong />` | |
| """ | |
| order = 700 | |
| to_replace_tag = 'b' | |
| replace_with_tag = 'strong' | |
| @implementer(IFilter) | |
| class ItalicReplacer(ElementReplacer): | |
| """ replace `<i />` elements w/ `<em />` | |
| """ | |
| order = 700 | |
| to_replace_tag = 'i' | |
| replace_with_tag = 'em' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment