def test_should_follow_default_behaviour(self): # we always have default `page` rules registered. rules = { 'page': lambda attrs: '<a href="/article/{}">'.format(attrs['id']) } rewriter = LinkRewriter(rules) page_type_link = rewriter('<a linktype="page" id="3">') self.assertEqual(page_type_link, '<a href="/article/3">') # but it should also be able to handle other supported # link types (email, external, anchor) even if no rules is provided external_type_link = rewriter('<a href="https://wagtail.io/">') self.assertEqual(external_type_link, '<a href="https://wagtail.io/">') email_type_link = rewriter('<a href="mailto:[email protected]">') self.assertEqual(email_type_link, '<a href="mailto:[email protected]">') anchor_type_link = rewriter('<a href="#test">') self.assertEqual(anchor_type_link, '<a href="#test">') # As well as link which don't have any linktypes link_without_linktype = rewriter('<a data-link="https://wagtail.io">') self.assertEqual(link_without_linktype, '<a data-link="https://wagtail.io">') # But should not handle if a custom linktype is mentioned but no # associate rules are registered. link_with_custom_linktype = rewriter( '<a linktype="custom" href="https://wagtail.io">') self.assertNotEqual(link_with_custom_linktype, '<a href="https://wagtail.io">') self.assertEqual(link_with_custom_linktype, '<a>')
def test_supported_type_should_follow_given_rules(self): # we always have `page` rules by default rules = { 'page': lambda attrs: '<a href="/article/{}">'.format(attrs['id']), 'external': lambda attrs: '<a rel="nofollow" href="{}">'.format(attrs['href']), 'email': lambda attrs: '<a data-email="true" href="{}">'.format(attrs['href']), 'anchor': lambda attrs: '<a data-anchor="true" href="{}">'.format(attrs['href']), 'custom': lambda attrs: '<a data-phone="true" href="{}">'.format(attrs['href']), } rewriter = LinkRewriter(rules) page_type_link = rewriter('<a linktype="page" id="3">') self.assertEqual(page_type_link, '<a href="/article/3">') # It should call appropriate rule supported linktypes (external or email) # based on the href value external_type_link = rewriter('<a href="https://wagtail.io/">') self.assertEqual(external_type_link, '<a rel="nofollow" href="https://wagtail.io/">') external_type_link_http = rewriter('<a href="http://wagtail.io/">') self.assertEqual(external_type_link_http, '<a rel="nofollow" href="http://wagtail.io/">') email_type_link = rewriter('<a href="mailto:[email protected]">') self.assertEqual(email_type_link, '<a data-email="true" href="mailto:[email protected]">') anchor_type_link = rewriter('<a href="#test">') self.assertEqual(anchor_type_link, '<a data-anchor="true" href="#test">') # But not the unsupported ones. link_with_no_linktype = rewriter('<a href="tel:+4917640206387">') self.assertEqual(link_with_no_linktype, '<a href="tel:+4917640206387">') # Also call the rule if a custom linktype is mentioned. link_with_custom_linktype = rewriter('<a linktype="custom" href="tel:+4917640206387">') self.assertEqual(link_with_custom_linktype, '<a data-phone="true" href="tel:+4917640206387">')
def html_rewriter(self): embed_rules = {} link_rules = {} for rule in self.converter_rules: if isinstance(rule, EmbedTypeRule): embed_rules[rule.embed_type] = rule.handler.expand_db_attributes elif isinstance(rule, LinkTypeRule): link_rules[rule.link_type] = rule.handler.expand_db_attributes return MultiRuleRewriter([LinkRewriter(link_rules), EmbedRewriter(embed_rules)])
def expand_db_html(html): """ Expand database-representation HTML into proper HTML usable on front-end templates """ global FRONTEND_REWRITER if FRONTEND_REWRITER is None: embed_rules = features.get_embed_types() link_rules = features.get_link_types() FRONTEND_REWRITER = MultiRuleRewriter( [LinkRewriter(link_rules), EmbedRewriter(embed_rules)]) return FRONTEND_REWRITER(html)
def expand_db_html(html): """ Expand database-representation HTML into proper HTML usable on front-end templates """ global FRONTEND_REWRITER if FRONTEND_REWRITER is None: embed_rules = features.get_embed_types() link_rules = features.get_link_types() FRONTEND_REWRITER = MultiRuleRewriter([ LinkRewriter({ linktype: handler.expand_db_attributes for linktype, handler in link_rules.items() }), EmbedRewriter({ embedtype: handler.expand_db_attributes for embedtype, handler in embed_rules.items() }) ]) return FRONTEND_REWRITER(html)