Skip to content

Instantly share code, notes, and snippets.

@Duologic
Created February 6, 2023 11:06
Show Gist options
  • Save Duologic/9104e20eb013a37e4d06c2aba71fe558 to your computer and use it in GitHub Desktop.
Save Duologic/9104e20eb013a37e4d06c2aba71fe558 to your computer and use it in GitHub Desktop.
Write HTML documents with Jsonnet using JsonML
local element = {
new(tagName, attributes={}): {
tagName: tagName,
attributes: attributes,
elementList: [],
render()::
[
self.tagName,
self.attributes,
'\n',
]
+ [
if std.mod(i, 2) == 0
then
local el = self.elementList[(i / 2)];
if std.isObject(el) && 'render' in el
then self.elementList[(i / 2)].render()
else el
else '\n'
for i in std.range(0, (std.length(self.elementList) * 2) - 1)
],
manifest()::
std.manifestXmlJsonml(
self.render()
),
},
addElements(el): {
elementList+:
if std.isArray(el)
then el
else [el],
},
};
local title =
element.new('title')
+ element.addElements('This is a title');
local pageTitle =
element.new('h1')
+ element.addElements('This is a page title');
local doc =
element.new('html')
+ element.addElements([
element.new('head')
+ element.addElements([title]),
element.new('body')
+ element.addElements([pageTitle]),
]);
doc.manifest()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment