public
Last active

  • Download Gist
gistfile1.io
Io
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
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>
*/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.