Skip to content

Instantly share code, notes, and snippets.

@richcollins
Created May 20, 2009 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richcollins/114692 to your computer and use it in GitHub Desktop.
Save richcollins/114692 to your computer and use it in GitHub Desktop.
HtmlNode := Object clone do(
name ::= "div"
init := method(
self attributes := Map clone
self children := List clone
)
asString := method(
appendHtmlTo(Sequence clone, 0)
)
appendHtmlTo := method(aSeq, depth,
prefix := "\t" repeated(depth)
aSeq appendSeq(prefix .. "<" .. name)
if(attributes keys size > 0,
aSeq appendSeq(" " .. attributes map(k, v, k .. "=\"" .. v .. "\"") join("="))
)
if(children size > 0) then(
aSeq appendSeq(">\n")
self children foreach(appendHtmlTo(aSeq, depth + 1))
aSeq appendSeq(prefix .. "</" .. name .. ">\n")
) else(
aSeq appendSeq("/>\n")
)
aSeq
)
appendChild := method(aNode,
self children append(aNode)
)
)
TextNode := Object clone do(
name ::= "textNode"
text ::= ""
with := method(text,
TextNode clone setText(text)
)
appendHtmlTo := method(aSeq, depth,
aSeq appendSeq("\t" repeated(depth) .. text .. "\n")
)
asString := method(
text
)
)
HtmlGenerator := Object clone do(
tagNames := "html head title body div img a" split
inTag ::= false
resetHtml := method(
self tagStack := List clone
)
tag := method(tagName,
parent := tagStack last
child := HtmlNode clone setName(tagName)
tagStack append(child)
if(parent, parent appendChild(child))
results := call evalArgs
if(call message arguments last cachedResult type == "Sequence",
text(results last)
)
tagStack removeLast
child
)
attr := method(name, value,
tagStack last attributes atPut(name, value)
)
tagNames foreach(tagName,
setSlot(tagName,
method(
self doMessage(
Message clone setName("tag") setArguments(call message arguments prepend(Message clone setCachedResult(call message name)))
)
)
)
)
text := method(text,
parent := tagStack last
child := TextNode with(text)
if(parent, parent children append(child))
child
)
)
Test := Object clone do(
appendProto(HtmlGenerator)
init := method(
resetHtml
)
document := method(
html(
head(
title("Stylous")
body(
div(
img(src:"resources/Logo.png")
)
div(
a(href:"/", "Shop")
a(href:"/p/favorites", "Favorites")
a(href:"/p/feedback", "Feedback")
a(href:"/p/about", "About")
)
div(
div(
div(
a(href:"/p/shoes")
img(src:"resources/MenuImages/shoes.jpg")
)
div(
a(href:"/p/handbags")
img(src:"resources/MenuImages/handbags.jpg")
)
div(
a(href:"/p/jewelry")
img(src:"resources/MenuImages/jewelry.jpg")
)
div(
a(href:"/p/watches")
img(src:"resources/MenuImages/watches.jpg")
)
)
)
)
)
)
)
)
writeln(Test clone document)
/*
<html>
<head>
<title>
Stylous
</title>
<body>
<div>
<img src="resources/Logo.png"/>
</div>
<div>
<a href="/">
Shop
</a>
<a href="/p/favorites">
Favorites
</a>
<a href="/p/feedback">
Feedback
</a>
<a href="/p/about">
About
</a>
</div>
<div>
<div>
<div>
<a href="/p/shoes"/>
<img src="resources/MenuImages/shoes.jpg"/>
</div>
<div>
<a href="/p/handbags"/>
<img src="resources/MenuImages/handbags.jpg"/>
</div>
<div>
<a href="/p/jewelry"/>
<img src="resources/MenuImages/jewelry.jpg"/>
</div>
<div>
<a href="/p/watches"/>
<img src="resources/MenuImages/watches.jpg"/>
</div>
</div>
</div>
</body>
</head>
</html>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment