-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebsite.dak
More file actions
234 lines (212 loc) · 8.04 KB
/
website.dak
File metadata and controls
234 lines (212 loc) · 8.04 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
(import [:esbuild :as esbuild]
[:node:fs [createReadStream createWriteStream]]
[:node:fs/promises [readFile writeFile mkdir rm cp]]
[:node:path [dirname join basename normalize]]
[:node:crypto [createHash]]
[:node:stream [Readable]]
[:node:stream/promises [pipeline]]
[:marked [marked]]
[:marked-base-url [baseUrl]]
[:marked-gfm-heading-id [gfmHeadingId]]
[:./transpiler.dak [esbuildPlugin]]
[:./html.dak [doc html unsafe]])
(const prodURL "https://www.daklang.com/")
(marked.use
{:mangle false :headerIds false}
(gfmHeadingId)
(baseUrl "https://github.com/daaku/dak/blob/main/"))
(fn markdown [config assets content]
(-> content
marked.parse
(.replace #/\.\/www\/assets\/(\w+\.\w+)/g #(. assets $2))
(.replaceAll prodURL "/")))
(fn@ esbuild-build [config]
@(esbuild.build {:entryPoints ["www/assets/logo.svg"
"www/assets/logo.png"
"www/assets/index.css"
"www/assets/index.dak"]
:define {"globalThis.IS_PRODUCTION" "true"
"import.meta.main" "false"}
:bundle true
:minify true
:format :esm
:outdir "dist"
:metafile true
:assetNames "assets/[name]-[hash]"
:entryNames "assets/[name]-[hash]"
:loader {:.svg :copy :.png :copy}
:external ["node:*"]
:sourcemap true
:plugins [(esbuildPlugin)]}))
(fn strip-prefix [s prefix]
(if (s.startsWith prefix) (s.slice prefix.length) s))
(fn assets-map [build-result]
(let [assets {}]
(for-of [[hashed-path info] (Object.entries build-result.metafile.outputs)]
(when info.entryPoint
(set (. assets (strip-prefix info.entryPoint "www/assets/"))
(strip-prefix hashed-path "dist"))))
assets))
(const tour [:welcome :literals :variables :import :functions :lambda
:classes :conditions :async :loops :destructuring :case
:try-catch :operators :macros])
(fn tour-title [s]
(-> s
(.replace "-" " ")
(.replace #/\w\S*/g #(str (-> $ (.charAt 0) .toUpperCase)
(-> $ (.substring 1) .toLowerCase)))))
(fn head-ui [assets {:keys [title] :or {description "The Dak language."}}]
[:head
[:meta {:charset :utf-8}]
[:title title]
[:meta {:name :viewport :content "width=device-width,initial-scale=1"}]
[:meta {:property :og:image
:content (str "https://www.daklang.com" (. assets "logo.png"))}]
(when description
[:meta {:name :description :content description}])
[:link {:rel :stylesheet :type "text/css" :href (. assets "index.css")}]
[:link {:rel :icon :type "image/svg+xml" :href (. assets "logo.svg")}]])
(fn foot-ui []
[:footer
[:ul
[:li [:a {:href "/"} "Dak Language"]]
[:li [:a {:href "/tour/"} "Tour"]]
[:li [:a {:href "/play/"} "Playground"]]
[:li [:a {:href "https://github.com/daaku/dak"} "GitHub"]]
[:li [:a {:href "mailto:n@daaku.org?subject=Dak" :target :_blank}
"\u00a9 Naitik Shah"]]]])
(fn logo-ui [assets]
[:a {:href "/"} [:img {:width 24 :height 24 :src (. assets "logo.svg")
:alt "Home" :title "Home"}]])
(fn play-ui [assets {:or {title "Dak Playground"
code ""
description ""}}]
(doc {:lang :en}
(head-ui assets
{:title title
:description
(or description
(str title " to experiment with code in the Dak Language."))})
[:body.tour
[:header
[:h1 (logo-ui assets) " " title]
[:div
[:a.tour-index {:href "/tour/"} "Tour Index"]
[:input#auto-eval {:type :checkbox :checked true}]
[:label {:for :auto-eval} "Eval"]
[:select#fmt [:option {:value :pretty} "Pretty"]
[:option {:value :raw} "Raw"]
[:option {:value :minify} "Minify"]]]]
[:div#code
[:div.code-edit
[:textarea {:id :dak-code
:placeholder "Write some Dak..."
:autocorrect :off
:autocomplete :off
:autocapitalize :none
:spellcheck false
:autofocus true} code]
[:div.overlay]]
[:textarea {:id :js-code
:placeholder "Output will show here."
:autocorrect :off
:autocomplete :off
:autocapitalize :none
:spellcheck false}]]
[:div#output]
(foot-ui)
[:script {:type :module :src (. assets "index.dak")}]]))
(fn@ play-build [config assets name]
(let [source-file (str "www/tour/" name ".dak")
out-file (str "dist/tour/" name "/index.html")
title (str (tour-title name) ": Dak Tour")
code @(readFile source-file "utf-8")]
@(mkdir (dirname out-file) {:recursive true})
@(writeFile out-file (play-ui assets {:title title :code code}))))
(fn@ error-html [config assets]
@(writeFile "dist/error.html"
(doc {:lang :en}
(head-ui assets {:title "Error"})
[:body
[:div#error
[:div
[:h1 "\u26a0"]
"There was a problem trying to get the page you requested."]]
[:script {:type :module :src (. assets "index.dak")}]])))
(fn exit [{:keys [sym title href action]} ...body]
[:div.exit
[:div.sym sym]
[:div.title title]
[:div.body ...body]
[:a.go {:href href} action]])
(fn exits-ui []
[:div.exits
(exit {:sym "\u{1f310}"
:title "Browser"
:href "https://github.com/daaku/dak/blob/main/src/website.dak"
:action "Build for Browsers"}
"See how this static website is made.")
(exit {:sym "\u{1f3ae}"
:title "Play"
:href "/tour/functions/"
:action "Playground"}
"Take a tour with an interactive playground.")
(exit {:sym "\u2328\ufe0f"
:title "CLI"
:href "https://github.com/daaku/dak/blob/main/src/bootstrap.mjs"
:action "Build CLIs"}
"Preload " [:code "bootstrap.mjs"] " in Bun for Dak support.")])
(fn@ index-html [config assets]
@(writeFile "dist/index.html"
(doc {:lang :en}
(head-ui assets
{:title "Dak Language"
:description "Dak is a Lisp like language that transpiles to JavaScript."})
[:body.narrow
[:div
(-> (markdown config assets @(readFile "readme.md" :utf8))
(.replace "<!--exits-->" (html (exits-ui)))
unsafe)]
(foot-ui config)
[:script {:type :module :src (. assets "index.dak")}]])))
(fn@ tour-index-html [config assets]
@(writeFile "dist/tour/index.html"
(doc {:lang :en}
(head-ui assets
{:title "Dak Tour"
:description "A series of modules to explore the Dak Language."})
[:body.narrow
[:h1 (logo-ui assets) " Dak Tour"]
[:div#tour-index
[:ul (... (tour.map
(fn [v]
[:li [:a {:href (str "/tour/" v "/")} (tour-title v)]])))]]]
(foot-ui config)
[:script {:type :module :src (. assets "index.dak")}])))
(fn@ play-index-html [config assets]
@(writeFile "dist/play/index.html"
(play-ui assets
{:title "Dak Playground"
:description "A Playground to experiment with the Dak Language."})))
(fn@ hex-sha256-file [path]
(let [hash (createHash :sha256)]
@(pipeline (createReadStream path) hash)
(hash.digest :hex)))
(fn since [start]
(str (Math.round (- (performance.now) start)) "ms"))
(fn@ main []
(const config {})
(const start (performance.now))
@(rm "dist" {:recursive true :force true})
@(Promise.all
[(mkdir "dist/tour" {:recursive true})
(mkdir "dist/play" {:recursive true})])
(let [assets (-> config esbuild-build await assets-map)]
@(Promise.all
[(index-html config assets)
(error-html config assets)
(tour-index-html config assets)
(play-index-html config assets)
(... (tour.map #(play-build config assets $)))]))
(console.log "Build completed in" (since start)))
@(main)