-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.lua
More file actions
1246 lines (1095 loc) · 29 KB
/
Copy pathmain.lua
File metadata and controls
1246 lines (1095 loc) · 29 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
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- Global error context
local processingContext = nil
local processingContext2 = nil
local onError = function (err)
local message = ""
if processingContext then
message = "While processing \"" .. processingContext .. "\""
if processingContext2 then
message = message .. " (" .. processingContext2 .. ")"
end
message = message .. ":\n\n"
end
return message .. debug.traceback(err, 3)
end
local function withErrorLogging(f)
local success, err = xpcall(f, onError)
if not success then
log.error(err)
os.exit(-1)
end
end
-- Support embedded scripts
local embeddedFiles = {}
local builtInThemes = {}
for _, name in ipairs(_embeddedScripts) do
embeddedFiles[name] = true
local theme = string.match(name, "themes/(.-)/theme%.lua")
if theme and theme ~= "shared" then
table.insert(builtInThemes, theme)
end
end
-- Need to hook loadfile since lexer.lua uses it directly...
package.path = package.path .. ";__builtin/?.lua"
local originalLoadFile = loadfile
loadfile = function(filename)
-- Need to hook loadfile since lexer.lua uses it to load scripts instead of using "require"
local name = string.match(filename, "^%__builtin/(.*%.lua)$")
if name and embeddedFiles[name] then
return _loadEmbeddedScript(name)
end
return originalLoadFile(filename)
end
-- Add a searcher to support "require" (note: built-ins are lowest priority, so they can be overridden locally)
table.insert(package.searchers, function (name)
local filename = string.gsub(name, "%.", "/") .. ".lua"
if embeddedFiles[filename] then
return _loadEmbeddedScript(filename)
end
end)
-- Helpers
local function check(f, ...)
local result, err = f(...)
if result then
return result
end
error(err)
end
local function loadOrError(ld, source, mode)
local result, err = load(ld, source, mode)
return result or error(err)
end
local function loadOrErrorInEnvironment(ld, source, mode, env)
-- Note: load()'s env argument distinguishes nil from "no argument", so
-- this needs to be a separate helper from loadOrError() above!
local result, err = load(ld,
source, mode, env)
return result or error(err)
end
log = {}
function log.error(message)
print("*** ERROR ***")
print(message)
end
function log.warn(message)
print("WARN:\t" .. message)
end
function log.info(message)
print("INFO:\t" .. message)
end
function table.merge(source, dest)
for k, v in pairs(source) do
dest[k] = v
end
end
function table.copy(t)
local r = {}
table.merge(t, r)
return r
end
function table.map(t, f)
local r = {}
for k, v in ipairs(t) do
r[k] = f(v)
end
return r
end
function table.sorted(t, compare)
local sorted = table.copy(t)
table.sort(sorted, compare)
return sorted
end
function table.sortBy(t, property, descending)
local sorted = table.copy(t)
if descending then
table.sort(sorted, function (a, b) return a[property] > b[property] end)
else
table.sort(sorted, function (a, b) return a[property] < b[property] end)
end
return sorted
end
function table.groupBy(t, key)
local groups = {}
for _, item in ipairs(t) do
local keyValue = item[key]
if keyValue then
-- Support single value or multiple
local set = keyValue
if type(keyValue) == "string" then
set = { keyValue }
end
for _, k in ipairs(set) do
if not groups[k] then
groups[k] = {}
end
table.insert(groups[k], item)
end
end
end
return groups
end
function table.concatenate(a, b)
local r = {}
for k, v in ipairs(a) do
r[k] = v
end
for _, v in ipairs(b) do
table.insert(r, v)
end
return r
end
function table.include(t, f)
local r = {}
for _, v in ipairs(t) do
if f(v) then
table.insert(r, v)
end
end
return r
end
function table.values(map)
local array = {}
for _, item in pairs(map) do
table.insert(array, item)
end
return array
end
iterator = {}
function iterator.count(iterator)
local count = 0
for _ in iterator do
count = count + 1
end
return count
end
function iterator.collect(iterator)
local result = {}
for item in iterator do
table.insert(result, item)
end
return result
end
-- String helpers
function string.toNumber(str)
return 0 + str
end
function string.charAt(str, i)
return string.sub(str, i, i)
end
function string.split(str, separator)
local i = 0
return function ()
if i > #str then
return nil
else
local j, j2 = string.find(str, separator, i)
if j then
local result = string.sub(str, i, j - 1)
i = j2 + 1
return result
else
local result = string.sub(str, i)
i = #str + 1
return result
end
end
end
end
function string.lines(str)
return string.split(str, "\r?\n")
end
function string.trim(str)
return string.match(str, "^%s*(.-)%s*$")
end
-- Frontmatter parsing
local function parseLua(lua, file)
local o = {}
loadOrErrorInEnvironment(lua, file or "frontmatter", "t", o)()
return o
end
local function unquote(str)
if string.charAt(str, 1) == "\"" and string.charAt(str, #str) == "\"" then
return string.gsub(string.sub(str, 2, -2), [[\"]], [["]])
else
return str
end
end
local function parseYamlValue(v)
local trimmed = string.trim(v)
-- Check if array
if string.charAt(trimmed, 1) == "[" and string.charAt(trimmed, #trimmed) == "]" then
return table.map(iterator.collect(string.split(string.sub(trimmed, 2, -2), ",")), parseYamlValue)
else
return unquote(trimmed)
end
end
local function parseYaml(yaml)
local o = {}
for line in string.lines(yaml) do
local k, v = string.match(line, "^(.-):(.+)")
if k and v then
o[k] = parseYamlValue(v)
end
end
return o
end
local function parseToml(toml)
local o = {}
for line in string.lines(toml) do
local k, v = string.match(line, "^(.-)%s-=(.+)")
if k and v then
o[k] = parseYamlValue(v)
end
end
return o
end
-- File system helpers
fs = {}
function fs.join(a, b)
if a == "" then
return b
elseif b == "" then
return a
else
return a .. "/" .. b
end
end
function fs.directory(path)
local dir = string.match(path, "(.*)/")
return dir or ""
end
-- Normalize path by 1) converting "\" to "/" and 2) resolving ".." and "."
function fs.normalize(path)
local results = {}
path = string.gsub(path, "\\", "/")
for part in string.split(path, "/") do
if part == "." then
-- Drop any "." components
elseif part == ".." then
if #results > 0 then
results[#results] = nil
else
-- Can't fully normalize; just return original path
return path
end
else
table.insert(results, part)
end
end
return table.concat(results, "/")
end
function fs.resolveRelative(base, relative)
return fs.normalize(fs.join(fs.directory(base), relative))
end
function fs.createDirectory(dir)
-- Create parent directories as needed
local last = #dir
local i = 1
while true do
local slash = string.find(dir, "/", i, true)
if slash then
_mkdir(string.sub(dir, 1, slash - 1))
if slash == last then
return
else
i = slash + 1
end
else
_mkdir(dir)
break
end
end
end
fs.listDirectory = _listDirectory
fs.isDirectory = _isDirectory
local function enumerateFilesRecursive(prefixLength, dir, files)
for _, name in ipairs(fs.listDirectory(dir)) do
local path = fs.join(dir, name)
if fs.isDirectory(path) then
enumerateFilesRecursive(prefixLength, path, files)
else
table.insert(files, string.sub(path, prefixLength + 1))
end
end
end
function fs.enumerateFiles(dir)
local prefixLength = #dir + 1
files = {}
enumerateFilesRecursive(prefixLength, dir, files)
return files
end
function fs.tryReadFile(path)
local f = (path == "-" and io.stdin) or io.open(path, "rb")
if f == nil then
return nil
end
local content = f:read("*a")
f:close()
return content
end
function fs.readFile(path)
local result = fs.tryReadFile(path)
if result then
return result
end
-- Fallback to built-in file, if available
local normalized = fs.normalize(path)
return (embeddedFiles[normalized] and _readEmbeddedFile(normalized))
or error("Could not open file: " .. path)
end
function fs.tryLoadFile(path)
local content = fs.tryReadFile(path)
if content then
return loadOrError(content, path, "t")
else
return nil
end
end
local themeDirectory = "."
function fs.readThemeFile(path)
return fs.readFile(fs.join(themeDirectory, path))
end
function fs.loadThemeFile(path)
local p = fs.join(themeDirectory, path)
return loadOrError(fs.readFile(p), p, "t")
end
function fs.doThemeFile(path)
return fs.loadThemeFile(path)()
end
function fs.writeFile(path, content)
local f = io.open(path, "wb")
f:write(content)
f:close()
end
function fs.copyFile(source, destination)
_copyFile(source, destination)
end
local function computePathToRoot(path)
return string.rep("../", iterator.count(string.gmatch(path, "/")))
end
-- URL helpers
url = {}
function url.isRelative(url)
return not string.find(url, ":")
end
-- Misc. helpers
lib = {}
lib.item = {}
function lib.item.repathRelativeLinks(item, prefix)
local parts = {}
local pathFromRoot = fs.directory(item.path)
_parseHtml(item.content, function (event)
local part = event.html
-- Repath relative links, if needed
if pathFromRoot ~= "" and event.attribute and event.value
and((event.tag == "a" and event.attribute == "href") -- Check for links
or (event.tag == "link" and event.attribute == "href")
or (event.tag == "script" and event.attribute == "src")
or (event.tag == "img" and event.attribute == "src"))
and not string.find(event.value, ":") -- Local/relative links only
then
part = event.attribute .. "=\"" .. (prefix or "") .. fs.join(pathFromRoot, event.value) .. "\""
end
table.insert(parts, part)
end)
return table.concat(parts)
end
function lib.item.createTableOfContents(item)
local level = nil
local min = nil
local toc = { }
local position = 1
local count = 0
while true do
local i, j, l, id, title = string.find(item.content, "<h([1-6]) -id=\"(.-)\" ->(.-)</h[1-6]>", position)
if i then
l = l + 0
if level then
if l < min then
log.warn("Invalid document structure in " .. item.path .. " for \"" .. id .. "\": encountered h" .. l .. " under h" .. min .. "!")
while l < min do
table.insert(toc, 1, "<ol>\n")
min = min - 1
end
end
while l > level do
table.insert(toc, "<ol>\n")
level = level + 1
end
while l < level do
table.insert(toc, "</ol>\n")
level = level - 1
end
else
level = l
min = level
table.insert(toc, "<ol>\n")
end
table.insert(toc, "<li><a href=\"#" .. id .. "\">" .. title .. "</a></li>\n")
count = count + 1
position = j + 1
else
break
end
end
if count > 1 then
while level >= min do
table.insert(toc, "</ol>\n")
level = level - 1
end
return table.concat(toc)
end
return ""
end
local function enrichItem(path, item)
if not item.path then
item.path = path
end
if not item.pathToRoot then
item.pathToRoot = computePathToRoot(item.path)
end
end
local function enrichItems(items)
for path, item in pairs(items) do
item.path = path
item.pathToRoot = computePathToRoot(path)
end
end
local function shouldInclude(path, pattern, item)
if pattern then
if type(pattern) == "string" then
return string.match(path, pattern)
elseif type(pattern) == "function" then
return pattern(item)
end
end
return true
end
function createTransformNode(transform, pattern)
return function (items)
local changes = {}
for path, item in pairs(items) do
processingContext = path
if shouldInclude(path, pattern, item) then
local originalPath = path
item.self = item
transform(item)
if originalPath ~= item.path then
table.insert(changes, { originalPath, item.path })
item.pathToRoot = nil
end
enrichItem(item.path, item)
end
processingContext = nil
end
for _, change in ipairs(changes) do
items[change[2]] = items[change[1]]
items[change[1]] = nil
end
end
end
function createAggregateNode(aggregate, pattern)
return function (items)
-- Find items
local included = {}
for path, item in pairs(items) do
if shouldInclude(path, pattern, item) then
table.insert(included, item)
end
end
-- Run aggregation
local outputItems = aggregate(included)
for _, item in ipairs(outputItems) do
enrichItem(item.path, item)
items[item.path] = item
end
end
end
-- Source/sink nodes
local fileItemMetatable = {
__index = function (table, key)
if key == "content" then
-- Read file and de-virtualize
local content = fs.readFile(rawget(table, "__luasmithSourcePath"))
rawset(table, "content", content)
rawset(table, "__luasmithSourcePath", nil)
setmetatable(table, nil)
end
return rawget(table, key)
end,
__newindex = function (table, key, value)
if key == "content" then
-- De-virtualize
rawset(table, "__luasmithSourcePath", nil)
setmetatable(table, nil)
end
return rawset(table, key, value)
end,
}
local function createFileItem(dir, path)
-- Virtualize item, to avoid reading content into memory if never manipulated
local item = { __luasmithSourcePath = fs.join(dir, path) }
enrichItem(path, item)
setmetatable(item, fileItemMetatable)
return item
end
injectFiles = function (files)
return function (items)
for path, content in pairs(files) do
local item = { content = content }
enrichItem(path, item)
items[path] = item
end
end
end
readFromSource = function (dir, pattern)
local item = {}
return function (items)
for _, path in ipairs(fs.enumerateFiles(dir)) do
item.path = path
if shouldInclude(path, pattern, item) then
processingContext = path
items[path] = createFileItem(dir, path)
processingContext = nil
end
end
end
end
writeToDestination = function (dir, pattern)
return function (items)
-- Check for unspecified content first
local dirsToCreate = {}
local filesToWrite = {}
for path, item in pairs(items) do
processingContext = path
if shouldInclude(path, pattern, item) then
if not rawget(item, "content") and not rawget(item, "__luasmithSourcePath") then
error("Item " .. path .. " does not have any content specified (content property is nil)! Did you forget to apply a template? (To generate an empty file, set the content property to an empty string.)")
end
local localPath = fs.join(dir, path)
dirsToCreate[fs.directory(localPath)] = true
filesToWrite[localPath] = item
end
processingContext = nil
end
-- Now actually write everything to disk
for path in pairs(dirsToCreate) do
processingContext = path
fs.createDirectory(path)
processingContext = nil
end
for path, item in pairs(filesToWrite) do
processingContext = path
-- Check for virtualized file
local rawContent = rawget(item, "content")
if rawContent then
fs.writeFile(path, rawContent)
else
local sourcePath = rawget(item, "__luasmithSourcePath")
fs.copyFile(sourcePath, path)
end
processingContext = nil
end
end
end
omitWhen = function (test, pattern)
return function (items)
local deletes = {}
for path, item in pairs(items) do
processingContext = path
if shouldInclude(path, pattern) and test(item) then
table.insert(deletes, path)
end
processingContext = nil
end
for _, path in ipairs(deletes) do
items[path] = nil
end
end
end
-- Transform nodes
markdown = {}
markdown.toHtml = _markdownToHtml
local function sluggify(slugs, html)
-- NOTE: Sadly, this hack only supports ASCII
-- Remove HTML (elements and entities) and disallowed characters, change to
-- lowercase, and replace spaces with hyphens
local result = html
result = string.gsub(result, "<.->", "")
result = string.gsub(result, "&.-;", "")
result = string.gsub(result, "[^a-zA-Z0-9 _%-]", "")
result = string.lower(result)
result = string.gsub(result, " ", "-")
-- Check for duplicates and make unique
local seenBefore = slugs[result]
if seenBefore then
slugs[result] = seenBefore + 1
result = result .. "-" .. seenBefore
else
slugs[result] = 1
end
return result
end
local function postProcessMarkdown(content)
local parts = {}
local position = 1
local slugs = {}
while true do
local i, j, inner = string.find(content, "<h[1-6]>(.-)</h[1-6]>", position)
if i then
local id = sluggify(slugs, inner)
table.insert(parts, string.sub(content, position, i + 2))
table.insert(parts, " id=\"")
table.insert(parts, id)
table.insert(parts, "\">")
table.insert(parts, string.sub(content, i + 4, j))
position = j + 1
else
table.insert(parts, string.sub(content, position))
break
end
end
return table.concat(parts)
end
local function parseFrontmatter(item)
local i, j, frontmatter
-- Parse YAML frontmatter
i, j, frontmatter = string.find(item.content, "^%-%-%-\r?\n(.-)\r?\n%-%-%-\r?\n")
if i and j and frontmatter then
table.merge(parseYaml(frontmatter), item)
item.content = string.sub(item.content, j + 1)
return
end
-- Parse Lua frontmatter
i, j, frontmatter = string.find(item.content, "^%[%[\r?\n(.-)\r?\n%]%]\r?\n")
if i and j and frontmatter then
table.merge(parseLua(frontmatter, item.path), item)
item.content = string.sub(item.content, j + 1)
return
end
-- Parse TOML frontmatter
i, j, frontmatter = string.find(item.content, "^%+%+%+\r?\n(.-)\r?\n%+%+%+\r?\n")
if i and j and frontmatter then
table.merge(parseToml(frontmatter), item)
item.content = string.sub(item.content, j + 1)
return
end
end
extractFrontmatter = function (pattern)
return createTransformNode(function (item)
parseFrontmatter(item)
end, pattern)
end
processMarkdown = function ()
return createTransformNode(function (item)
parseFrontmatter(item)
item.path = string.gsub(item.path, "%.md$", ".html")
item.content = markdown.toHtml(item.content)
-- TODO: This is a quick hack to add ids to headers. Ideally, this
-- would be integrated into md4c, and written in C.
item.content = postProcessMarkdown(item.content)
end,
"%.md$")
end
local grammars = {}
lexer = require("lexer")
local function tryLoadGrammar(language, aliases)
-- Cache result of trying to load grammar
local cached = grammars[language]
if cached == nil then
local success, grammar = pcall(function ()
return lexer.load(language)
end)
if success then
grammars[language] = grammar
return grammar
end
local alias = aliases[language]
if alias then
grammar = tryLoadGrammar(alias, aliases)
if grammar then
grammars[language] = grammar
return grammar
end
end
-- Distinguish "not found" from error
if package.searchpath(language, package.path) then
log.info("Error occurred while loading highlighter for: " .. language)
lexer.load(language)
else
log.info("Syntax highlighting not available for: " .. language)
end
grammars[language] = false
return nil
else
return cached or nil
end
end
local function unescapeHtml(html)
html = string.gsub(html, "'", "'")
html = string.gsub(html, "'", "'")
html = string.gsub(html, """, "\"")
html = string.gsub(html, "<", "<")
html = string.gsub(html, ">", ">")
html = string.gsub(html, "&", "&")
return html
end
local function escapeHtml(raw)
raw = string.gsub(raw, "&", "&")
raw = string.gsub(raw, ">", ">")
raw = string.gsub(raw, "<", "<")
raw = string.gsub(raw, "\"", """)
raw = string.gsub(raw, "'", "'")
return raw
end
local function highlightSpanDefault(verbatim, tag)
return "<span class=\"hl-" .. tag .. "\">" .. verbatim .. "</span>"
end
local function highlightSyntaxInternal(language, escaped, aliases, highlightSpan)
local parser = tryLoadGrammar(language, aliases)
if parser then
local parts = {}
local code = unescapeHtml(escaped)
tokens = parser:lex(code)
local prev = 1
for i = 1, #tokens, 2 do
-- Convert e.g. "string.longstring" to just "string"
local tag = string.gsub(tokens[i], "%..*", "")
local raw = string.sub(code, prev, tokens[i+1] - 1)
local verbatim = escapeHtml(raw)
-- Don't bother tagging whitespace
if string.find(tag, "whitespace") then
table.insert(parts, verbatim)
else
table.insert(parts, highlightSpan(verbatim, tag))
end
prev = tokens[i+1]
end
return table.concat(parts)
end
return escaped
end
highlightSyntax = function (optionsOrHighlightSpan)
-- Need to handle highlightSpan instead of options for backcompat
local highlightSpan
local aliases
if type(optionsOrHighlightSpan) == "function" then
highlightSpan = optionsOrHighlightSpan
elseif type(optionsOrHighlightSpan) == "table" then
local options = optionsOrHighlightSpan
highlightSpan = options.highlightSpan
aliases = options.aliases
end
highlightSpan = highlightSpan or highlightSpanDefault
aliases = aliases or {}
return createTransformNode(function (item)
local inPre = false
local inCode = false
local language = nil
local codeParts = nil
local htmlParts = {}
-- State machine using inPre, inCode, and language
local handleTag = {
["pre"] = function () inPre = true end,
["/pre"] = function () inPre = false end,
["code"] = function (event)
if not language then
if event.attribute == "class" then
language = string.match(event.value, "^language%-(.*)$")
if language then
-- Start accumulating code chunks
codeParts = {}
end
end
end
end,
["/code"] = function () inCode = false end,
}
_parseHtml(item.content, function (event)
local html = nil
if language then
-- In a code block; accumulate text nodes or output at end
local kind = event.event
if kind == "other" then
-- Accumulate code
table.insert(codeParts, event.html)
html = ""
elseif kind ~= "exit" then
-- Highlight and output code
local code = table.concat(codeParts)
if code ~= "" then
html = highlightSyntaxInternal(language, code, aliases, highlightSpan) .. event.html
end
-- Reset
language = nil
codeParts = nil
end
else
-- State machine transitions
local handler = handleTag[event.tag]
if handler then
handler(event)
else
language = nil
end
end
html = html or event.html
table.insert(htmlParts, html)
end)
item.content = table.concat(htmlParts)
end,
"%.html$")
end
processEtlua = function (pattern)
return createTransformNode(function (item)
item.content = etlua.render(item.content, item)
end,
pattern or "%.md$")
end
injectMetadata = function (properties, pattern)
return createTransformNode(function (item)
table.merge(properties, item)
end,
pattern)
end
deriveMetadata = function (derivations, pattern)
return createTransformNode(function (item)
for key, f in pairs(derivations) do
item[key] = f(item)
end
end,
pattern)
end
-- Note: Needed to rename etlua's module to not collide with any etlua lexer...
etlua = require("_etlua")
-- Wrap etlua to ensure errors are fatal
local function wrapEtlua(f)
return function (...)
return check(f, ...)
end
end
etlua.compile = wrapEtlua(etlua.compile)
etlua.render = wrapEtlua(etlua.render)
applyTemplates = function(templates)