-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspindle.lua
More file actions
985 lines (815 loc) · 23.9 KB
/
spindle.lua
File metadata and controls
985 lines (815 loc) · 23.9 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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
--[[
MIT License
Spindle: a static site generator
Copyright (C) 2019-2024 Harley Denham
]]
spindle.handlers = {} -- file types
spindle.tokens = {} -- line-level tokens; headings, images, etc.
spindle.inlines = {} -- inline syntax; links, bold, etc.
spindle.modifiers = {} -- %variable:modifiers
spindle.all_pages = {}
spindle.all_files = {}
spindle.lock_file = {} -- temp map for 'other' files @todo
spindle.markup_cache = {}
local defer_list = {}
function spindle.defer(f)
defer_list[#defer_list + 1] = f
end
spindle.output_path = "_site/"
spindle.handlers[".x"] = function(file_path)
spindle.parse_markup = spindle.__parse_markup
local page = spindle.load_page(file_path)
return page.output_path, page.canonical_url
end
spindle.tokens[">"] = function(page, scope, arg)
spindle.parse_markup = spindle.__parse_markup
local part = spindle.load_markup("_data/" .. arg .. ".x")
return spindle.render_internal(page, scope, part.syntax_tree)
end
-- @todo make this safe my goodness
spindle.tokens["~"] = function(page, scope, arg)
spindle.parse_markup = spindle.__parse_markup
local args = spindle.split_quoted(arg)
local part = spindle.load_markup("_data/" .. args[1] .. ".x")
local path = spindle.find_file(args[2])
local sub_page = spindle.load_page(path)
if page.vars.taxonomy_variable and page.vars.taxonomy_category then
local value = sub_page.vars[page.vars.taxonomy_variable]
if value and not string.lower(value):match(string.lower(page.vars.taxonomy_category)) then
return ""
end
end
return spindle.render_internal(sub_page, scope, part.syntax_tree)
end
-- @todo needs full testing
spindle.tokens["%"] = function(page, scope, arg)
local args = spindle.split_quoted(arg)
spindle.replace_inline(arg[1], arg[2])
return ""
end
--[[spindle.tokens["~"] = function(page, scope, arg)
spindle.parse_markup = spindle.__parse_markup
local args = spindle.split_quoted(arg)
-- local data = "_data/" .. args[1] .. ".x"
-- local part = spindle.load_markup("_data/" .. args[1] .. ".x")
local temp = spindle.find_in_scope(scope, args[1])
if temp == nil then
-- xx
end
local path = spindle.find_file(args[2])
return spindle.render_internal(spindle.load_page(path), scope, part.syntax_tree)
end]]
spindle.tokens["#"] = '<h1 id="%0:slug">%0</h1>'
spindle.tokens["##"] = '<h2 id="%0:slug">%0</h2>'
spindle.tokens["###"] = '<h3 id="%0:slug">%0</h3>'
spindle.tokens["-"] = { main = '<li>%0</li>', wrap = '<ul>%0</ul>' }
spindle.tokens["+"] = { main = '<li>%0</li>', wrap = '<ol>%0</ol>' }
spindle.tokens["!"] = function(page, scope, arg)
local _, url = spindle.process_any_file(arg)
return spindle.template_expand(page, '<img src="%0">', url)
end
spindle.tokens.block_default = '%0'
spindle.tokens.default = '<p>%0</p>'
-- @hack to ensure these exist
-- but skip the typecheck
spindle.tokens["$"] = 0
spindle.tokens["$$"] = 0
spindle.to_upper = string.upper
spindle.to_lower = string.lower
spindle.to_quote = function(arg)
return string.format("%q", arg)
end
spindle.modifiers.slug = spindle.to_slug
spindle.modifiers.uslug = spindle.to_slug
spindle.modifiers.title = spindle.to_title
spindle.modifiers.upper = spindle.to_upper
spindle.modifiers.lower = spindle.to_lower
spindle.modifiers.quote = spindle.to_quote
spindle.modifiers.thousands = spindle.format_thousand_separators
function spindle.make_anchor_tag(label, path)
if spindle.has_protocol(path) then
return string.format('<a href="%s">%s</a>', path, label)
end
if path:sub(1, 1) == '#' then
return string.format('<a href="%s">%s</a>', path, label)
end
local frag = path:match("#.+$")
if frag == nil then
frag = ""
else
path = path:gsub("#.+$", "")
end
local _, url = spindle.process_any_file(path)
return string.format('<a href="%s%s">%s</a>', url, frag, label)
end
-- [[STRING UTILS]]
function spindle.has_protocol(text)
return text:match("^%a+://") ~= nil
end
function spindle.url_from_path(path, domain)
return (domain and domain or spindle.domain .. path):gsub("/index.*$", ""):gsub("%.x$", ""):gsub("%.html$", "") -- @todo hard-coded
end
function spindle.short_ext(text)
return text:match("%.[^%./]+$")
end
function spindle.long_ext(text)
return text:match("%.[^/]+$")
end
function spindle.dir(text)
return text:gsub("(.*/)(.*)", "%1")
end
function spindle.basename(text)
return text:gsub("(.*/)(.*)", "%2")
end
function spindle.root_basename(text) -- @todo might rename
return spindle.basename(text):sub(1, -spindle.long_ext(text):len() - 1)
end
function spindle.trim(s)
return string.gsub(s, "^%s*(.-)%s*$", "%1")
end
-- modified from https://stackoverflow.com/a/36958689
local function escape_magic(s)
local magic_set = '[()%%.[^$%]*+%-?]'
if s == nil then return end
return (s:gsub(magic_set, '%%%1'))
end
function string:gsplit(delimiter)
if self:sub(-#delimiter) ~= delimiter then
self = self .. delimiter
end
return self:gmatch('(.-)' .. escape_magic(delimiter))
end
-- end stackoverflow
-- @todo we accidentally made this redundant
function string:split(delimiter)
local t = {}
for item in self:gsplit(delimiter) do
table.insert(t, item)
end
return t
end
-- @todo we should back out the :split/:gsplit
-- methods in favour of an Odin-based splitter
-- there's too much cross-pollinated logic that
-- should be operating in the same space/basis
-- this is a workaround hack for Lua's lack of non-inclusive negative matches
-- essentially, you define the escaped condition *and* the correct condition
-- and it will return either the now-unescaped correct text or the full replacement
function spindle.replace_escaped(template, level_one, level_two, replace)
return template:gsub(level_one, function(x)
return x:gsub(level_two, function(x) return string.format(replace, x) end)
end)
end
function spindle.iterate_lines(str)
if str:sub(-1) ~= "\n" then
str = str .. "\n"
end
return str:gmatch("(.-)\n")
end
function spindle.find_in_scope(scope, term)
for i = #scope, 1, -1 do
local v = scope[i].decl
if v and v == term then
return scope[i].value
end
end
return nil
end
function spindle.register_inline(name, func)
if func == nil and _ENV[name] then
table.insert(spindle.inlines, {name = name, exec = _ENV[name]})
else
table.insert(spindle.inlines, {name = name, exec = func})
end
end
function spindle.replace_inline(name, func)
for i, inline in ipairs(spindle.inlines) do
if inline.name == name then
if type(func) == 'string' then
inline.exec = _ENV[func]
else
inline.exec = func
end
end
end
end
spindle.register_inline("link", function(page, line)
return line:gsub("%[(.-)%]%((.-)%)", spindle.make_anchor_tag)
end)
spindle.register_inline("find", function(page, line)
return line:gsub("%%{(.-)}", function(x)
local _, url = spindle.process_any_file(x)
return url
end)
end)
function spindle.load_markup(file_path)
if file_path == nil then
return nil
end
local page = {}
page.vars = {}
page.slugs = {}
page.source_path = file_path
if spindle.markup_cache[file_path] then
page.syntax_tree = spindle.markup_cache[file_path]
else
local m = spindle.parse_markup(page, spindle.load_file(file_path)) -- @todo unsafe
spindle.markup_cache[file_path] = m
page.syntax_tree = m
end
return page
end
function spindle.load_page(file_path)
if file_path == nil then
return nil
end
if not spindle.file_exists(file_path) then
return nil
end
if spindle.all_pages[file_path] then
return spindle.all_pages[file_path]
end
local curl = spindle.url_from_path(file_path)
local page = spindle.load_markup(file_path)
page.canonical_url = curl
page.vars.canonical_url = curl
-- source_path is set in parse_markup
page.output_path = spindle.output_path .. file_path:gsub("%.x$", ".html")
page.vars.source_path = file_path
page.vars.output_path = page.output_path
-- must be cached before pre-render to avoid loops
spindle.all_pages[file_path] = page
page.vars.content = spindle.render(page, page.syntax_tree)
if page.vars.template then
spindle.parse_markup = spindle.__parse_markup
local template = spindle.load_markup("_data/" .. page.vars.template .. ".x")
spindle.render(page, template.syntax_tree)
end
return page
end
function spindle.load_file(file_path)
local f = io.open(file_path)
if f == nil then
return ""
end
local s = f:read('*a')
f:close()
return s
end
local function uslug_expand(page, slug)
if page.slugs[slug] then
local n = page.slugs[slug]
slug = string.format("%s-%d", slug, n)
page.slugs[slug] = n + 1
else
page.slugs[slug] = 1
end
return slug
end
function spindle.expand(page, scope, template)
-- @hack see template section in main
local was_content_expansion = false
-- variables: %var:modifiers
local result = template:gsub("%%([%a_]+):([%a_]+)", function(x, y)
if x == "content" then
was_content_expansion = true
end
local z = spindle.find_in_scope(scope, x) or page.vars[x]
if z == nil then
return x
end
if spindle.modifiers[y] then
local s = spindle.modifiers[y](z)
if y == "uslug" then
return uslug_expand(page, s)
end
return s
end
return z
end)
-- variables: %var
result = result:gsub("%%([%a_]+)", function(x)
if x == "content" then
was_content_expansion = true
end
return spindle.find_in_scope(scope, x) or page.vars[x]
end)
if was_content_expansion then
return result
end
-- spindle.inlines, like [links](#) or **bolded**
for i, inline in ipairs(spindle.inlines) do
result = inline.exec(page, result)
end
return result
end
function spindle.template_expand(page, template, value)
-- %placeholder:modifier
local result = template:gsub("%%(%d+):([%w_]+)", function(x, y)
local n = x + 0
if n == 0 then
x = value
else
x = spindle.split_quoted(value)[n]
end
if spindle.modifiers[y] then
local s = spindle.modifiers[y](x)
if y == "uslug" then
return uslug_expand(page, s)
end
return s
end
return x
end)
-- %placeholder
result = result:gsub("%%(%d+)", function(x)
local n = x + 0
if n == 0 then
return value
end
return spindle.split_quoted(value)[n]
end)
return result
end
--[[function print_syntax_tree(level, syntax_tree)
for i, entry in ipairs(syntax_tree) do
print(level, i, entry.token, entry.block, entry.ifstmt or entry.elsestmt, entry.text or '')
if entry.token and entry.block then
print(" ", entry.token, entry.block)
end
if entry.block then
print_syntax_tree(level + 1, entry)
end
end
end]]
function spindle.find_tokens(block, ...)
local t = {}
for _, entry in ipairs(block) do
for _, v in ipairs {...} do
if entry.token == v then
table.insert(t, entry)
break
end
end
if entry.block then
local n = spindle.find_tokens(entry, ...)
for _, v in ipairs(n) do
table.insert(t, v)
end
n = nil
end
end
return t
end
function spindle.parse_markup(page, blob)
local syntax_tree = {}
local active = syntax_tree
local stack = {syntax_tree}
local is_block_raw = false
for line in spindle.iterate_lines(blob) do
local is_comment = false
local active = stack[#stack]
if line == "" then
if is_block_raw then
active.text = active.text .. '\n'
goto next_line
end
active[#active + 1] = { token = 'ws' }
goto next_line
end
if line:match('^%s*}') then
if is_block_raw then
if active.parens > 0 then
active.text = active.text .. '\n' .. line
active.parens = active.parens + spindle._balance_parens(line)
else
stack[#stack] = nil
is_block_raw = false
end
goto next_line
end
stack[#stack] = nil
if line:match("%s+else%s+") then
local block_id = line:match("%s+([%w_]+)%s+{$")
local new_block = {
token = block_id == "else" and 'block_default' or block_id,
elsestmt = true,
block = true,
}
active = stack[#stack]
active[#active + 1] = new_block
stack[#stack + 1] = new_block
end
goto next_line
end
if is_block_raw then
if #active.text > 0 then
active.text = active.text .. '\n' .. line
else
active.text = active.text .. line
end
active.parens = active.parens + spindle._balance_parens(line)
goto next_line
end
if line:match('^%s*/%s+') then
is_comment = true
line = line:gsub("^%s*/", "") -- remove the comment
end
-- check for variables
local x = line:match('^%s*([%w_]+)%s*=') or line:match('^%s*%[([%W]+)%]%s*=')
if x then
if is_comment then
goto next_line
end
local y = line:match("=%s*(.+)")
if line:match("{$") then
y = spindle.trim(y:sub(1, -2))
local new_block = { token = x, block = y }
active[#active + 1] = new_block
stack[#stack + 1] = new_block
goto next_line
end
if y == 'false' then y = false end
if y == 'true' then y = true end
if #stack == 1 then
page.vars[x] = y
end
active[#active + 1] = { decl = x, value = y }
goto next_line
end
local ifs = line:match('^%s*if%s+not%s+%%(.-)%s+.-{$')
if ifs then
local block_id = line:match("%s+([%w_]+)%s+{$")
local new_block = {
token = block_id or 'block_default',
ifstmt = ifs,
block = true,
inverse = true,
is_comment = is_comment,
}
active[#active + 1] = new_block
stack[#stack + 1] = new_block
goto next_line
end
local ifs = line:match('^%s*if%s+%%(.-)%s+.-{$')
if ifs then
local block_id = line:match("%s+([%w_]+)%s+{$")
local new_block = {
token = block_id or 'block_default',
ifstmt = ifs,
block = true,
is_comment = is_comment,
}
active[#active + 1] = new_block
stack[#stack + 1] = new_block
goto next_line
end
-- open block
local is_block = line:match('^%s*{$')
if is_block then
local new_block = { token = 'block_default', block = true, is_comment = is_comment }
active[#active + 1] = new_block
stack[#stack + 1] = new_block
goto next_line
end
local label = line:match('^%s*(.-)%s+{$')
if label then
label = spindle.split_fields(label)
local new_block = { token = label[1], is_comment = is_comment }
active[#active + 1] = new_block
stack[#stack + 1] = new_block
if #label > 1 and label[2] == 'raw' then
is_block_raw = true
new_block.raw = true
new_block.text = ""
new_block.parens = 0
else
new_block.block = true
end
goto next_line
end
if is_comment then
goto next_line
end
-- check for token/value pair
local x, y = line:match('^%s*(%S+)%s+(.-)$')
if x ~= nil and not x:match('%w') then
active[#active + 1] = { token = x, text = spindle.trim(y) }
goto next_line
end
line = spindle.trim(line)
local char = string.sub(line, 1, 1)
if char == '<' then
active[#active + 1] = { token = 'html', text = line }
else
active[#active + 1] = { token = 'default', text = line }
end
::next_line::
end
return syntax_tree
end
spindle.__parse_markup = spindle.parse_markup -- here to assist with hacking
function spindle.render(page, active_block)
local scope = {}
for k, v in pairs(spindle.tokens) do
scope[#scope + 1] = { decl = k, value = v }
end
return spindle.render_internal(page, scope, active_block)
end
function spindle.render_internal(page, scope, active_block)
local content = ""
local index = 0
local scope_frame = 0
while true do
index = index + 1
if index > #active_block then
break
end
local entry = active_block[index]
if entry.decl then
if page.vars.taxonomy_variable and page.vars.taxonomy_category then
if entry.decl == page.vars.taxonomy_variable and not string.lower(entry.value):match(string.lower(page.vars.taxonomy_category)) then
spindle.clear_scope_frame(scope, scope_frame)
return ""
end
end
scope[#scope + 1] = entry
scope_frame = scope_frame + 1
goto render_continue
end
if entry.token == "ws" then
goto render_continue
end
if entry.token == "html" then
content = content .. spindle.expand(page, scope, entry.text)
goto render_continue
end
if entry.block and entry.token then
if entry.is_comment then
goto render_continue
end
if entry.ifstmt then
local has_else = false
if index < #active_block and active_block[index + 1].elsestmt then
index = index + 1
has_else = true
end
local eval = (spindle.find_in_scope(scope, entry.ifstmt) or page.vars[entry.ifstmt]) and true or false
if entry.inverse then
eval = not eval
end
if not eval then
if has_else then
entry = active_block[index]
else
goto render_continue
end
end
end
content = content .. spindle.render_internal(page, scope, entry)
goto render_continue
end
if entry.token then
-- @todo function calls should *not* pass an arg0 function name
-- it's a waste of time and it's already more confusing than it
-- needs to be with Lua's off-by-one arrays
if entry.token == '$' then
local args = spindle.split_quoted(entry.text)
if _ENV[args[1]] then
-- we pre-expand here because inside imports
-- a function call essentially sanitises the
-- scope and we lose track of where we're
-- supposed to be.
local func = table.remove(args, 1)
for k, v in ipairs(args) do
args[k] = spindle.expand(page, scope, v)
end
-- we only, deliberately, accept the first return
local v = _ENV[func](page, args)
if v ~= nil then
content = content .. v
end
end
goto render_continue
elseif entry.token == '$$' then
local x = "local page = select(1, ...); " .. entry.text
local v = load(x)(page)
if v ~= nil then
content = content .. spindle.expand(page, scope, v)
end
goto render_continue
end
local t = spindle.find_in_scope(scope, entry.token) or page.vars[entry.token]
if t == nil then
entry.text = string.format("%s %s", entry.token, entry.text)
entry.token = 'default'
t = spindle.find_in_scope(scope, entry.token) or page.vars[entry.token]
end
local x = type(t)
if x == 'table' then
local tok = entry.token
local sub_content = ""
local count = 0
while true do
local entry = active_block[index]
if entry.token and entry.token:byte(1) ~= tok:byte(1) then
index = index - 1
break
end
local new_t
if entry.token and entry.token ~= tok then
new_t = spindle.find_in_scope(scope, entry.token) or page.vars[entry.token]
end
if new_t then
if type(new_t) == 'table' then
new_t = new_t.main
end
else
new_t = t.main
end
if entry.raw then
sub_content = sub_content .. spindle.template_expand(page, new_t, entry.text)
else
sub_content = sub_content .. spindle.expand(page, scope, spindle.template_expand(page, new_t, entry.text))
end
count = count + 1
index = index + 1
if index > #active_block then
break
end
end
if (not t.minimum) or t.minimum <= count then
if entry.raw then
content = content .. spindle.template_expand(page, t.wrap, sub_content)
else
content = content .. spindle.expand(page, scope, spindle.template_expand(page, t.wrap, sub_content))
end
else
content = content .. sub_content
end
goto render_continue
elseif x == 'string' then
if entry.raw then
content = content .. spindle.template_expand(page, t, entry.text)
else
content = content .. spindle.expand(page, scope, spindle.template_expand(page, t, entry.text))
end
goto render_continue
elseif x == 'function' then
local v = t(page, scope, entry.raw and entry.text or spindle.expand(page, scope, entry.text))
if v ~= nil then
content = content .. (entry.raw and v or spindle.expand(page, scope, v))
end
goto render_continue
end
end
::render_continue::
end
-- if this block is empty we don't bother creating the empty wrapper
if content == "" then
spindle.clear_scope_frame(scope, scope_frame)
return content
end
if active_block.block then
local t = spindle.find_in_scope(scope, active_block.token) or page.vars[active_block.token]
local x = type(t)
if x == 'string' then
local w = spindle.expand(page, scope, t)
content = spindle.template_expand(page, w, content)
elseif x == 'function' then
local v = t(page, scope, content)
if v ~= nil then
content = active_block.raw and v or spindle.expand(page, scope, v)
end
end
end
spindle.clear_scope_frame(scope, scope_frame)
return content
end
function spindle.clear_scope_frame(scope, scope_frame)
if scope_frame > 0 then
for i = #scope, 1, -1 do
scope[i] = nil
scope_frame = scope_frame - 1
if scope_frame == 0 then
break
end
end
end
end
function spindle.export_file(path)
local file_path = spindle.find_file(path)
if file_path == nil then
return false
end
spindle.copy_file(file_path, spindle.output_path .. file_path)
return true
end
-- returns filepath + url
function spindle.process_any_file(path)
if path == '/' then
return path, spindle.domain
end
if path == '' then
return nil, ""
end
if not spindle.has_protocol(path) then
local file_path = spindle.find_file(path)
if file_path == nil then
return nil, ""
end
local ext = spindle.short_ext(file_path)
if spindle.handlers[ext] then
return spindle.handlers[ext](file_path)
else
return spindle.default_handler(file_path)
end
end
return nil, path
end
function spindle.default_handler(file_path)
local file = {
source_path = file_path,
output_path = spindle.output_path .. file_path,
canonical_url = spindle.url_from_path(file_path)
}
spindle.all_files[file.output_path] = file
return file.output_path, file.canonical_url
end
function spindle.has_run(path)
if spindle.lock_file[path] == true then
return true
end
spindle.lock_file[path] = true
return false
end
function spindle.generate_sitemap(file_list)
local sitemap = [[<?xml version="1.0" encoding="utf-8" standalone="yes"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">]]
local schema = [[<url><loc>%s</loc></url>]]
for i, entry in ipairs(file_list) do
file_list[i] = string.format(schema, entry)
end
return sitemap .. table.concat(file_list, "") .. [[</urlset>]]
end
function spindle.make_page_list(reveal_hidden)
local file_list = {}
for i, page in pairs(spindle.all_pages) do
if reveal_hidden or not page.vars.spindle_hide_page then -- @docs
table.insert(file_list, page.canonical_url)
end
end
for i, file in pairs(spindle.all_files) do
if spindle.long_ext(file.output_path) == ".html" then
table.insert(file_list, file.canonical_url)
end
end
-- we do this extra sort so the output is idempotent
table.sort(file_list)
return file_list
end
function spindle.safe_import(file_name)
if spindle.file_exists(file_name) then
return dofile(file_name)
end
return nil
end
function main(starting_file)
spindle.safe_import("_data/config.lua")
spindle.process_any_file(starting_file)
local file_list = {}
for i, page in pairs(spindle.all_pages) do
page.slugs = {}
page.vars.content = spindle.render(page, page.syntax_tree)
-- @todo make some line-level syntax we can quickly parse to set the content anchor
-- without making it a native variable -- this lets us take the 'content' specific
-- hook out of spindle.expand and avoid all the double-expansion mess
if page.vars.template then
spindle.parse_markup = spindle.__parse_markup
local template = spindle.load_markup("_data/" .. page.vars.template .. ".x")
spindle.write_file(page.output_path, spindle.render(page, template.syntax_tree))
else
spindle.write_file(page.output_path, page.vars.content)
end
table.insert(file_list, page.source_path)
end
for i, file in pairs(spindle.all_files) do
spindle.make_directory(file.output_path)
if not spindle.copy_file(file.source_path, file.output_path) then
print("failed to copy", file.source_path)
end
table.insert(file_list, file.source_path)
end
if spindle.do_sitemap then
spindle.write_file(spindle.output_path .. "sitemap.xml", spindle.generate_sitemap(spindle.make_page_list()))
table.insert(file_list, "sitemap.xml")
end
table.sort(file_list)
for _, item in ipairs(file_list) do
print(item)
end
if #defer_list > 0 then
for _, f in ipairs(defer_list) do
f()
end
end
end