Skip to content

Instantly share code, notes, and snippets.

@esmil
Created March 12, 2011 16:19
Show Gist options
  • Save esmil/867338 to your computer and use it in GitHub Desktop.
Save esmil/867338 to your computer and use it in GitHub Desktop.
Scary templating library..
local template = require 'template'
local html = template.new(function()
html{ xmlns = 'http://www.w3.org/1999/xhtml', lang = 'en', ['xml:lang'] = 'en',
head{
title 'Hathaway HTTP dump',
style{ type='text/css',
'th { text-align:left; }'
},
},
body{
h2 'Request',
table{
tr{ th 'Method', td{ __'method' }},
tr{ th 'Uri:', td{ __'uri' }},
tr{ th 'Version:', td{ __'version' }},
},
h2 'Headers',
table{
__spairs('headers', tr{ th{ __'k' }, td{ __'v' }}),
},
h2 'Body',
form{ action = '/form', method = 'post',
p{
textarea{ name = 'text', cols = 80, rows = 25 }, br{},
input{ type = 'submit', value = 'Submit' },
}
},
form{ action = '/quit', method = 'post',
p{
input{ type = 'hidden', name = 'quit', value = 'secret' },
input{ type = 'submit', value = 'Quit' }
}
}
}
}
end)
local req = {
method = 'GET',
uri = '/index.html',
version = '1.1',
headers = {
Host = 'localhost',
Connection = 'keep-alive',
}
}
html:pp(io.write, req)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Hathaway HTTP dump</title>
<style type="text/css">th { text-align:left; }</style>
</head>
<body>
<h2>Request</h2>
<table>
<tr>
<th>Method</th>
<td>GET</td>
</tr>
<tr>
<th>Uri:</th>
<td>/index.html</td>
</tr>
<tr>
<th>Version:</th>
<td>1.1</td>
</tr>
</table>
<h2>Headers</h2>
<table>
<tr>
<th>Connection</th>
<td>keep-alive</td>
</tr>
<tr>
<th>Host</th>
<td>localhost</td>
</tr>
</table>
<h2>Body</h2>
<form action="/form" method="post">
<p>
<textarea rows="25" name="text" cols="80">
</textarea>
<br />
<input value="Submit" type="submit" />
</p>
</form>
<form action="/quit" method="post">
<p>
<input value="secret" type="hidden" name="quit" />
<input value="Quit" type="submit" />
</p>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment