|
49 | 49 | (defn html []
|
50 | 50 | "Use `get-info` to construct an HTML document."
|
51 | 51 |
|
52 |
| - (import |
53 |
| - ; The depenency on `lxml` isn't declared in `setup.py` because it |
54 |
| - ; isn't needed to play the game. |
55 |
| - lxml.html |
56 |
| - lxml.builder) |
57 |
| - |
58 |
| - (setv E (lxml.builder.ElementMaker |
59 |
| - :makeelement lxml.html.html-parser.makeelement)) |
60 |
| - |
61 | 52 | (defn mapsym [tile]
|
62 | 53 | (E.code :class "mapsym" #* (gfor
|
63 | 54 | cc tile.mapsym
|
|
70 | 61 |
|
71 | 62 | (setv info (get-info))
|
72 | 63 |
|
73 |
| - (setv doc (E.html :lang "en" |
| 64 | + (+ "<!DOCTYPE html>\n" (render-elem (E.html :lang "en" |
74 | 65 |
|
75 | 66 | ; The `<head>`
|
76 | 67 | (E.head
|
77 |
| - (E.meta :charset "UTF-8") |
| 68 | + (E.meta :charset "UTF-8" :_self-closing T) |
78 | 69 | (E.title "Tilepedia — Infinitesimal Quest 2 + ε")
|
79 | 70 | (E.style "
|
80 | 71 | h2, h3
|
|
117 | 108 | (E.li #* (if (isinstance bullet tuple)
|
118 | 109 | [(E.strong (get bullet 0) ": ") (str (get bullet 1))]
|
119 | 110 | [(E.strong bullet)]))))
|
120 |
| - (E.div :class "flavor" tile.flavor)]))])))) |
121 |
| - |
122 |
| - (lxml.html.tostring doc |
123 |
| - :pretty-print T :encoding "unicode" :doctype "<!DOCTYPE html>")) |
| 111 | + (E.div :class "flavor" tile.flavor)]))])))))) |
| 112 | + |
| 113 | +; In combination with `render-elem`, `E` works roughly like |
| 114 | +; `lxml.builder.ElementMaker`. |
| 115 | +(setv E ((type "ElementMaker" #() (dict |
| 116 | + :__getattr__ (fn [self tag] |
| 117 | + (fn [#* kids [_self-closing F] #** attrs] |
| 118 | + #(tag attrs kids _self-closing))))))) |
| 119 | + |
| 120 | +(defn render-elem [x] |
| 121 | + (import html [escape :as hesc]) |
| 122 | + (when (isinstance x str) |
| 123 | + (return (hesc x :quote F))) |
| 124 | + (setv [tag attrs kids self-closing] x) |
| 125 | + (.format "<{} {}>{}{}" |
| 126 | + (hesc tag) |
| 127 | + (.join " " (gfor |
| 128 | + [k v] (.items attrs) |
| 129 | + f"{(hesc k)}='{(hesc v)}'")) |
| 130 | + (.join "" (map render-elem kids)) |
| 131 | + (if self-closing "" f"</{(hesc tag)}>"))) |
124 | 132 |
|
125 | 133 |
|
126 | 134 | (defn cat [l]
|
|
0 commit comments