-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml.ur
203 lines (193 loc) · 11 KB
/
html.ur
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
196
197
198
199
200
201
202
203
datatype parse a =
Success of a
| Failure of string
con attribute = fn t => {Nam : string,
Parse : string -> option t}
con tag = fn ts => {Nam : string,
Attributes : $(map attribute ts),
Folder : folder ts,
Construct : ctx ::: {Unit} -> [[Body] ~ ctx] => $ts
-> xml ([Body] ++ ctx) [] [] -> xml ([Body] ++ ctx) [] []}
fun tag [use] [ignore] [use ~ ignore] (fl : folder use) (name : string) (attrs : $(map attribute use))
(construct : ctx ::: {Unit} -> [[Body] ~ ctx] => Basis.tag (use ++ ignore) ([Body] ++ ctx) ([Body] ++ ctx) [] []) =
{Nam = name,
Attributes = attrs,
Folder = fl,
Construct = fn [ctx] [[Body] ~ ctx] (ats : $use) (inner : xml ([Body] ++ ctx) [] []) =>
Basis.tag null None noStyle None ats construct inner}
fun simpleTag [ignore] name (bt : bodyTag ignore) : tag [] =
@@tag [[]] [ignore] ! _ name {} (fn [ctx] [[Body] ~ ctx] => bt ())
fun simpleTag' [use] [ignore] [use ~ ignore] (fl : folder use)
name (bt : bodyTag (use ++ ignore)) (ats : $(map attribute use)) : tag use =
@@tag [use] [ignore] ! fl name ats (fn [ctx] [[Body] ~ ctx] => bt ())
fun url name = {Nam = name,
Parse = checkUrl}
datatype error a =
Good of a
| Bad of string
fun format [tags] (fl : folder tags) (tags : $(map tag tags)) [ctx] [[Body] ~ ctx] s =
let
fun loop s : error (xml ([Body] ++ ctx) [] [] * string) =
case String.msplit {Haystack = s, Needle = "&<"} of
None => Good (cdata s, "")
| Some (pre, ch, post) =>
case ch of
#"&" =>
(case String.split post #";" of
None => Bad "No ';' after '&'"
| Some (code, post) =>
let
val xml =
case code of
"lt" => <xml><</xml>
| "gt" => <xml>></xml>
| "amp" => <xml>&</xml>
| "nbsp" => <xml> </xml>
| _ =>
if String.length code > 0 && String.sub code 0 = #"#" then
case read (String.suffix code 1) of
None => <xml/>
| Some 0 => <xml/>
| Some n => cdataChar (Char.fromInt n)
else
<xml/>
in
case loop post of
Good (after, post) => Good (<xml>{[pre]}{xml}{after}</xml>, post)
| x => x
end)
| _ =>
if String.length post > 0 && String.sub post 0 = #"/" then
case String.split post #"\x3E" of
None => Bad "No '>' after '</'"
| Some (_, post) => Good (<xml>{[pre]}</xml>, post)
else
case String.msplit {Haystack = post, Needle = " >"} of
None => Bad "No '>' after '<'"
| Some (tname, ch, post) =>
@foldR [tag] [fn _ => unit -> error (xml ([Body] ++ ctx) [] [] * string)]
(fn [nm :: Name] [ts :: {Type}] [r :: {{Type}}] [[nm] ~ r] (meta : tag ts) acc () =>
if meta.Nam = tname then
let
fun doAttrs (ch, post, ats : $(map option ts)) =
case ch of
#"\x3E" => Good (ats, post)
| _ =>
if String.length post > 0 && Char.isSpace (String.sub post 0) then
doAttrs (ch, String.substring post {Start = 1,
Len = String.length post - 1},
ats)
else
case String.split post #"=" of
None =>
(case String.split post #"\x3E" of
None => Bad "No tag ender '\x3E'"
| Some (_, post) => Good (ats, post))
| Some (aname, post) =>
if String.length post >= 1 && String.sub post 0 = #"\"" then
case String.split (String.substring post
{Start = 1,
Len = String.length post
- 1})
#"\"" of
None => Bad "No '\"' to end attribute value"
| Some (aval, post) =>
let
val ats =
@map2 [attribute] [option] [option]
(fn [t] meta v =>
if aname = meta.Nam then
meta.Parse aval
else
v)
meta.Folder meta.Attributes ats
in
doAttrs (#" ", post, ats)
end
else
Bad "Attribute value doesn't begin with quote"
in
case doAttrs (ch, post, @map0 [option] (fn [t :: Type] => None)
meta.Folder) of
Good (ats, post) =>
let
val ats =
@map2 [attribute] [option] [ident]
(fn [t] meta v =>
case v of
None => error <xml>Missing attribute {[meta.Nam]}
for {[tname]}</xml>
| Some v => v)
meta.Folder meta.Attributes ats
in
case loop post of
Good (inner, post) =>
(case loop post of
Good (after, post) =>
Good (<xml>{[pre]}{meta.Construct [ctx] !
ats inner}{after}</xml>, post)
| x => x)
| x => x
end
| Bad s => Bad s
end
else
acc ())
(fn () => Bad ("Unknown HTML tag " ^ tname)) fl tags ()
in
case loop s of
Bad msg => Failure msg
| Good (xml, _) => Success xml
end
val b = simpleTag "b" @@b
val i = simpleTag "i" @@i
val a = simpleTag' "a" @@a {Href = url "href"}
val strong = simpleTag "strong" @@strong
val em = simpleTag "em" @@em
val p = simpleTag "p" @@p
fun unhtml s =
case String.msplit {Haystack = s, Needle = "&<"} of
None => s
| Some (pre, ch, post) =>
case ch of
#"&" =>
(case String.split post #";" of
None => s
| Some (code, post) =>
let
val ch =
case code of
"lt" => #"<"
| "gt" => #">"
| "amp" => #"&"
| _ =>
if String.length code > 0 && String.sub code 0 = #"#" then
case read (String.suffix code 1) of
None => #" "
| Some 0 => #" "
| Some n => Char.fromInt n
else
#" "
in
pre ^ String.str ch ^ unhtml post
end)
| _ =>
if String.length post > 0 && String.sub post 0 = #"/" then
case String.split post #">" of
None => s
| Some (_, post) => pre ^ unhtml post
else
let
fun passHtmlStuff post =
case String.msplit {Haystack = post, Needle = "\">"} of
None => s
| Some (_, ch, post) =>
case ch of
#">" => pre ^ unhtml post
| _ =>
case String.split post #"\"" of
None => s
| Some (_, post) => passHtmlStuff post
in
passHtmlStuff post
end