使用yaml的漂亮的html/xhtml/xml
yatom的Python项目详细描述
雅通
使用yaml的漂亮的html/xhtml/xml。
yatom是一个yaml到标记编译器,支持xml和html作为输出格式。
html和它的兄弟并不友好,它们非常复杂且常常不可读的特性(由于其父代:sgml)无法被超越,无论缩进和格式化都要付出多大的努力。即使这样,也需要进一步的后处理,以避免巨大的开销。
在某种程度上,yatom与jedi/pugjs和haml有着相同的概念,但是除非受到yaml的启发,否则yatom绝对是纯粹的yaml,完全不懂语言,没有额外的东西、要求或不兼容。
yaml-in,html-out,简单。
用法
使用yatom的简单html5页面。
importyatomsource=''' doctype: html5 html: head: title: My Yatom page! body: h2: Yatom is awesome p: >- Now, you can code your page with beautiful markup without worrying about inefficient HTML output nor erratic linebreak behavior thanks to Yatom. '''print(yatom.HTMLProcessor.from_source(source).render())
<!DOCTYPE html><html><head><title>My Yatom page!</title></head><body><h2>Yatom is awesome</h2><p>Now, you can code your page with beautiful markup without worrying about inefficient HTML output nor erratic linebreak behavior thanks to Yatom.</p></body></html>
yatom语法
yatom使用常规yaml,但这并不意味着文档可以是任意形状的。
yaml文档结构由目标语言定义,但大多数情况下都遵循以下规则:
- yaml根必须是映射,因为映射定义了文档结构。
- 如果遇到非映射值,则将其视为文本。
html/xhtml/xml
定义整个html文档的yaml语法非常简单。如果您已经知道html,您可以开始编写yatom模板,只需很少的规则:
- 映射键是标记名,但有一个例外:
- 键
doctype
的处理方式不同,其接受的值如下所示:- HTML4严格
- HTML4过渡
- HTML4框架集
- HTML5
- XHTML1严格
- XHTML1过渡
- XHTML1框架集
- XHTML11
- 键
- 映射字符串值被视为文本内容。
- 点前缀键是标记属性,必须出现在任何其他同级键之前,但有些例外:
- 。内联文本字符串的文本(转义)
- 。非转义内联文本字符串的文本(允许内联标记)
- .cdata表示XML CData标记
- .html注释的注释
除了以前的规则外,属性还支持嵌套,规则如下:
- 当样式值是映射时,属性被视为css属性,嵌套的映射键用破折号(
-
)连接。 - 当类值是映射或列表时,其属性将被视为不同的类(点是前缀)。它的层次结构(映射或数组)与破折号(
-
)组合在一起。 - 如果任何其他属性值是映射或列表,则其层次结构将与破折号(
-
)组合。
另外,只有在doctype需要时,很少有标记提供正常的默认值:
- HTML4和XHTML:
style
元素的属性type
默认为text/css
。script
元素的属性type
默认为application/javascript
。
- 仅XHTML:
html
元素同时具有默认的lang
和xmlns
。style
和script
元素的内容都被CDATA
标记自动包装。
应用语义html规则。
示例
HTML5的简单例子。
importyatomsource=''' doctype: html5 html: head: title: my page body: h2: my page p: | multiline text p: .text: mixed span: .style: color: red .text: tags .text: and strong: text '''print(yatom.HTMLProcessor.from_source(source).render())
<!DOCTYPE html><html><head><title>my page</title></head><body><h2>my page</h2><p>multiline text </p><p>mixed<spanstyle="color:red">tags</span>and<strong>text</strong></p></body></html>
更高级的xhtml4示例。
importyatomsource=''' doctype: xhtml11 html: head: title: my page script: window.alert('<hello world>') body: .data: something: 1 other: 2 .class: - simple: - nested - other .style: padding: top: 2em bottom: 2em left: 25% right: 25% p: some simple text '''print(yatom.HTMLProcessor.from_source(source).render())
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="en"lang="en"><head><title>my page</title><scripttype="application/javascript"><![CDATA[window.alert('<hello world>')]]></script></head><bodydata-something="1"data-other="2"class="simple-nested other"style="padding-top:2em;padding-bottom:2em;padding-left:25%;padding-right:25%"><p>some simple text</p></body></html>
路线图
- []漂亮的印花
- []可插入模板引擎或逻辑
- []把python2扔掉