用python表示html的库
pyhiccup的Python项目详细描述
小嗝0.1
python版本的clojure hiccuphttps://github.com/weavejester/hiccup 詹姆斯·里夫斯的原创概念
pyhiccup是一个用python表示html的库。它使用列表或元组 表示元素,dict表示元素的属性。 支持的python版本是:
- 2.6
- 2.7
- 3.3
- 3.4
安装
pip install pyhiccup
语法
下面是pyhiccup语法的一个基本示例。
>>>frompyhiccup.coreimporthtml>>>data=[>>>['div',>>>{'class':'a-class','data-y':'23'},>>>['span','my-text',>>>['ul',[['li',x]forxin['café','milk','sugar']]]]]>>>]>>>html(data)u'<!DOCTYPE html><html lang="en" xml:lang="en" dir="rtl"><div data-y="23" class="a-class"><span>my-text<ul><li>café<li>milk<li>sugar</ul></span></div></html>'
html函数支持不同的默认类型html5、html4、xhtml strict、xhtml transitional
>>>frompyhiccup.coreimporthtml>>>data=[]>>>html(data,etype='xhtml-strict')>>>u'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html lang="en" xml:lang="en" dir="rtl" xmlns="http://www.w3.org/1999/xhtml"/>'
您可以将任意关键字参数传递给html它们将被转换为html标记属性
>>>frompyhiccup.coreimporthtml>>>data=[]>>>html(data,etype='xhtml-strict',an-attr='foo')u'... <html an-attr="foo" lang="en" xml:lang="en" dir="rtl" xmlns="http://www.w3.org/1999/xhtml"/>'
pyhiccup还提供了一个表示xml的函数。也支持任意关键字参数。
>>>frompyhiccup.coreimportxml>>>data=['form-desc',>>>['field',{'name':'a_name'}],>>>['field',{'name':'a_other_name'}]]>>>conv=xml(data,'foo-ns',bar='an_attr')u'<?xml version="1.0" encoding="UTF-8"?><foo-ns bar="an_attr"><form-desc><field name="a_name"/><field name="a_other_name"/></form-desc></foo-ns>'
有时您希望能够从命名空间中创建xml/html块。core.convert就是为此而制作的。
>>>frompyhiccup.coreimportconvert>>>frompyhiccup.elementimportlink_to>>>convert(link_to('http://github.com/nbessi/pyhiccup','pyhiccup'))u'<a href="http://github.com/nbessi/pyhiccup">pyhiccup</a>'
元素名称空间中提供了帮助程序。将帮助您添加超链接、图像等。
>>>frompyhiccup.elementimportlink_to>>>link_to(u'https://github.com/nbessi/pyhiccup',u'pyhiccup')[u'a',{u'href':u'https://github.com/nbessi/pyhiccup'},u'pyhiccup']