Skip to content

Commit 3be5c74

Browse files
committed
Removed the dependency on lxml for the HTML tilepedia.
1 parent 692d2d0 commit 3be5c74

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

simalq/tile/tilepedia.hy

+23-15
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,6 @@
4949
(defn html []
5050
"Use `get-info` to construct an HTML document."
5151

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-
6152
(defn mapsym [tile]
6253
(E.code :class "mapsym" #* (gfor
6354
cc tile.mapsym
@@ -70,11 +61,11 @@
7061

7162
(setv info (get-info))
7263

73-
(setv doc (E.html :lang "en"
64+
(+ "<!DOCTYPE html>\n" (render-elem (E.html :lang "en"
7465

7566
; The `<head>`
7667
(E.head
77-
(E.meta :charset "UTF-8")
68+
(E.meta :charset "UTF-8" :_self-closing T)
7869
(E.title "Tilepedia — Infinitesimal Quest 2 + ε")
7970
(E.style "
8071
h2, h3
@@ -117,10 +108,27 @@
117108
(E.li #* (if (isinstance bullet tuple)
118109
[(E.strong (get bullet 0) ": ") (str (get bullet 1))]
119110
[(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)}>")))
124132

125133

126134
(defn cat [l]

tests/test_misc.hy

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
(defn test-tilepedia []
3636
"Just check that calling all these info methods doesn't crash."
37-
(assert (isinstance (hy.I.simalq/tile/tilepedia.get-info) dict)))
37+
(assert (isinstance (hy.I.simalq/tile/tilepedia.html) str)))
3838

3939

4040
(defn test-builtin-quests []

0 commit comments

Comments
 (0)