/html.arc

http://github.com/alimoeeny/arc · Unknown · 443 lines · 361 code · 82 blank · 0 comment · 0 complexity · eb97075d185b8095f4aace69c7f82f48 MD5 · raw file

  1. ; HTML Utils.
  2. (def color (r g b)
  3. (with (c (table)
  4. f (fn (x) (if (< x 0) 0 (> x 255) 255 x)))
  5. (= (c 'r) (f r) (c 'g) (f g) (c 'b) (f b))
  6. c))
  7. (def dehex (str) (errsafe (coerce str 'int 16)))
  8. (defmemo hex>color (str)
  9. (and (is (len str) 6)
  10. (with (r (dehex (cut str 0 2))
  11. g (dehex (cut str 2 4))
  12. b (dehex (cut str 4 6)))
  13. (and r g b
  14. (color r g b)))))
  15. (defmemo gray (n) (color n n n))
  16. (= white (gray 255)
  17. black (gray 0)
  18. linkblue (color 0 0 190)
  19. orange (color 255 102 0)
  20. darkred (color 180 0 0)
  21. darkblue (color 0 0 120)
  22. )
  23. (= opmeths* (table))
  24. (mac opmeth args
  25. `(opmeths* (list ,@args)))
  26. (mac attribute (tag opt f)
  27. `(= (opmeths* (list ',tag ',opt)) ,f))
  28. (= hexreps (table))
  29. (for i 0 255 (= (hexreps i)
  30. (let s (coerce i 'string 16)
  31. (if (is (len s) 1) (+ "0" s) s))))
  32. (defmemo hexrep (col)
  33. (+ (hexreps (col 'r)) (hexreps (col 'g)) (hexreps (col 'b))))
  34. (def opcolor (key val)
  35. (w/uniq gv
  36. `(whenlet ,gv ,val
  37. (pr ,(string " " key "=#") (hexrep ,gv)))))
  38. (def opstring (key val)
  39. `(aif ,val (pr ,(+ " " key "=\"") it #\")))
  40. (def opnum (key val)
  41. `(aif ,val (pr ,(+ " " key "=") it)))
  42. (def opsym (key val)
  43. `(pr ,(+ " " key "=") ,val))
  44. (def opsel (key val)
  45. `(if ,val (pr " selected")))
  46. (def opcheck (key val)
  47. `(if ,val (pr " checked")))
  48. (def opesc (key val)
  49. `(awhen ,val
  50. (pr ,(string " " key "=\""))
  51. (if (isa it 'string) (pr-escaped it) (pr it))
  52. (pr #\")))
  53. ; need to escape more? =?
  54. (def pr-escaped (x)
  55. (each c x
  56. (pr (case c #\< "&#60;"
  57. #\> "&#62;"
  58. #\" "&#34;"
  59. #\& "&#38;"
  60. c))))
  61. (attribute a href opstring)
  62. (attribute a rel opstring)
  63. (attribute a class opstring)
  64. (attribute a id opsym)
  65. (attribute a onclick opstring)
  66. (attribute a onmouseout opstring)
  67. (attribute a onmouseover opstring)
  68. (attribute body alink opcolor)
  69. (attribute body bgcolor opcolor)
  70. (attribute body leftmargin opnum)
  71. (attribute body link opcolor)
  72. (attribute body marginheight opnum)
  73. (attribute body marginwidth opnum)
  74. (attribute body topmargin opnum)
  75. (attribute body vlink opcolor)
  76. (attribute div id opstring)
  77. (attribute div name opstring)
  78. (attribute div class opstring)
  79. (attribute div onclick opstring)
  80. (attribute div onmouseout opstring)
  81. (attribute div onmouseover opstring)
  82. (attribute font color opcolor)
  83. (attribute font face opstring)
  84. (attribute font size opnum)
  85. (attribute form action opstring)
  86. (attribute form method opsym)
  87. (attribute form id opstring)
  88. (attribute form name opstring)
  89. (attribute form onsubmit opstring)
  90. (attribute form enctype opstring)
  91. (attribute img align opsym)
  92. (attribute img border opnum)
  93. (attribute img height opnum)
  94. (attribute img width opnum)
  95. (attribute img vspace opnum)
  96. (attribute img hspace opnum)
  97. (attribute img src opstring)
  98. (attribute img id opsym)
  99. (attribute img alt opstring)
  100. (attribute input name opstring)
  101. (attribute input id opstring)
  102. (attribute input size opnum)
  103. (attribute input type opsym)
  104. (attribute input value opesc)
  105. (attribute input checked opcheck)
  106. (attribute input onclick opstring)
  107. (attribute input onblur opstring)
  108. (attribute input class opstring)
  109. (attribute select name opstring)
  110. (attribute option selected opsel)
  111. (attribute table bgcolor opcolor)
  112. (attribute table border opnum)
  113. (attribute table cellpadding opnum)
  114. (attribute table cellspacing opnum)
  115. (attribute table width opstring)
  116. (attribute table id opstring)
  117. (attribute textarea cols opnum)
  118. (attribute textarea id opstring)
  119. (attribute textarea name opstring)
  120. (attribute textarea rows opnum)
  121. (attribute textarea wrap opsym)
  122. (attribute td align opsym)
  123. (attribute td bgcolor opcolor)
  124. (attribute td colspan opnum)
  125. (attribute td rowspan opnum)
  126. (attribute td width opnum)
  127. (attribute td height opnum)
  128. (attribute td valign opsym)
  129. (attribute td class opstring)
  130. (attribute tr bgcolor opcolor)
  131. (attribute tr valign opsym)
  132. (attribute tr height opnum)
  133. (attribute tr class opstring)
  134. (attribute tr onclick opstring)
  135. (attribute tr id opstring)
  136. (attribute hr color opcolor)
  137. (attribute script type opstring)
  138. (attribute span class opstring)
  139. (attribute span align opstring)
  140. (attribute span id opsym)
  141. (attribute rss version opstring)
  142. (mac gentag args (start-tag args))
  143. (mac tag (spec . body)
  144. `(do ,(start-tag spec)
  145. ,@body
  146. ,(end-tag spec)))
  147. (mac tag-if (test spec . body)
  148. `(if ,test
  149. (tag ,spec ,@body)
  150. (do ,@body)))
  151. (def start-tag (spec)
  152. (if (atom spec)
  153. `(pr ,(string "<" spec ">"))
  154. (let opts (tag-options (car spec) (pair (cdr spec)))
  155. (if (all [isa _ 'string] opts)
  156. `(pr ,(string "<" (car spec) (apply string opts) ">"))
  157. `(do (pr ,(string "<" (car spec)))
  158. ,@(map (fn (opt)
  159. (if (isa opt 'string)
  160. `(pr ,opt)
  161. opt))
  162. opts)
  163. (pr ">"))))))
  164. (def end-tag (spec)
  165. `(pr ,(string "</" (carif spec) ">")))
  166. (def literal (x)
  167. (case (type x)
  168. sym (in x nil t)
  169. cons (caris x 'quote)
  170. t))
  171. ; Returns a list whose elements are either strings, which can
  172. ; simply be printed out, or expressions, which when evaluated
  173. ; generate output.
  174. (def tag-options (spec options)
  175. (if (no options)
  176. '()
  177. (let ((opt val) . rest) options
  178. (let meth (if (is opt 'style) opstring (opmeth spec opt))
  179. (if meth
  180. (if val
  181. (cons (if (precomputable-tagopt val)
  182. (tostring (eval (meth opt val)))
  183. (meth opt val))
  184. (tag-options spec rest))
  185. (tag-options spec rest))
  186. (do
  187. (pr "<!-- ignoring " opt " for " spec "-->")
  188. (tag-options spec rest)))))))
  189. (def precomputable-tagopt (val)
  190. (and (literal val)
  191. (no (and (is (type val) 'string) (find #\@ val)))))
  192. (def br ((o n 1))
  193. (repeat n (pr "<br>"))
  194. (prn))
  195. (def br2 () (prn "<br><br>"))
  196. (mac center body `(tag center ,@body))
  197. (mac underline body `(tag u ,@body))
  198. (mac tab body `(tag (table border 0) ,@body))
  199. (mac tr body `(tag tr ,@body))
  200. (let pratoms (fn (body)
  201. (if (or (no body)
  202. (all [and (acons _) (isnt (car _) 'quote)]
  203. body))
  204. body
  205. `((pr ,@body))))
  206. (mac td body `(tag td ,@(pratoms body)))
  207. (mac trtd body `(tr (td ,@(pratoms body))))
  208. (mac tdr body `(tag (td align 'right) ,@(pratoms body)))
  209. (mac tdcolor (col . body) `(tag (td bgcolor ,col) ,@(pratoms body)))
  210. )
  211. (mac row args
  212. `(tr ,@(map [list 'td _] args)))
  213. (mac prrow args
  214. (w/uniq g
  215. `(tr ,@(map (fn (a)
  216. `(let ,g ,a
  217. (if (number ,g)
  218. (tdr (pr ,g))
  219. (td (pr ,g)))))
  220. args))))
  221. (mac prbold body `(tag b (pr ,@body)))
  222. (def para args
  223. (gentag p)
  224. (when args (apply pr args)))
  225. (def menu (name items (o sel nil))
  226. (tag (select name name)
  227. (each i items
  228. (tag (option selected (is i sel))
  229. (pr i)))))
  230. (mac whitepage body
  231. `(tag html
  232. (tag (body bgcolor white alink linkblue) ,@body)))
  233. (def errpage args (whitepage (apply prn args)))
  234. (def blank-url () "s.gif")
  235. ; Could memoize these.
  236. ; If h = 0, doesn't affect table column widths in some Netscapes.
  237. (def hspace (n) (gentag img src (blank-url) height 1 width n))
  238. (def vspace (n) (gentag img src (blank-url) height n width 0))
  239. (def vhspace (h w) (gentag img src (blank-url) height h width w))
  240. (mac new-hspace (n)
  241. (if (number n)
  242. `(pr ,(string "<span style=\"padding-left:" n "px\" />"))
  243. `(pr "<span style=\"padding-left:" ,n "px\" />")))
  244. ;(def spacerow (h) (tr (td (vspace h))))
  245. (def spacerow (h) (pr "<tr style=\"height:" h "px\"></tr>"))
  246. ; For use as nested table.
  247. (mac zerotable body
  248. `(tag (table border 0 cellpadding 0 cellspacing 0)
  249. ,@body))
  250. ; was `(tag (table border 0 cellpadding 0 cellspacing 7) ,@body)
  251. (mac sptab body
  252. `(tag (table style "border-spacing: 7px 0px;") ,@body))
  253. (mac widtable (w . body)
  254. `(tag (table width ,w) (tr (td ,@body))))
  255. (def cellpr (x) (pr (or x "&nbsp;")))
  256. (def but ((o text "submit") (o name nil))
  257. (gentag input type 'submit name name value text))
  258. (def submit ((o val "submit"))
  259. (gentag input type 'submit value val))
  260. (def buts (name . texts)
  261. (if (no texts)
  262. (but)
  263. (do (but (car texts) name)
  264. (each text (cdr texts)
  265. (pr " ")
  266. (but text name)))))
  267. (mac spanrow (n . body)
  268. `(tr (tag (td colspan ,n) ,@body)))
  269. (mac form (action . body)
  270. `(tag (form method "post" action ,action) ,@body))
  271. (mac textarea (name rows cols . body)
  272. `(tag (textarea name ,name rows ,rows cols ,cols) ,@body))
  273. (def input (name (o val "") (o size 10))
  274. (gentag input type 'text name name value val size size))
  275. (mac inputs args
  276. `(tag (table border 0)
  277. ,@(map (fn ((name label len text))
  278. (w/uniq (gl gt)
  279. `(let ,gl ,len
  280. (tr (td (pr ',label ":"))
  281. (if (isa ,gl 'cons)
  282. (td (textarea ',name (car ,gl) (cadr ,gl)
  283. (let ,gt ,text (if ,gt (pr ,gt)))))
  284. (td (gentag input type ',(if (is label 'password)
  285. 'password
  286. 'text)
  287. name ',name
  288. size ,len
  289. value ,text)))))))
  290. (tuples args 4))))
  291. (def single-input (label name chars btext (o pwd))
  292. (pr label)
  293. (gentag input type (if pwd 'password 'text) name name size chars)
  294. (sp)
  295. (submit btext))
  296. (mac cdata body
  297. `(do (pr "<![CDATA[")
  298. ,@body
  299. (pr "]]>")))
  300. (def eschtml (str)
  301. (tostring
  302. (each c str
  303. (pr (case c #\< "&#60;"
  304. #\> "&#62;"
  305. #\" "&#34;"
  306. #\' "&#39;"
  307. #\& "&#38;"
  308. c)))))
  309. (def esc-tags (str)
  310. (tostring
  311. (each c str
  312. (pr (case c #\< "&#60;"
  313. #\> "&#62;"
  314. #\& "&#38;"
  315. c)))))
  316. (def nbsp () (pr "&nbsp;"))
  317. (def link (text (o dest text) (o color))
  318. (tag (a href dest)
  319. (tag-if color (font color color)
  320. (pr text))))
  321. (def underlink (text (o dest text))
  322. (tag (a href dest) (tag u (pr text))))
  323. (def striptags (s)
  324. (let intag nil
  325. (tostring
  326. (each c s
  327. (if (is c #\<) (set intag)
  328. (is c #\>) (wipe intag)
  329. (no intag) (pr c))))))
  330. (def clean-url (u)
  331. (rem [in _ #\" #\' #\< #\>] u))
  332. (def shortlink (url)
  333. (unless (or (no url) (< (len url) 7))
  334. (link (cut url 7) url)))
  335. ; this should be one regexp
  336. (def parafy (str)
  337. (let ink nil
  338. (tostring
  339. (each c str
  340. (pr c)
  341. (unless (whitec c) (set ink))
  342. (when (is c #\newline)
  343. (unless ink (pr "<p>"))
  344. (wipe ink))))))
  345. (mac spanclass (name . body)
  346. `(tag (span class ',name) ,@body))
  347. (def pagemessage (text)
  348. (when text (prn text) (br2)))
  349. ; Could be stricter. Memoized because looking for chars in Unicode
  350. ; strings is terribly inefficient in Mzscheme.
  351. (defmemo valid-url (url)
  352. (and (len> url 10)
  353. (or (begins url "http://")
  354. (begins url "https://"))
  355. (~find [in _ #\< #\> #\" #\'] url)))
  356. (mac fontcolor (c . body)
  357. (w/uniq g
  358. `(let ,g ,c
  359. (if ,g
  360. (tag (font color ,g) ,@body)
  361. (do ,@body)))))