10 | import Libraries.Text.PrettyPrint.Prettyprinter.Render.HTML as Lib
13 | escapeHTML : Config -> Char -> List Char
14 | escapeHTML config ' ' = unpack config.space
15 | escapeHTML config c = unpack (htmlEscape $
cast c)
18 | annotate : Maybe Decoration -> String -> String
19 | annotate Nothing s = s
20 | annotate (Just dec) s = apply (convert dec) s
23 | apply : String -> String -> String
24 | apply f a = #"<span class="\#{f}">\#{a}</span>"#
29 | styleElement : Decoration -> Category -> String
30 | styleElement dec cat = concat $
intersperse "\n" $
catMaybes
31 | [ pure ("." ++ convert dec ++ " {")
32 | , (" \{cat .style}") <$ guard (cat .style /= "")
33 | , (" color: \{cat .colour}") <$ guard (cat .colour /= "")
38 | styleHeader : Config -> String
39 | styleHeader cfg = """
40 | \{styleElement Data cfg.datacons}
41 | \{styleElement Typ cfg.typecons}
42 | \{styleElement Bound cfg.bound}
43 | \{styleElement Function cfg.function}
44 | \{styleElement Keyword cfg.keyword}
45 | \{styleElement Comment cfg.comment}
46 | \{styleElement Namespace cfg.namespce}
47 | \{styleElement Postulate cfg.postulte}
48 | \{styleElement Module cfg.aModule}
52 | standalonePre : Config -> String
53 | standalonePre config = """
54 | <!DOCTYPE html><html lang="en">
57 | <meta charset="utf-8">
59 | \{styleHeader config}
62 | text-decoration: none;
66 | .IdrisLineNumber:hover {
69 | .IdrisLineNumber:target {
73 | background-color: yellow;
77 | function initialize() {
78 | function handleHash(ev) {
79 | if (!location.hash) return
80 | let m = location.hash.match(/#(line\\d+)(?:-(line\\d+))?/)
82 | let start = document.getElementById(m[1])
83 | let end = document.getElementById(m[2])
85 | if (end && end.compareDocumentPosition(start) === 4) {
86 | ([start, end] = [end, start])
88 | // Only on page load
89 | if (!ev) start.scrollIntoView()
90 | let parent = start.parentElement
91 | let lines = parent.children
93 | for (let n = 0; n < lines.length; n++) {
95 | if (el === start) className = 'IdrisHighlight'
96 | el.className = className
97 | if (el === end || className && !end) className = ''
104 | function handlePointerMove(ev) {
106 | for (let el = document.elementFromPoint(ev.clientX, ev.clientY); el; el = el.parentElement) {
107 | if (el.parentElement === startLine.parentElement) {
108 | if (endLine !== el) {
117 | function update(ev) {
118 | window.location.hash = startLine === endLine ? startLine.id : startLine.id + '-' + endLine.id
120 | function handlePointerDown(ev) {
121 | let target = ev.target
122 | if (target.className === 'IdrisLineNumber') {
123 | startLine = endLine = target.parentElement
124 | window.addEventListener('pointermove', handlePointerMove)
126 | ev.preventDefault()
129 | function handlePointerUp(ev) {
132 | window.removeEventListener('pointermove', handlePointerMove)
133 | startLine = endLine = null
136 | window.addEventListener('hashchange', handleHash)
137 | window.addEventListener('pointerdown', handlePointerDown)
138 | window.addEventListener('pointerup', handlePointerUp)
143 | <body onload="initialize()">
144 | <code class="IdrisCode">
148 | standalonePost : String
149 | standalonePost = """
156 | makeMacroPre : String -> String
157 | makeMacroPre name = """
158 | <code class="IdrisCode">
162 | makeMacroPost : String
163 | makeMacroPost = """
168 | makeInlineMacroPre : String -> String
169 | makeInlineMacroPre name = """
170 | <code class="IdrisCode">
174 | makeInlineMacroPost : String
175 | makeInlineMacroPost = """
180 | mkDriver : Config -> Driver
181 | mkDriver config = MkDriver
184 | lineID = "line\{ln}"
185 | desc = concat (List.replicate (minus wdth (length ln)) " " ++ [ln]) in
186 | ##"<div id="\##{lineID}"><a href="#\##{lineID}" class="IdrisLineNumber"> \##{desc} | </a>"##
188 | (escapeHTML config)
190 | (standalonePre config, standalonePost)
191 | (makeInlineMacroPre, makeInlineMacroPost)
192 | (makeMacroPre, makeMacroPost)
195 | initHTMLCmd : Command "init"
196 | initHTMLCmd = MkCommand
197 | { description = "Generate default configuration file"
200 | , arguments = filePath
204 | init : (ParsedCommand _ HTML.initHTMLCmd) -> IO ()
205 | init parsed = initExec HTML parsed.arguments