Construya de forma segura HTML válida con código Python. Ejemplo:
from microhtml import *
print (
ᑉhtml (
ᑉhead ( ᑉtitle ( 'Test page' )),
ᑉbody ( ᑉspan ( 'Simple example.' , class_ = 'example' ))). pretty ())Esto imprime un documento XHTML válido y formateado:
<! 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 > Sí, probablemente necesitará copiar el símbolo de Unicode y sí, querrá usar un editor moderno, pero la sintaxis resultante es muy compacta y no colisionará con sus identificadores incluso con la import * .
El minimalismo también se extiende a la implementación: es muy corto (<80 líneas en V0.3). Echa un vistazo a microhtml/__init__.py .
Ejemplo más largo con más características mostradas:
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 ())Esto sale:
<! 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 > Para construir un paquete PYPI, simplemente make la marca. Instalará un VenV, ejecutará pruebas, generará un .pyi (tipo de finalización de tipo/sintaxis) y construirá un paquete de origen (SDIST). Use make clean para limpiar.
Si encuentra que los personajes de Unicode en el código fuente son una horrible abominación, y no le importe interminable anidado with expresiones, es posible que prefiera Yattag.
Utiliza tidylib para una impresión bonita. Inspiración extraída de pyhtml por Cenk Altı.
Copyright 2019 Jarno Elonen. Liberado bajo la licencia MIT. Vea la licencia para más detalles.