สร้าง HTML ที่ถูกต้องอย่างปลอดภัยด้วยรหัส Python ตัวอย่าง:
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 *
Minimalism ยังขยายไปถึงการใช้งาน - มันสั้น มาก (<80 บรรทัดใน v0.3) ตรวจสอบ 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 (type/syntax complection stub) และสร้างแพ็คเกจต้นทาง (SDIST) ใช้ make clean เพื่อทำความสะอาด
หากคุณพบว่าอักขระ Unicode ในซอร์สโค้ดน่ารังเกียจน่ากลัวและไม่รังเกียจที่จะซ้อนกันอย่างไม่มีที่สิ้นสุด with การแสดงออกคุณอาจชอบ Yattag
ใช้ tidylib สำหรับการพิมพ์สวย แรงบันดาลใจที่ดึงมาจาก pyhtml โดย Cenk Altı
ลิขสิทธิ์ 2019 Jarno Elonen ปล่อยภายใต้ใบอนุญาต MIT ดูใบอนุญาตสำหรับรายละเอียด