"""
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF ORIN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
class HtmlTag(Tag): def unicode(self, indent=2):
l = []
HtmlVisitor(l.append, indent, shortempty=False).visit(self) return u("").join(l)
# exported plain html namespace
class html(Namespace):
__tagclass__ = HtmlTag
__stickyname__ = True
__tagspec__ = dict(
[
(x, 1) for x in ( "a,abbr,acronym,address,applet,area,b,bdo,big,blink," "blockquote,body,br,button,caption,center,cite,code,col," "colgroup,comment,dd,del,dfn,dir,div,dl,dt,em,embed," "fieldset,font,form,frameset,h1,h2,h3,h4,h5,h6,head,html," "i,iframe,img,input,ins,kbd,label,legend,li,link,listing," "map,marquee,menu,meta,multicol,nobr,noembed,noframes," "noscript,object,ol,optgroup,option,p,pre,q,s,script," "select,small,span,strike,strong,style,sub,sup,table," "tbody,td,textarea,tfoot,th,thead,title,tr,tt,u,ul,xmp," "base,basefont,frame,hr,isindex,param,samp,var"
).split(",") if x
]
)
class Style(object): def __init__(self, **kw): for x, y in kw.items():
x = x.replace("_", "-")
setattr(self, x, y)
class raw(object): """just a box that can contain a unicode string that will be
included directly in the output"""
def __init__(self, uniobj):
self.uniobj = uniobj
class SimpleUnicodeVisitor(object): """recursive visitor to write unicode."""
def __init__(self, write, indent=0, curindent=0, shortempty=True):
self.write = write
self.cache = {}
self.visited = {} # for detection of recursion
self.indent = indent
self.curindent = curindent
self.parents = []
self.shortempty = shortempty # short empty tags or not
def visit(self, node): """dispatcher on node's class/bases name."""
cls = node.__class__ try:
visitmethod = self.cache[cls] except KeyError: for subclass in cls.__mro__:
visitmethod = getattr(self, subclass.__name__, None) if visitmethod isnotNone: break else:
visitmethod = self.__object
self.cache[cls] = visitmethod
visitmethod(node)
# the default fallback handler is marked private # to avoid clashes with the tag name object def __object(self, obj): # self.write(obj)
self.write(escape(unicode(obj)))
def raw(self, obj):
self.write(obj.uniobj)
def list(self, obj): assert id(obj) notin self.visited
self.visited[id(obj)] = 1 for elem in obj:
self.visit(elem)
def attributes(self, tag): # serialize attributes
attrlist = dir(tag.attr)
attrlist.sort()
l = [] for name in attrlist:
res = self.repr_attribute(tag.attr, name) if res isnotNone:
l.append(res)
l.extend(self.getstyle(tag)) return u("").join(l)
def repr_attribute(self, attrs, name): if name[:2] != "__":
value = getattr(attrs, name) if name.endswith("_"):
name = name[:-1] if isinstance(value, raw):
insert = value.uniobj else:
insert = escape(unicode(value)) return' %s="%s"' % (name, insert)
def getstyle(self, tag): """return attribute list suitable for styling.""" try:
styledict = tag.style.__dict__ except AttributeError: return [] else:
stylelist = [x + ": " + y for x, y in styledict.items()] return [u(' style="%s"') % u("; ").join(stylelist)]
def _issingleton(self, tagname): """can (and will) be overridden in subclasses""" return self.shortempty
def _isinline(self, tagname): """can (and will) be overridden in subclasses""" returnFalse
class HtmlVisitor(SimpleUnicodeVisitor):
single = dict(
[
(x, 1) for x in ("br,img,area,param,col,hr,meta,link,base,""input,frame").split( ","
)
]
)
inline = dict(
[
(x, 1) for x in ( "a abbr acronym b basefont bdo big br cite code dfn em font " "i img input kbd label q s samp select small span strike " "strong sub sup textarea tt u var".split(" ")
)
]
)
def repr_attribute(self, attrs, name): if name == "class_":
value = getattr(attrs, name) if value isNone: return return super(HtmlVisitor, self).repr_attribute(attrs, name)
def _issingleton(self, tagname): return tagname in self.single
def _isinline(self, tagname): return tagname in self.inline
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.