For the Factor web server I want to have a way of generating HTML that looks HTML-ish and is easy to use. I've tried various approaches and the one I've settled with at the moment is in cont-html.factor. Loading this into Factor 0.60.3 will you let use it interactively:
"cont-html.factor" run-file
USE: cont-html
Once loaded you can write HTML to stdout by embedding HTML looking code:
<a href="http://www.double.co.nz/factor/cont-html.factor" a> [
"Link Text" write
] </a>
=> <a href="http://www.double.co.nz/factor/cont-html.factor">Link Text</a>
( action-url -- )
<form method="post" action="rot" form> [
<p> [ "Enter Some Text" write ] </p>
<input type="text" size="20" name="input1" input>
<input type="submit" value="Ok" input>
] </form>
=> <form>
<p>Enter Some Text</p>
<input type="text" size="20" name="input1">
<input type="submit" value="Ok">
</form>
This is actual Factor code that looks very HTML like and generates the HTML you'd expect. The code can also generate attribute tags dynamically instead of 'inline' as above. Things like '<input=' and 'size=' are words in Factor that set attributes, generate tags, etc as expected. The closing tags
like </form> write the output to standard out. This gives you some of the functionality that JSP's would giv you.
Prior to this I had code like '[ [ "text" write ] input ] form' which became a bit unreadable once definitions got fairly nested - mainly due to the tag name coming after the code block. I don't know if the above will work for more 'real world' stuff but it has a nice feel to it and so far works reasonably well. I think it's a nice test of an 'embedded language' inside of Factor anyway.
1:50:30 PM
|