Source code for hyperpython.components.hyperlinks

import collections

from ..core import Element
from ..html import html
from ..tags import a, p, span, h, ul, li
from ..utils import escape, lazy_singledispatch
from ..utils.role_dispatch import role_singledispatch


[docs]def a_or_p(*args, href=None, **kwargs): """ Return a or p tag depending if href is defined or not. """ if href: return a(*args, href=href, **kwargs) else: return p(*args, href=href, **kwargs)
[docs]def a_or_span(*args, href=None, **kwargs): """ Return a or span tag depending if href is defined or not. """ if href: return a(*args, href=href, **kwargs) else: return span(*args, **kwargs)
@hyperlink.register(str) def _hyperlink_str(data, href=None, **attrs): if href is None: data, href = split_link(data) if not href: raise ValueError('string does not declare a target url') attrs['href'] = href return h('a', attrs, escape(data)) @hyperlink.register(collections.Mapping) def _hyperlink_map(obj, **attrs): content = obj['content'] if 'href' not in obj: raise ValueError('mapping must define an href key.') for k, v in obj.items(): if k != 'content': attrs.setdefault(k, v) return h('a', attrs, content) @hyperlink.register('django.db.models.Model') def _hyperlink_model(x, **attrs): attrs['href'] = attrs.get('href') or x.get_absolute_url() body = html(x) return h('a', attrs, body)
[docs]@role_singledispatch def url(obj): """ Returns a url for the given object. """ if hasattr(obj, '__url__'): return obj.__url__() if hasattr(obj, 'get_absolute_url'): return obj.get_absolute_url() raise TypeError(f'no methods registered for {type(obj).__class__}')
def split_link(name): """ Return a tuple with (name, link) from a string of "name<link>". If no link is found, the second value is None. """ if name.endswith('>'): name, sep, link = name.partition('<') if sep: return name.rstrip(), link[:-1] return name, None