-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
webize.art
executable file
·332 lines (286 loc) · 10.3 KB
/
webize.art
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env arturo -d
;==========================================
; Webize
;
; Easy-to-use & minimalist
; static website generator
; in Arturo
;
; @file: webize.art
; @author: drkameleon
;==========================================
Webize: #[
version: "0.2"
]
;--------------------------
; Globals
;--------------------------
Paths: #[
root: path\current
build: "|project|/build"
dataPath: "|project|/data"
pages: "|project|/pages"
theme: "|project|/theme"
resourceFonts: "|project|/theme/resources/fonts"
resourceImages: "|project|/theme/resources/images"
resourceScripts: "|project|/theme/resources/scripts"
resourceStyles: "|project|/theme/resources/styles"
]
Settings: #[]
Config: #[]
Target: "build"
Theme: "theme"
ThemeSettings: # "theme/settings.art"
InfoPadding: 13
;--------------------------
; Private Helpers
;--------------------------
{_showLogo}: function [][
print join @ to :block {
color #green" _ _" color #white.bold" __ __ _ _ " "\n"
color #green" _| || |_" color #white.bold" \ / /__| |__ (_)_______ " "\n"
color #green" |_ -| _|" color #white.bold" \ /\ / / _ \ '_ \| |_ / _ \" "\n"
color #green" |_ |- _|" color #white.bold"\ V V / __/ |_/ | |/ / __/" "\n"
color #green" |_||_|" color #white.bold" \_/\_/ \___|_.__/|_/___\___|"
}
]
{_showHeader}: function [message][
print ""
print color #magenta "==========================================================================="
print [pad "" InfoPadding color #magenta upper message]
print color #magenta "==========================================================================="
]
{_showInfo}: function [msg,inf][
if? attr "section" ->
print [color #green ("* " ++ msg) inf]
else ->
print [color #cyan (" - " ++ msg) inf]
]
{_showInfoLine}: function [head,tail,success][
if? success -> print [color #green pad head InfoPadding tail]
else -> print [color #cyan pad head InfoPadding tail]
]
{_showHelp}: function [][
print {
Usage:
webize [command] <options>
Commands:
new <name> Creates new project
build Builds current project
help Show this help screen
version Show current version
}
]
{_toolExists?}: function [cmd][
not? empty? strip execute ~{!sh command -v |cmd|}
]
{_routerDiagram}: function [p][
join.with:" ▪︎ " split.path p -- extract.extension p
]
{_routeDiagram}: function [p][
join.with:"/" split.path p
]
;--------------------------
; Helpers
;--------------------------
linkTo: function [location][
i: 0
parts: location
if block? parts -> parts: join.path parts
parts: split.path parts
adjust: 0
if "_index" = last pagePath -> adjust: 1
while [and? [i < size pagePath]
[i < size parts]]
[
if pagePath\[i] <> parts\[i] -> break
i: i + 1
]
ret: ""
if? pagePath=parts [
ret: "#"
ret
]
else [
ret: join.path (repeat [".."] ((size pagePath)-i)-adjust) ++ join.path slice parts i (size parts)-1
ret: replace encode.url replace ret "?" "-" "+" "-"
if? ret="" -> "."
else -> ret
]
]
importPartial: function [pa][
project: path\current
read join.path @[~ Paths\theme pa]
]
getCanonical: function [root][
parts: ("_index"=last pagePath) ? -> chop pagePath -> pagePath
parts: map parts 'p -> replace encode.url replace p "?" "-" "+" "-"
final: root ++ (lower join.path parts) ++ "/"
final: replace final "https://" "<HTTPS>"
replace replace final "//" "/" "<HTTPS>" "https://"
]
permalink: function [tag][
replace lower tag " " "-"
]
;--------------------------
; The main library
;--------------------------
{_newProject}: function [project][
printInfo.section "creating new project:" project
write.directory to :string project ø
loop Paths [k,pa][
subpath: ~pa
printInfo "creating:" subpath
write.directory ~pa ø
]
write ~"|project|/pages/index.md.art" ""
write ~"|project|/theme/default.html" ""
write ~"|project|/theme/settings.art" ""
write ~"|project|/config.art" ""
]
{_buildProject}: function [loc][
if? exists? "config.art" [
Config: #"config.art"
Data: #.data[]
loop list "data" 'f [
if ".art" = extract.extension f ->
set Data (extract.basename f) -- extract.extension f do read f
]
;inspect Data
;-------------------------
; Render pages
;-------------------------
call {_showHeader} ["rendering"]
pages: select list.recursive "pages" => [ suffix? & ".art"]
pageTree: #.data[]
loop pages 'pg [
current: pageTree
p: pg -- "pages/"
parts: (split.path p) -- "_index.art"
loop parts 'part [
pp: part
if contains? pp ".art" ->
pp: pp -- ".art"
if not? key? current pp [
set current pp #.data []
]
current: get current pp
]
]
; inspect pageTree
count: size pages
loop.with: 'c pages 'page [
sourcePath: page --"pages/"
targetRoute: sourcePath -- extract.extension page
targetPath: ""
if? "_index" = extract.filename page ->
targetPath: replace append join.path @[loc targetRoute] ".html" "_" ""
else ->
targetPath: append join.path @[loc targetRoute "index"] ".html"
targetPath: lower replace replace targetPath "?" "-" " " "-"
targetDir: extract.directory targetPath
targetFile: extract.filename targetPath
write.directory targetDir ø
call {_showInfoLine} @[~"[|c+1|/|count|]" call {_routerDiagram} @[sourcePath] false]
templatePath: ""
loop ThemeSettings\routes [route,template][
if not? empty? match sourcePath route [
; print ["matched:" route "for:" sourcePath]
templatePath: join.path @[Theme template]
break
]
]
do [
pagePath: split.path targetRoute
;; debug
; print [ "\n"
; "sourcePath:" sourcePath "\n"
; "targetRoute:" targetRoute "\n"
; "targetPath:" targetPath "\n"
; "targetDir:" targetDir "\n"
; "targetFile:" targetFile "\n"
; "pagePath:" pagePath "\n"
; "template:" templatePath "\n"
; ]
W: #.data page
rendered: new render.template read templatePath
if key? ThemeSettings 'postprocess [
loop ThemeSettings\postprocess [k,v] [
replace 'rendered to :regex k v
]
]
write targetPath rendered
execute ~"html-minifier --collapse-whitespace --remove-comments |targetPath| -o |targetPath|"
]
]
call {_showInfoLine} @["Done." "" true]
;-------------------------
; Post-processing
;-------------------------
call {_showHeader} ["finalizing"]
loop filter list "theme/resources" => [in? "." &] 'res [
folderPath: join.path @[loc "resources" extract.basename res]
call {_showInfoLine} @["create" call {_routeDiagram} @[res -- "theme/resources/"] false]
write.directory join.path @[loc "resources" extract.basename res] ø
loop list res 'item [
tgt: join.path @[loc item -- "theme/"]
case [in? extract.extension item]
when? [[".scss" ".sass"]] [
call {_showInfoLine} @["process" call {_routeDiagram} @[item -- "theme/resources/"] false]
newTgt: replace tgt extract.extension tgt ".css"
execute ~"sass --style compressed |item| |newTgt|"
]
when? [[".png"]][
call {_showInfoLine} @["compress" call {_routeDiagram} @[item -- "theme/resources/"] false]
newTgt: replace tgt extract.extension tgt ".webp"
execute ~"cwebp |item| -o |newTgt|"
call {_showInfoLine} @["copy" call {_routeDiagram} @[item -- "theme/resources/"] false]
execute ~"cp |item| |extract.directory tgt|"
]
when? [[".js"]][
call {_showInfoLine} @["copy" call {_routeDiagram} @[item -- "theme/resources/"] false]
execute ~"cp |item| |extract.directory tgt|"
execute ~"uglifyjs --compress -o |tgt| -- |tgt|"
]
else [
call {_showInfoLine} @["copy" call {_routeDiagram} @[item -- "theme/resources/"] false]
execute ~"cp |item| |extract.directory tgt|"
]
]
]
call {_showInfoLine} @["Done." "" true]
print ""
]
else [
print "cannot build: not valid project"
]
]
initialize: function [][
loop ['sass 'coffee 'minify 'cwebp] [tool][
set Settings tool (call {_toolExists?} @[tool])
]
]
;--------------------------
; The main entry point
;--------------------------
if standalone? [
call {_showLogo} []
initialize
if empty? arg [ call {_showHelp} [], exit ]
case [key? args]
when? -> "new" [
call {_newProject} @[args\new]
]
when? -> "build" [
location: join.path @[path\current Target]
if key? args 'at [
location: join.path @[path\current to :string args\at ]
]
call {_buildProject} @[location]
]
else -> print "command not recognized"
]
;==========================================
; This is the end,
; my only friend, the end...
;==========================================