-
Notifications
You must be signed in to change notification settings - Fork 5
/
llmk.lua
executable file
·1608 lines (1369 loc) · 39.7 KB
/
llmk.lua
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
#!/usr/bin/env texlua
--
-- This is file `llmk.lua'.
--
-- Copyright 2018-2023 Takuto Asakura (wtsnjp)
-- GitHub: https://github.com/wtsnjp
-- Twitter: @wtsnjp
--
-- This sofware is released under the MIT License.
--
local llmk = {} -- the module table
----------------------------------------
do -- The "core" submodule
local M = {}
-- option flags (default)
M.debug = {
config = false,
parser = false,
run = false,
fdb = false,
programs = false,
}
M.verbosity_level = 1
M.silent = false
M.dry_run = false
llmk.core = M
end
----------------------------------------
do -- The "const" submodule
local M = {}
-- program information
M.prog_name = 'llmk'
M.version = '1.2.0'
M.copyright = 'Copyright 2018-2023'
M.author = 'Takuto Asakura (wtsnjp)'
M.llmk_toml = 'llmk.toml'
-- exit codes
M.exit_ok = 0
M.exit_error = 1
M.exit_failure = 2
M.exit_parser = 3
M.exit_type = 4
-- config item specification
M.top_level_spec = {
-- <program> = {<type>, <default value>}
bibtex = {'string', 'bibtex'},
clean_files = {'[string]', {
'%B.aux', '%B.bbl', '%B.bcf', '%B-blx.bib', '%B.blg', '%B.fls',
'%B.idx', '%B.ilg', '%B.ind', '%B.lof', '%B.log', '%B.lot', '%B.nav',
'%B.out', '%B.run.xml', '%B.snm', '%B.toc', '%B.vrb',
}},
clobber_files = {'[string]', {'%B.dvi', '%B.pdf', '%B.ps', '%B.synctex.gz'}},
dvipdf = {'string', 'dvipdfmx'},
dvips = {'string', 'dvips'},
extra_clean_files = {'[string]', {}},
latex = {'string', 'lualatex'},
llmk_version = {'string', nil},
makeindex = {'string', 'makeindex'},
makeglossaries = {'string', 'makeglossaries'},
max_repeat = {'integer', 5},
output_directory = {'string', nil},
ps2pdf = {'string', 'ps2pdf'},
sequence = {'[string]', {'latex', 'bibtex', 'makeindex', 'makeglossaries', 'dvipdf'}},
source = {'*[string]', nil},
}
M.program_spec = {
-- <item> = {<type>, {<specifiers allowed>, <default value>}}
args = {'*[string]', {true, {'%T'}}},
aux_file = {'string', {true, nil}},
aux_empty_size = {'integer', {false, nil}},
command = {'string', {false, ''}}, -- '' default because it must be string
generated_target = {'boolean', {false, false}},
opts = {'*[string]', {true, nil}},
postprocess = {'string', {false, nil}},
target = {'string', {true, '%S'}},
}
M.default_programs = {
bibtex = {
target = '%B.bib',
args = {'%B'}, -- "%B.bib" will result in an error
postprocess = 'latex',
},
dvipdf = {
target = '%B.dvi',
generated_target = true,
},
dvips = {
target = '%B.dvi',
generated_target = true,
},
latex = {
opts = {
'-interaction=nonstopmode',
'-file-line-error',
'-synctex=1',
'-output-directory="%o"',
},
aux_file = '%B.aux',
aux_empty_size = 9, -- "\\relax \n" is empty
},
makeindex = {
target = '%B.idx',
generated_target = true,
postprocess = 'latex',
},
makeglossaries = {
target = '%B.glo',
generated_target = true,
postprocess = 'latex',
opts = {'-d "%o"'},
args = {'%b.glo'}, -- "%B.glo" will result in an error
},
ps2pdf = {
target = '%B.ps',
generated_target = true,
},
}
llmk.const = M
end
----------------------------------------
do -- The "util" submodule
local M = {}
local function log(label, msg, ...)
local prefix = llmk.const.prog_name .. ' ' .. label .. ': '
io.stderr:write(prefix .. msg:format(...) .. '\n')
end
function M.err_print(err_type, msg, ...)
if err_type == 'error' then
-- error must be reported
elseif err_type == 'info' then
if llmk.core.verbosity_level < 2 then return end
elseif err_type == 'warning' then
if llmk.core.verbosity_level < 1 then return end
end
log(err_type, msg, ...)
end
function M.dbg_print(dbg_type, msg, ...)
if llmk.core.debug[dbg_type] then
log('debug-' .. dbg_type, msg, ...)
end
end
function M.dbg_print_table(dbg_type, table)
if not llmk.core.debug[dbg_type] then return end
local function helper(tab, ind)
local function pp(msg, ...)
M.dbg_print(dbg_type, string.rep(' ', ind) .. msg, ...)
end
for k, v in pairs(tab) do
if type(v) == 'table' then
pp(k .. ':')
helper(v, ind + 2)
elseif type(v) == 'string' then
pp(k .. ': "%s"', v)
else -- number, boolean, etc.
pp(k .. ': %s', tostring(v))
end
end
end
helper(table, 2)
end
function M.get_status(raw)
if os.type == 'windows' then
return raw
else
return raw / 256
end
end
-- Replace config param to filename
function M.replace_specifiers(str, source, target, output_directory)
local tmp = '/' .. source
local basename_match = tmp:match('^.*/(.*)%..*$')
str = str:gsub('%%S', source)
str = str:gsub('%%T', target)
local sub_basename = source
if basename_match then
sub_basename = basename_match
end
local sub_output_directory = '.'
local sub_output_directory_basename = sub_basename
if output_directory then
sub_output_directory = output_directory
sub_output_directory_basename = output_directory .. '/' .. sub_basename
end
str = str:gsub('%%b', sub_basename)
str = str:gsub('%%o', sub_output_directory)
str = str:gsub('%%B', sub_output_directory_basename)
return str
end
llmk.util = M
end
----------------------------------------
do -- The "checker" submodule
local M = {}
local function checked_value(k, v, expected)
local function error_if_wrong_type(val, t)
if type(val) ~= t then
llmk.util.err_print('error',
'[Type Error] Key "%s" must have value of type %s', k, expected)
os.exit(llmk.const.exit_type)
end
end
if expected == 'integer' then
error_if_wrong_type(v, 'number')
elseif expected == 'boolean' then
error_if_wrong_type(v, 'boolean')
elseif expected == 'string' then
error_if_wrong_type(v, 'string')
elseif expected == '[string]' then
error_if_wrong_type(v, 'table')
if v[1] ~= nil then -- it is not an empty array
error_if_wrong_type(v[1], 'string')
end
elseif expected == '*[string]' then
if type(v) == 'string' then
v = {v}
else
error_if_wrong_type(v, 'table')
if v[1] ~= nil then -- it is not an empty array
error_if_wrong_type(v[1], 'string')
end
end
end
return v
end
local function type_check(tab)
local new_top = {}
for k, v in pairs(tab) do
if k == 'programs' then
if type(v) ~= 'table' then
llmk.util.err_print('error', '[Type Error] Key "programs" must be a table')
os.exit(llmk.const.exit_type)
end
local new_prog = {}
for p_name, p_val in pairs(v) do
if type(p_val) ~= 'table' then
llmk.util.err_print('error',
'[Type Error] Key "programs.%s" must be a table', p_name)
os.exit(llmk.const.exit_type)
else
new_prog[p_name] = {}
for ik, iv in pairs(p_val) do
if not llmk.const.program_spec[ik] then
llmk.util.err_print('warning',
'Program key "%s" is unknown; ignoring it', ik)
else
expected = llmk.const.program_spec[ik][1]
new_prog[p_name][ik] = checked_value(ik, iv, expected)
end
end
end
end
new_top[k] = new_prog
else
if not llmk.const.top_level_spec[k] then
llmk.util.err_print('warning',
'Top-level key "%s" is unknown; ignoring it', k)
else
expected = llmk.const.top_level_spec[k][1]
new_top[k] = checked_value(k, v, expected)
end
end
end
return new_top
end
local function version_check(given_version)
if not given_version then -- nothing to do
return
end
-- parse the given version to the llmk_version key
local given_major, given_minor = given_version:match('^(%d+)%.(%d+)')
if not given_major or not given_minor then
llmk.util.err_print('warning', 'In valid llmk_version: ' .. given_version)
return
else
given_major, given_minor = tonumber(given_major), tonumber(given_minor)
end
-- the version of this program
local major, minor = llmk.const.version:match('^(%d+)%.(%d+)')
major, minor = tonumber(major), tonumber(minor)
-- warn if this program is older than the given version
if major < given_major or (major == given_major and minor < given_minor) then
llmk.util.err_print('warning',
'This program (v%d.%d) is older than the specified llmk_version (v%d.%d)',
major, minor, given_major, given_minor)
end
-- warn if the given mojor version is older than this program
if given_major < major then
llmk.util.err_print('warning',
'The specified llmk_version (v%d.%d) is older than this program (v%d.%d)',
given_major, given_minor, major, minor)
end
-- Note: no breaking change has been made (yet)
end
function M.check(tab)
local new_tab = type_check(tab)
version_check(new_tab.llmk_version)
return new_tab
end
llmk.checker = M
end
----------------------------------------
do -- The "config" submodule
local M = {}
local function init_config()
local config = {}
for k, v in pairs(llmk.const.top_level_spec) do
config[k] = v[2]
end
config.programs = llmk.const.default_programs
return config
end
-- copy command name from top level
local function fetch_from_top_level(config, name)
if config.programs[name] then
if not config.programs[name].command and config[name] then
config.programs[name].command = config[name]
end
end
return config
end
local function update_config(config, tab)
-- merge the table from TOML
local function merge_table(tab1, tab2)
for k, v in pairs(tab2) do
if k == 'programs' then
local programs1 = tab1[k]
local programs2 = tab2[k]
for i_k, i_v in pairs(programs2) do
if type(programs1[i_k]) == 'table' then
for ii_k, ii_v in pairs(programs2[i_k]) do
programs1[i_k][ii_k] = ii_v
end
else
programs1[i_k] = i_v
end
end
else
tab1[k] = v
end
end
return tab1
end
local config = merge_table(config, tab)
-- set essential program names from top-level
local prg_names = {'latex', 'bibtex', 'makeindex', 'makeglossaries', 'dvipdf', 'dvips', 'ps2pdf'}
for _, name in pairs(prg_names) do
config = fetch_from_top_level(config, name)
end
-- show config table (for debug)
llmk.util.dbg_print('config', 'The final config table is as follows:')
llmk.util.dbg_print_table('config', config)
return config
end
function M.fetch_from_latex_source(fn)
local tab
local config = init_config()
-- get TOML field and parse it
local toml, line = llmk.parser.get_toml(fn)
if toml == '' then
llmk.util.err_print('warning',
'Neither TOML field nor magic comment is found in "%s"; ' ..
'using default config', fn)
end
tab = llmk.parser.parse_toml(toml, {fn, line})
-- check input and merge it to the config
tab = llmk.checker.check(tab)
config = update_config(config, tab)
return config
end
function M.fetch_from_llmk_toml()
local tab
local config = init_config()
local f = io.open(llmk.const.llmk_toml)
if f ~= nil then
local toml = f:read('*all')
tab = llmk.parser.parse_toml(toml, {llmk.const.llmk_toml, 1})
f:close()
else
llmk.util.err_print('error', 'No target specified and no %s found',
llmk.const.llmk_toml)
os.exit(llmk.const.exit_error)
end
-- check input and merge it to the config
tab = llmk.checker.check(tab)
config = update_config(config, tab)
return config
end
llmk.config = M
end
----------------------------------------
do -- The "parser" submodule
--[[
This TOML parser is modified version of toml.lua
- Copyright 2017 Jonathan Stoler
- Licensed under MIT
https://github.com/jonstoler/lua-toml/blob/master/LICENSE
]]
local M = {}
function M.parse_toml(toml, file_info)
-- basic local variables
local ws = '[\009\032]'
local nl = '[\10\13\10]'
local buffer = ''
local cursor = 1
local line = 0
local res = {}
local obj = res
-- basic local functions
local function parser_err(msg)
local function get_toml_str(nol)
local pattern = string.format('([^%s]*)%s', nl:sub(2, -2), nl)
local l = 0
for cur_line in toml:gmatch(pattern) do
if l == nol then
return cur_line
end
l = l + 1
end
end
llmk.util.err_print('error', '[Parse Error] %s', msg)
llmk.util.err_print('error', '--> %s:%d: %s',
file_info[1], file_info[2] + line, get_toml_str(line))
os.exit(llmk.const.exit_parser)
end
local function char(n)
n = n or 0
return toml:sub(cursor + n, cursor + n)
end
local function step(n)
n = n or 1
for i = 0, n-1 do
if char(i):match(nl) then
line = line + 1
end
end
cursor = cursor + n
end
local function skip_ws()
while(char():match(ws)) do
step()
end
end
local function trim(str)
return str:gsub('^%s*(.-)%s*$', '%1')
end
local function bounds()
return cursor <= toml:len()
end
-- parse functions for each type
local function parse_string()
-- TODO: multiline
local del = char() -- ' or "
local str = ''
-- all available escape characters
local escape = {
b = "\b",
t = "\t",
n = "\n",
f = "\f",
r = "\r",
['"'] = '"',
["\\"] = "\\",
}
-- utf function from http://stackoverflow.com/a/26071044
-- converts \uXXX into actual unicode
local function utf(char)
local bytemarkers = {{0x7ff, 192}, {0xffff, 224}, {0x1fffff, 240}}
if char < 128 then return string.char(char) end
local charbytes = {}
for bytes, vals in pairs(bytemarkers) do
if char <= vals[1] then
for b = bytes + 1, 2, -1 do
local mod = char % 64
char = (char - mod) / 64
charbytes[b] = string.char(128 + mod)
end
charbytes[1] = string.char(vals[2] + char)
break
end
end
return table.concat(charbytes)
end
-- skip the quotes
step()
while(bounds()) do
-- end of string
if char() == del then
step()
break
end
if char():match(nl) then
parser_err('Single-line string cannot contain line break')
end
if del == '"' and char() == '\\' then -- process escape characters
if escape[char(1)] then
-- normal escape
str = str .. escape[char(1)]
step(2) -- go past backslash and the character
elseif char(1) == 'u' then
-- utf-16
step()
local uni = char(1) .. char(2) .. char(3) .. char(4)
step(5)
uni = tonumber(uni, 16)
if (uni >= 0 and uni <= 0xd7ff) and not (uni >= 0xe000 and uni <= 0x10ffff) then
str = str .. utf(uni)
else
parser_err('Unicode escape is not a Unicode scalar')
end
elseif char(1) == 'U' then
-- utf-32
step()
local uni = char(1) .. char(2) .. char(3) .. char(4) ..
char(5) .. char(6) .. char(7) .. char(8)
step(9)
uni = tonumber(uni, 16)
if (uni >= 0 and uni <= 0xd7ff) and not (uni >= 0xe000 and uni <= 0x10ffff) then
str = str .. utf(uni)
else
parser_err('Unicode escape is not a Unicode scalar')
end
else
parser_err('Invalid escape')
end
else -- literal string; leave as it is
str = str .. char()
step()
end
end
return str
end
local function parse_number()
-- TODO: exp, date
local num = ''
while(bounds()) do
if char():match('[%+%-%.eE_0-9]') then
if char() ~= '_' then
num = num .. char()
end
elseif char():match(nl) then
break
elseif char():match(ws) or char() == '#' then
break
else
parser_err('Invalid number')
end
step()
end
return tonumber(num)
end
local get_value
local function parse_array()
step()
skip_ws()
local a_type
local array = {}
while(bounds()) do
if char() == ']' then
break
elseif char():match(nl) then
step()
skip_ws()
elseif char() == '#' then
while(bounds() and not char():match(nl)) do
step()
end
else
local v = get_value()
if not v then break end
if a_type == nil then
a_type = type(v)
elseif a_type ~= type(v) then
parser_err('Mixed types in array')
end
array = array or {}
table.insert(array, v)
if char() == ',' then
step()
end
skip_ws()
end
end
step()
return array
end
local function parse_boolean()
local bool
if toml:sub(cursor, cursor + 3) == 'true' then
step(4)
bool = true
elseif toml:sub(cursor, cursor + 4) == 'false' then
step(5)
bool = false
else
parser_err('Invalid primitive')
end
skip_ws()
if char() == '#' then
while(not char():match(nl)) do
step()
end
end
return bool
end
-- judge the type and get the value
get_value = function()
if (char() == '"' or char() == "'") then
return parse_string()
elseif char():match('[%+%-0-9]') then
return parse_number()
elseif char() == '[' then
return parse_array()
-- TODO: array of table, inline table
else
return parse_boolean()
end
end
-- main loop of parser
while(cursor <= toml:len()) do
-- ignore comments and whitespace
if char() == '#' then
while(not char():match(nl)) do
step()
end
end
if char() == '=' then
step()
skip_ws()
-- prepare the key
local key = trim(buffer)
buffer = ''
if key == '' then
parser_err('Empty key name')
elseif obj[key] then
-- duplicate keys are not allowed
parser_err('Cannot redefine key "' .. key .. '"')
end
local value = get_value()
if value ~= nil then
obj[key] = value
--dbg_print('parser', 'Entry "' .. key .. ' = ' .. value .. '"')
end
-- skip whitespace and comments
skip_ws()
if char() == '#' then
while(bounds() and not char():match(nl)) do
step()
end
end
-- if garbage remains on this line, raise an error
if not char():match(nl) and cursor < toml:len() then
parser_err('Invalid primitive')
end
elseif char() == '[' then
buffer = ''
step()
local table_array = false
if char() == '[' then
table_array = true
step()
end
obj = res
local function process_key(is_last)
is_last = is_last or false
buffer = trim(buffer)
if buffer == '' then
parser_err('Empty table name')
end
if is_last and obj[buffer] and not table_array and #obj[buffer] > 0 then
parser_err('Cannot redefine tabel')
end
if table_array then
if obj[buffer] then
obj = obj[buffer]
if is_last then
table.insert(obj, {})
end
obj = obj[#obj]
else
obj[buffer] = {}
obj = obj[buffer]
if is_last then
table.insert(obj, {})
obj = obj[1]
end
end
else
obj[buffer] = obj[buffer] or {}
obj = obj[buffer]
end
end
while(bounds()) do
if char() == ']' then
if table_array then
if char(1) ~= ']' then
parser_err('Mismatching brackets')
else
step()
end
end
step()
process_key(true)
buffer = ''
break
--elseif char() == '"' or char() == "'" then
-- TODO: quoted keys
elseif char() == '.' then
step()
process_key()
buffer = ''
else
buffer = buffer .. char()
step()
end
end
buffer = ''
--elseif (char() == '"' or char() == "'") then
-- TODO: quoted keys
end
-- put the char to the buffer and proceed
buffer = buffer .. (char():match(nl) and '' or char())
step()
end
return res
end
function M.get_toml(fn)
local toml = ''
local toml_field = false
local toml_source = fn
local f = io.open(toml_source)
llmk.util.dbg_print('config', 'Looking for config in the file "%s"', toml_source)
local ts_tmp
local ts_latex
local ts_bibtex
local first_line = true
local shebang
local line = 0
local start_pos = -1
for l in f:lines() do
line = line + 1
-- 1. llmk-style TOML field
if string.match(l, '^%s*%%%s*%+%+%++%s*$') then
-- NOTE: only topmost field is valid
if not toml_field then
toml_field = true
start_pos = line + 1
else
llmk.util.dbg_print('config', 'TOML field found')
break
end
else
if toml_field then
local match = string.match(l, '^%s*%%%s*(.-)%s*$')
if match == nil then
llmk.util.err_print('error', 'Encounter invalid line in TOML field')
os.exit(llmk.const.exit_error)
else
toml = toml .. match .. '\n'
end
end
end
-- 2. TeXShop directives
ts_tmp = string.match(l, '^%s*%%%s*!%s*TEX%s+program%s*=%s*(.-)%s*$') or
string.match(l, '^%s*%%%s*!%s*TeX%s+program%s*=%s*(.-)%s*$') or
string.match(l, '^%s*%%%s*!%s*TEX%s+TS%-program%s*=%s*(.-)%s*$')
if ts_tmp then
ts_latex = ts_latex or ts_tmp
end
ts_tmp = string.match(l, '^%s*%%%s*!%s*BIB%s+program%s*=%s*(.-)%s*$') or
string.match(l, '^%s*%%%s*!%s*BIB%s+TS%-program%s*=%s*(.-)%s*$')
if ts_tmp then
ts_bibtex = ts_bibtex or ts_tmp
end
-- 3. shebang
if first_line then
first_line = false
shebang = string.match(l, '^%s*%%#!%s*(.-)%s*$')
end
end
f:close()
-- convert magic or shebang to TOML
if toml == '' and (ts_latex or ts_bibtex) then
llmk.util.dbg_print('config', 'TeXShop directives found')
if ts_latex then
toml = toml .. 'latex = "' .. ts_latex .. '"\n'
end
if ts_bibtex then
toml = toml .. 'bibtex = "' .. ts_bibtex .. '"\n'
end
elseif toml == '' and shebang then
llmk.util.dbg_print('config', 'Shebang found')
toml = 'latex = "' .. shebang .. '"\n'
end
return toml, start_pos
end
llmk.parser = M
end
----------------------------------------
do -- The "runner" submodule
local M = {}
-- dependencies
local lfs = require 'lfs'
local md5 = require 'md5'
-- module local variable
local start_time = os.time()
local function table_copy(org)
local copy
if type(org) == 'table' then
copy = {}
for org_key, org_value in next, org, nil do
copy[table_copy(org_key)] = table_copy(org_value)
end
setmetatable(copy, table_copy(getmetatable(org)))
else -- number, string, boolean, etc.
copy = org
end
return copy
end
local function setup_programs(fn, config)
--[[Setup the programs table for each sequence.
Collecting tables of only related programs, which appears in the
`config.sequence` or `prog.postprocess`, and replace all specifiers.
Args:
fn (str): the input FILE name
Returns:
table of program tables
]]
local prognames = {}
local new_programs = {}
local programs = config.programs
-- collect related programs
local function add_progname(name)
-- is the program known?
if not programs[name] then
llmk.util.err_print('error', 'Unknown program "%s" is in the sequence', name)
os.exit(llmk.const.exit_error)
end
-- if not new, no addition
for _, c in pairs(prognames) do
if c == name then
return
end
end
-- if new, add it!
prognames[#prognames + 1] = name
end
for _, name in pairs(config.sequence) do
-- add the program name
add_progname(name)