def custom_tag(tag_name, class_name=None, flattener=flatten_tag, attrs=None): """ class factory for defining tags with custom type (i.e. not of class Tag) """ if not class_name: class_name = tag_name if not attrs: attrs = {} def __init__(self, *args, **kw): Tag.__init__(self, tag_name, **attrs) TagClass = type("c_%sTag" % class_name, (Tag, ), {'__init__': __init__}) ProtoClass = type("c_%sProto" % class_name, (Proto, ), {'Class': TagClass}) register_flattener(ProtoClass, lambda o: flattener(o())) register_flattener(TagClass, flattener) return ProtoClass(tag_name)
# a class to handle empty tags in html4 format class Html4EmptyTag(Tag): pass def flatten_empty_html4_tag(o): if o.render: o = o.render(o, o.data) attrs = u''.join(quoteattrs(o.attrs)) return u'<%s%s>' % (o.name, attrs) register_flattener(Html4EmptyTag, flatten_empty_html4_tag) class Html4EmptyProto(HtmlProto): Class = Html4EmptyTag pass def flatten_html4_empty_proto(p): return '<%s>' % (p) register_flattener(Html4EmptyProto, flatten_html4_empty_proto) tags = Namespace()
from liphtePy.util import Namespace class Entity(str): def __init__(self, args): self.name, self.value = args def __repr__(self): return self.name def flatten_entity(e): return e.value register_flattener(Entity, flatten_entity) entities = Namespace(dict( [(name, Entity((name, "&#%s;" % value))) for name, value in [ ('nbsp', '160'), # non-breaking space ('cent', '162'), # cent sign ('pound', '163'), # pound sign ('curren', '164'), # currency sign ('yen', '165'), # yen sign ('brvbar', '166'), # broken vertical bar ('sect', '167'), # section sign ('uml', '168'), # diaeresis ('copy', '169'), # copyright sign ('ordf', '170'), # feminine ordinal indicator ('laquo', '171'), # left-pointing double angle quotation mark
return u'<%s />' % p def flatten_sequence(o): return u''.join([flatten(i) for i in o]) def flatten_callable(o): return flatten(o()) def flatten_macro(o): return u'' register_flattener(list, flatten_sequence) register_flattener(tuple, flatten_sequence) register_flattener(Proto, flatten_proto) register_flattener(Tag, flatten_tag) #register_flattener ( str, lambda s: escape ( unicode ( s, 'utf-8' ) ) ) register_flattener(str, escape) register_flattener(Invisible, flatten_invisible) register_flattener(cdata, str) register_flattener(comment, flatten_comment) register_flattener(xml, flatten_xml) register_flattener(type(lambda: None), flatten_callable) register_flattener(Macro, flatten_macro) def custom_tag(tag_name, class_name=None, flattener=flatten_tag, attrs=None): """ class factory for defining tags with custom type (i.e. not of class Tag) """
# class HtmlProto(str): __slots__ = [] def __call__(self, **kw): return Tag(self)(**kw) def __getitem__(self, children): return Tag(self)[children] def flatten_htmlproto(p): return '<%s></%s>' % (p, p) register_flattener(HtmlProto, flatten_htmlproto) # # register common HTML elements # tag_names = [ 'a', 'abbr', 'address', 'article', 'aside', 'audio', 'b', 'bdi', 'bdo', 'blockquote',