py_microhtml
PyPI release 0.2
使用Python代码安全构建有效的HTML。例子:
from microhtml import *
print (
ᑉhtml (
ᑉhead ( ᑉtitle ( 'Test page' )),
ᑉbody ( ᑉspan ( 'Simple example.' , class_ = 'example' ))). pretty ())这打印出有效的格式XHTML文档:
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
< html xmlns = " http://www.w3.org/1999/xhtml " >
< head >
< title >
Test page
</ title >
</ head >
< body >
< span class = " example " >Simple example.</ span >
</ body >
</ html >是的,您可能需要复制Unicode符号,是的,您需要使用现代编辑器,但是所得的语法非常紧凑,即使import *也不会与您的标识符相撞。
极简主义也扩展到实施 -非常短(v0.3中的80行)。查看microhtml/__init__.py 。
更长的示例展示了更多功能:
from microhtml import *
# Rendering a non-indented string (result: <p>Third <em>and last</em> paragraph</p>)
raw_html = str ( ᑉp ( "Third " , ᑉem ( "and last" ), ' paragraph' ))
# Writing a nicely formatted / tidied XHTML document to a file descriptor
print (
ᑉhtml ( lang = 'en_US' )(
ᑉhead ( ᑉtitle ( "Test page" )),
ᑉbody (
ᑉp ( "Hi!" , width = 123 ), # 123 becomes "123"
ᑉhr ( class_ = 'someclass' ), # Reserved words like "class" can be written with a trailing underscore
ᑉp ( 'Literal strings are safely <em>escaped</em> by default.' ),
ᑉrawstr ( raw_html ), # Use ᑉrawstr() if you don't want escaping
ᑉtag ( 'applet' , code = 'Bubbles.class' , width = 350 , height = 350 ), # Tag with custom name
ᑉdiv ( "custom" , data__custom = "abc" ), # '__' in attribute names is replaced with '-'
ᑉdiv ( style = 'float: right' )( # This is how you can type attributes on left and content on right
ᑉdiv ( style = 'border: 1px solid black' )(
ᑉa ( "Nested" , href = '#anchortest' ), '|' , 'link' )))). pretty ())这输出:
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
< html lang = " en_US " xmlns = " http://www.w3.org/1999/xhtml " xml : lang = " en_US " >
< head >
< title >
Test page
</ title >
</ head >
< body >
< p width = " 123 " >
Hi!
</ p >
< hr class = " someclass " />
< p >
Literal strings are safely < em > escaped < /em > by default.
</ p >
< p >
Third < em >and last</ em > paragraph
</ p >< applet code = " Bubbles.class " width = " 350 " height = " 350 " >
</ applet >
< div data-custom = " abc " >
custom
</ div >
< div style = " float: right " >
< div style = " border: 1px solid black " >
< a href = " #anchortest " >Nested</ a >|link
</ div >
</ div >
</ body >
</ html >要构建PYPI软件包,只需发行make 。它将安装VENV,运行测试,生成.pyi(类型/语法完成存根)并构建一个源软件包(SDIST)。使用make clean清洁。
如果您在源代码中发现Unicode字符是可怕的可恶,并且不要介意无休止的表情with ,那么您可能更喜欢Yattag。
使用tidylib进行漂亮的打印。来自CenkAltı的pyhtml的灵感。
版权所有2019 Jarno Elonen。根据MIT许可发布。有关详细信息,请参见许可证。