自定义 Python 支配标记元素
Custom Python Dominate Tag Element
致 support both a JPEG and WEBP compressed image,我想在网页中包含以下 HTML 代码:
<picture>
<source srcset="img/awesomeWebPImage.webp" type="image/webp">
<source srcset="img/creakyOldJPEG.jpg" type="image/jpeg">
<img src="img/creakyOldJPEG.jpg" alt="Alt Text!">
</picture>
我一直在使用 Python Dominate,它对我来说通常效果很好。
但是我认为Dominate不支持Picture和Source标签。
我可以将 HTML 添加为 raw() Dominate 标签,但想知道是否有办法让 Dominate 识别这些标签。
p = picture()
with p:
source(srcset=image.split('.')[0]+'.webp', type="image/webp")
source(srcset=image, type="image/jpeg")
img(src=image, alt=imagealt)
我看到了这种错误:
p = picture()
NameError: global name 'picture' is not defined
Dominate 用于生成 HTML(5) 个文档。
元素列表在 tags.py
文件中定义,参见 GitHub 中的存储库:https://github.com/Knio/dominate/blob/master/dominate/tags.py。
但是,picture
不是标准标签。
您可以查看包含 ElementMaker
类似于 Dominate 的 lxml 库,以便轻松构建 XML 树。见 E-Factory.
例如:
>>> from lxml.builder import E
>>> def CLASS(*args): # class is a reserved word in Python
... return {"class":' '.join(args)}
>>> html = page = (
... E.html( # create an Element called "html"
... E.head(
... E.title("This is a sample document")
... ),
... E.body(
... E.h1("Hello!", CLASS("title")),
... E.p("This is a paragraph with ", E.b("bold"), " text in it!"),
... E.p("This is another paragraph, with a", "\n ",
... E.a("link", href="http://www.python.org"), "."),
... E.p("Here are some reserved characters: <spam&egg>."),
... etree.XML("<p>And finally an embedded XHTML fragment.</p>"),
... )
... )
... )
>>> print(etree.tostring(page, pretty_print=True))
<html>
<head>
<title>This is a sample document</title>
</head>
<body>
<h1 class="title">Hello!</h1>
<p>This is a paragraph with <b>bold</b> text in it!</p>
<p>This is another paragraph, with a
<a href="http://www.python.org">link</a>.</p>
<p>Here are some reserved characters: <spam&egg>.</p>
<p>And finally an embedded XHTML fragment.</p>
</body>
</html>
您可以通过继承 dominate.tags.html_tag
class
创建图片 class
from dominate.tags import html_tag
class picture(html_tag):
pass
这现在可以用作任何预定义标签。
致 support both a JPEG and WEBP compressed image,我想在网页中包含以下 HTML 代码:
<picture>
<source srcset="img/awesomeWebPImage.webp" type="image/webp">
<source srcset="img/creakyOldJPEG.jpg" type="image/jpeg">
<img src="img/creakyOldJPEG.jpg" alt="Alt Text!">
</picture>
我一直在使用 Python Dominate,它对我来说通常效果很好。 但是我认为Dominate不支持Picture和Source标签。 我可以将 HTML 添加为 raw() Dominate 标签,但想知道是否有办法让 Dominate 识别这些标签。
p = picture()
with p:
source(srcset=image.split('.')[0]+'.webp', type="image/webp")
source(srcset=image, type="image/jpeg")
img(src=image, alt=imagealt)
我看到了这种错误:
p = picture()
NameError: global name 'picture' is not defined
Dominate 用于生成 HTML(5) 个文档。
元素列表在 tags.py
文件中定义,参见 GitHub 中的存储库:https://github.com/Knio/dominate/blob/master/dominate/tags.py。
但是,picture
不是标准标签。
您可以查看包含 ElementMaker
类似于 Dominate 的 lxml 库,以便轻松构建 XML 树。见 E-Factory.
例如:
>>> from lxml.builder import E
>>> def CLASS(*args): # class is a reserved word in Python
... return {"class":' '.join(args)}
>>> html = page = (
... E.html( # create an Element called "html"
... E.head(
... E.title("This is a sample document")
... ),
... E.body(
... E.h1("Hello!", CLASS("title")),
... E.p("This is a paragraph with ", E.b("bold"), " text in it!"),
... E.p("This is another paragraph, with a", "\n ",
... E.a("link", href="http://www.python.org"), "."),
... E.p("Here are some reserved characters: <spam&egg>."),
... etree.XML("<p>And finally an embedded XHTML fragment.</p>"),
... )
... )
... )
>>> print(etree.tostring(page, pretty_print=True))
<html>
<head>
<title>This is a sample document</title>
</head>
<body>
<h1 class="title">Hello!</h1>
<p>This is a paragraph with <b>bold</b> text in it!</p>
<p>This is another paragraph, with a
<a href="http://www.python.org">link</a>.</p>
<p>Here are some reserved characters: <spam&egg>.</p>
<p>And finally an embedded XHTML fragment.</p>
</body>
</html>
您可以通过继承 dominate.tags.html_tag
class
from dominate.tags import html_tag
class picture(html_tag):
pass
这现在可以用作任何预定义标签。