بناء 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 * .
يمتد الحد الأدنى أيضًا إلى التنفيذ - إنه قصير جدًا (<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 (كعب إكمال النوع/بناء الجملة) وإنشاء حزمة مصدر (SDIST). استخدام make clean لتنظيف.
إذا وجدت أحرف Unicode في الكود المصدري ، فالأسال الرهيبة ، ولا تمانع في التداخل with التعبيرات ، فقد تفضل Yattag.
يستخدم tidylib للطباعة الجميلة. إلهام مستمد من pyhtml بواسطة Cenk Altı.
حقوق الطبع والنشر 2019 Jarno Elonen. صدر تحت رخصة معهد ماساتشوستس للتكنولوجيا. انظر الترخيص للحصول على التفاصيل.