Beautiful HTML/XHTML/XML using YAML
Beautiful HTML/XHTML/XML using YAML.
Yatom is a YAML to markup compiler supporting XML and HTML as output formats.
HTML and its siblings are not friendly at all, their very convoluted and often unreadable nature (due its parent: SGML) cannot be overtaken regardless on how much effort is put on both indentation and formatting. And even that requires further postprocessing to avoid the a huge size overhead.
In some way, Yatom shares the same concept as jedi/pugjs and HAML, but unless being yaml-inspiredTM, Yatom is absolutely pure YAML, completely language agnostic, without extra stuff, requirements or incompatibilities.
YAML in, HTML out, simple.
Simple HTML5 page using Yatom.
import yatom
source = '''
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 uses regular YAML, but that doesn't mean documents can of arbitrary shape.
The YAML document structure is defined by the target language, but mostly all follow the following rules:
The YAML syntax defining an entire HTML document is quite simple. If you already know HTML you can start writing Yatom templates with very few rules:
doctype
is handled differently, its accepted values are listed here:
In addition to previous rules, attributes support nesting, with the following rules:
-
).-
).-
).And as a bonus, and only if required by doctype, few tags provide sane defaults:
style
element's attribute type
defaults to text/css
.script
element's attribute type
defaults to application/javascript
.html
element has both default lang
and xmlns
.style
and script
element's content is automatically wrapped with CDATA
tags.Semantic HTML rules are applied.
Simple example for HTML5.
import yatom
source = '''
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<span style="color:red">tags</span>and<strong>text</strong></p></body></html>
More advanced XHTML4 example.
import yatom
source = '''
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">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>my page</title><script type="application/javascript"><![CDATA[window.alert('<hello world>')]]></script></head><body data-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>