qpython3h怎么用_可以在python3下面用的pyh

#@file: pyh.py#@purpose: a HTML tag generator#@author: Emmanuel Turlay

__doc__ = """The pyh.py module is the core of the PyH package. PyH lets you

generate HTML tags from within your python code.

See http://code.google.com/p/pyh/ for documentation."""

__author__ = "Emmanuel Turlay "

__version__ = '$Revision: 43 $'

__date__ = '$Date$'

from sys import_getframe, stdout, modules, version

nOpen={}

nl= '\n'doctype= '\n'charset= '\n'tags= ['html', 'body', 'head', 'link', 'meta', 'div', 'p', 'form', 'legend','input', 'select', 'span', 'b', 'i', 'option', 'img', 'script','table', 'tr', 'td', 'th', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6','fieldset', 'a', 'title', 'body', 'head', 'title', 'script', 'br', 'table']

selfClose= ['input', 'img', 'link', 'br']classTag(list):

tagname= ''

def __init__(self, *arg, **kw):

self.attributes=kwifself.tagname :

name=self.tagname

self.isSeq=Falseelse:

name= 'sequence'self.isSeq=True

self.id= kw.get('id', name)#self.extend(arg)

for a inarg: self.addObj(a)def __iadd__(self, obj):if isinstance(obj, Tag) andobj.isSeq:for o inobj: self.addObj(o)else: self.addObj(obj)returnselfdefaddObj(self, obj):if not isinstance(obj, Tag): obj =str(obj)

id=self.setID(obj)

setattr(self, id, obj)

self.append(obj)defsetID(self, obj):ifisinstance(obj, Tag):

id=obj.id

n= len([t for t in self if isinstance(t, Tag) andt.id.startswith(id)])else:

id= 'content'n= len([t for t in self if notisinstance(t, Tag)])if n: id = '%s_%03i' %(id, n)if isinstance(obj, Tag): obj.id =idreturniddef __add__(self, obj):if self.tagname: returnTag(self, obj)

self.addObj(obj)returnselfdef __lshift__(self, obj):

self+=objreturnobjdefrender(self):

result= ''

ifself.tagname:

result= '<%s%s%s>' % (self.tagname, self.renderAtt(), self.selfClose()*'/')if notself.selfClose():for c inself:ifisinstance(c, Tag):

result+=c.render()else: result +=cifself.tagname:

result+= '%s>' %self.tagname

result+= '\n'

returnresultdefrenderAtt(self):

result= ''

for n, v inself.attributes.items():if n != 'txt' and n != 'open':if n == 'cl': n = 'class'result+= '%s="%s"' %(n, v)returnresultdefselfClose(self):return self.tagname inselfClosedefTagFactory(name):classf(Tag):

tagname=name

f.__name__ =namereturnf

thisModule= modules[__name__]for t intags: setattr(thisModule, t, TagFactory(t))defValidW3C():

out= a(img(src='http://www.w3.org/Icons/valid-xhtml10', alt='Valid XHTML 1.0 Strict'), href='http://validator.w3.org/check?uri=referer')returnoutclassPyH(Tag):

tagname= 'html'

def __init__(self, name='MyPyHPage'):

self+=head()

self+=body()

self.attributes= dict(xmlns='http://www.w3.org/1999/xhtml', lang='en')

self.head+=title(name)def __iadd__(self, obj):if isinstance(obj, head) orisinstance(obj, body): self.addObj(obj)elif isinstance(obj, meta) or isinstance(obj, link): self.head +=objelse:

self.body+=obj

id=self.setID(obj)

setattr(self, id, obj)returnselfdef addJS(self, *arg):for f in arg: self.head += script(type='text/javascript', src=f)def addCSS(self, *arg):for f in arg: self.head += link(rel='stylesheet', type='text/css', href=f)def printOut(self,file=''):if file: f = open(file, 'w')else: f =stdout

f.write(doctype)

f.write(self.render())

f.flush()iffile: f.close()classTagCounter:

_count={}

_lastOpen=[]for t in tags: _count[t] =0def __init__(self, name):

self._name=namedefopen(self, tag):ifisLegal(tag):

self._count[tag]+= 1self._lastOpen+=[tag]defclose(self, tag):if isLegal(tag) and self._lastOpen[-1] ==tag:

self._count[tag]-= 1self._lastOpen.pop()else:print('Cross tagging is wrong')defisAllowed(self, tag, open):if not open andself.isClosed(tag):print('TRYING TO CLOSE NON-OPEN TAG: %s' %tag)returnFalsereturnTruedefisOpen(self, tag):if isLegal(tag): returnself._count[tag]defisClosed(self, tag):if isLegal(tag): return notself._count[tag]defisLegal(tag):if tag in tags: returnTrueelse:print('ILLEGAL TAG: %s' %tag)return False

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值