-
Notifications
You must be signed in to change notification settings - Fork 1
/
collection.lua
1362 lines (1211 loc) · 49.8 KB
/
collection.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
--[[-- # Collection builder - Building collections with Pandoc
@author Julien Dutant <julien.dutant@kcl.ac.uk>
@copyright 2021 Julien Dutant
@license MIT - see LICENSE file for details.
@release 0.5
]]
-- # Filter settings
-- # Global variables
local utils = pandoc.utils
local stringify = pandoc.utils.stringify
local system = pandoc.system
local path = require('pandoc.path')
-- environement variables
local env = {
working_directory = system.get_working_directory(),
}
env.input_folder = path.directory(PANDOC_STATE['input_files'][1])
-- setup map
-- further fields that may be added:
-- - gather: strings list, metadata keys to gather from children
-- - replace: strings list, metadata keys to be replaced by children's ones
-- - pass: strings list, metadata keys to be passed onto children
-- - globalize: strings list, metadata keys to be made global onto children
local setup = {
do_something = false, -- whether the filter needs to do anything
isolate = false, -- whether to isolate sources by default
needs_isolate_filter = false, -- whether the prefix-ids filters are needed
offprint_mode = false, -- whether we're in offprint mode
}
VERBOSITY_LEVELS = {INFO = 0, WARNING = 1, ERROR = 2}
-- # Helper functions
--- message: send message to std_error
-- @param type string INFO, WARNING, ERROR
-- @param text string message text
function message(type, text)
type = VERBOSITY_LEVELS[type] and type or 'ERROR'
if VERBOSITY_LEVELS[PANDOC_STATE.verbosity] <= VERBOSITY_LEVELS[type] then
io.stderr:write('[' .. type .. '] Collection lua filter: '
.. text .. '\n')
end
end
--- type: pandoc-friendly type function
-- pandoc.utils.type is only defined in Pandoc >= 2.17
-- if it isn't, we extend Lua's type function to give the same values
-- as pandoc.utils.type on Meta objects: Inlines, Inline, Blocks, Block,
-- string and booleans
-- Caution: not to be used on non-Meta Pandoc elements, the
-- results will differ (only 'Block', 'Blocks', 'Inline', 'Inlines' in
-- >=2.17, the .t string in <2.17).
local type = pandoc.utils.type or function (obj)
local tag = type(obj) == 'table' and obj.t and obj.t:gsub('^Meta', '')
return tag and tag ~= 'Map' and tag or type(obj)
end
--- lazily compiled default templates for pandoc.write
compiled_templates = {}
function get_template(format)
compiled_templates[format] = compiled_templates[format]
or pandoc.template.compile(pandoc.template.default(format))
return compiled_templates[format]
end
--- to_json: converts a entire Pandoc document to json
-- @param doc pandoc Pandoc object to be converted to json
-- @return string json string representation if success, nil if failed
-- @TODO in Win, use Python or Perl if present, powershell is slow
function to_json(doc)
-- in Pandoc >= 2.17, we can simply use pandoc.write
if PANDOC_VERSION >= '2.17' then
if pandoc.utils.type(doc) == 'Pandoc' then
return pandoc.write(doc, 'json')
else
return nil
end
end
-- in Pandoc <= 2.17, first confirm that doc is Pandoc object
if not (doc.meta and doc.blocks) then
return nil
end
-- pandoc.utils.run_json_filter(doc, command) converts the Pandoc
-- doc to its JSON representation, sends it to stdin, executes
-- `command` expects a JSON representation of a Pandoc document
-- return. Our `command` simply wraps the json string Pandoc
-- stands to stdin in (a JSON representation) of a Pandoc document
-- with a Rawblock containing that string.
-- we use `sed` on MacOs/Linux systems and Powershell on Win.
local command = ''
local arguments = pandoc.List:new()
-- strings to build an json representation of an empty document
-- with a RawBlock element
local api_ver_str = tostring(PANDOC_API_VERSION):gsub('%.',',')
local before = '{"pandoc-api-version":[' .. api_ver_str .. '],'
.. [["meta":{},"blocks":[{"t":"RawBlock","c":["json","]]
local after = [["]}]}]]
local result = nil
if pandoc.system.os == 'mingw32' then
-- we need to set input and output in utf8
-- before run_json_filter is called
-- [Console]::OutputEncoding for stdin
-- $OutputEncoding for stdout
-- see https://stackoverflow.com/questions/49476326/displaying-unicode-in-powershell
-- @TODO find a way to restore later! we can't use variables
-- as they are dumped at the end of this call
os.execute([[PowerShell -NoProfile -Command ]]
.. ' [Console]::OutputEncoding=[Text.Encoding]::utf8;'
.. ' $OutputEncoding=[Text.Encoding]::utf8;'
)
command = 'powershell'
arguments:extend({'-NoProfile', '-Command'})
-- write the powershell script
-- (for some reason it isn't necessary to wrap it in double quotes)
local pwsh_script = ''
-- manipulate stdin
pwsh_script = pwsh_script .. '$input'
-- escape backslashes and double quotes
pwsh_script = pwsh_script .. [[ -replace '\\','\\']]
.. [[ -replace '\"','\"']]
-- wrap the result in an empty document with a RawBlock element
pwsh_script = pwsh_script .. " -replace '^','" .. before .. "'"
.. " -replace '$','" .. after .. "'"
arguments:insert(pwsh_script)
result = pandoc.utils.run_json_filter(doc, command, arguments)
-- restore console settings here
else
command = 'sed'
local sed_script = ''
-- escape backlashes and double quotes
sed_script = sed_script .. [[s/\\/\\\\/g; ]] .. [[s/\"/\\"/g; ]]
-- wrap the result in an empty document with a RawBlock element
sed_script = sed_script .. [[s/^/]] .. before .. [[/; ]]
.. [[s/$/]] .. after .. [[/; ]]
arguments:insert(sed_script)
result = pandoc.utils.run_json_filter(doc, command, arguments)
end
-- catch the result in the `text` field of the first block
-- return nil if failed
return result.blocks[1].text or nil
end
--- meta_to_yaml: converts MetaMap to yaml
---@return result string | nil
function meta_to_yaml (map)
if not map then return nil end
local doc = pandoc.Pandoc({}, pandoc.Meta(map))
local function extract_yaml (str)
if str then
return str:match("^%-%-%-\n(.*\n)%-%-%-\n") or ''
end
end
-- in Pandoc >= 2.17, we can simply use pandoc.write
if PANDOC_VERSION >= '2.17' then
return extract_yaml(
pandoc.write(doc, 'markdown',
{ template = get_template('markdown')}
)
)
else
local json = to_json(doc)
if json then
return extract_yaml(
pandoc.pipe('pandoc', {'-f', 'json', '-s', '-t', 'markdown'}, json)
)
end
end
end
--- save_meta_as_yaml: save Meta or Metamap as a yaml file
-- uses pandoc to convert Meta or Metamap to yaml.
-- a converter would be faster, but would need to parse Meta elements.
function save_meta_as_yaml(map,filepath)
yaml = meta_to_yaml(map) or ''
-- save file, even if empty
local file = io.open(filepath, 'w')
file:write(yaml)
file:close()
end
-- # Internal filters
--- prefix_crossref_ids_filter, prefix_ids_filter: filters to prefix
-- sources's ids and links to avoid conflicts between sources.
-- These two filters will be ran on imported sources if we need to isolate
-- their internal crossreferences from other sources
-- The first filter, applied before the user's, handles Pandoc-crossref
-- crossreferences. The second, applied after, handles remaining
-- crossreferences.
prefix_crossref_ids_filter = [[
-- # Global variables
---@type string user's custom prefix
local prefix = ''
---@type pandoc.List identifers removed
local old_identifiers = pandoc.List:new()
---@type pandoc.List identifers added
local new_identifiers = pandoc.List:new()
---@type pandoc.List identifiers to ignore
local ids_to_ignore = pandoc.List:new()
---@type boolean whether to process pandoc-crossref links
local pandoc_crossref = true
local crossref_prefixes = pandoc.List:new({'fig','sec','eq','tbl','lst',
'Fig','Sec','Eq','Tbl','Lst'})
local crossref_str_prefixes = pandoc.List:new({'eq','tbl','lst',
'Eq','Tbl','Lst'}) -- found in Str elements (captions or after eq)
local codeblock_captions = true -- is the codeblock caption syntax on?
--- type: pandoc-friendly type function
-- pandoc.utils.type is only defined in Pandoc >= 2.17
-- if it isn't, we extend Lua's type function to give the same values
-- as pandoc.utils.type on Meta objects: Inlines, Inline, Blocks, Block,
-- string and booleans
-- Caution: not to be used on non-Meta Pandoc elements, the
-- results will differ (only 'Block', 'Blocks', 'Inline', 'Inlines' in
-- >=2.17, the .t string in <2.17).
local type = pandoc.utils.type or function (obj)
local tag = type(obj) == 'table' and obj.t and obj.t:gsub('^Meta', '')
return tag and tag ~= 'Map' and tag or type(obj)
end
--- get_options: get filter options for document's metadata
-- @param meta pandoc Meta element
function get_options(meta)
-- syntactic sugar: options aliases
-- merging behaviour: aliases prevail
local aliases = {'prefix', 'pandoc-crossref'}
for _,alias in ipairs(aliases) do
if meta['prefix-ids-' .. alias] ~= nil then
-- create a 'prefix-ids' key if needed
if not meta['prefix-ids'] then
meta['prefix-ids'] = pandoc.MetaMap({})
end
meta['prefix-ids'][alias] = meta['prefix-ids-' .. alias]
meta['prefix-ids-' .. alias] = nil
end
end
-- save options in global variables
if meta['prefix-ids'] then
if meta['prefix-ids']['prefix'] then
prefix = pandoc.utils.stringify(meta['prefix-ids']['prefix'])
end
if meta['prefix-ids']['pandoc-crossref'] ~= nil
and meta['prefix-ids']['pandoc-crossref'] == false then
pandoc_crossref = false
end
end
-- if meta.codeBlockCaptions is false then we should *not*
-- process `lst:label` identifiers that appear in Str elements
-- (that is, in codeblock captions). We will still convert
-- those that appear as CodeBlock attributes
if not meta.codeBlockCaptions then
codeblock_captions = false
crossref_str_prefixes = crossref_str_prefixes:filter(
function(item) return item ~= 'lst' end)
end
return meta
end
--- process_doc: process the pandoc document
-- generates a prefix if needed, walk through the document
-- and adds a prefix to all elements with identifier.
-- @param pandoc Pandoc element
-- @TODO handle meta fields that may contain identifiers? abstract
-- and thanks?
function process_doc(doc)
-- generate prefix if needed
if prefix == '' then
prefix = pandoc.utils.sha1(pandoc.utils.stringify(doc.blocks))
end
-- add_prefix function
-- check that it's a pandoc-crossref type
-- do not add prefixes to empty identifiers
-- store the old identifiers to later fix links
add_prefix = function (el)
if el.identifier and el.identifier ~= '' then
-- if pandoc-crossref type, we add the prefix after "fig:", "tbl", ...
-- though (like pandoc-crossref) we must ignore #lst:label unless there's
-- a caption attribute or the codeblock caption syntax is on
if pandoc_crossref then
local type, identifier = el.identifier:match('^(%a+):(.*)')
if type and identifier and crossref_prefixes:find(type) then
-- special case in which we don't touch it:
-- a codeblock with #lst:label id but no caption
-- nor caption table syntax on
if el.t == 'CodeBlock' and not codeblock_captions
and type == 'lst' and (not el.attributes
or not el.attributes.caption) then
return
-- in all other cases we add prefix between `type`
-- and `identifier`
-- NOTE: in principle we should check that if it's
-- a codeblock it has a caption paragraph before or
-- after, but that requires going through the doc
-- el by el, not worth it.
else
old_identifiers:insert(type .. ':' .. identifier)
new_id = type .. ':' .. prefix .. identifier
el.identifier = new_id
new_identifiers:insert(new_id)
return el
end
end
end
-- if no pandoc_crossref action was taken, apply simple prefix
-- Warning: if `autoSectionLabels` is true, pandoc-crossref
-- will add `sec:` to Header element ids; so we anticipate that
old_identifiers:insert(el.identifier)
new_id = prefix .. el.identifier
el.identifier = new_id
if el.t == 'Header'
and doc.meta.autoSectionLabels ~= false then
new_identifiers:insert('sec:' .. new_id)
else
new_identifiers:insert(new_id)
end
return el
end
end
-- add_prefix_string function
-- handles {#eq:label} for equations and {#tbl:label} or {#lst:label}
-- in table or listing captions.
add_prefix_string = function(el)
local type, identifier = el.text:match('^{#(%a+):(.*)}')
if type and identifier and crossref_str_prefixes:find(type) then
old_identifiers:insert(type .. ':' .. identifier)
local new_id = type .. ':' .. prefix .. identifier
new_identifiers:insert(new_id)
return pandoc.Str('{#' .. new_id .. '}')
end
end
-- process_identifiers function
-- apply the add_prefix and add_prefix_strings functions to
-- elements with pandoc-crossref identifiers
process_identifiers = function(blocks)
local div = pandoc.walk_block(pandoc.Div(blocks), {
Image = add_prefix,
Header = add_prefix,
Table = add_prefix,
CodeBlock = add_prefix,
Str = add_prefix_string,
})
return div.content
end
-- prefix identifiers in doc and in metadata fields with blocks content
for key,val in pairs(doc.meta) do
if type(val) == 'Blocks' then
doc.meta[key] = pandoc.MetaBlocks(
process_identifiers(pandoc.List(val))
)
elseif type(val) == 'List' then
for i = 1, #val do
if type(val[i]) == 'Blocks' then
doc.meta[key][i] = pandoc.MetaBlocks(
process_identifiers(pandoc.List(val[i]))
)
end
end
end
end
doc.blocks = process_identifiers(doc.blocks)
-- function to add prefixes to links
local add_prefix_to_link = function (link)
if link.target:sub(1,1) == '#'
and old_identifiers:find(link.target:sub(2,-1)) then
local target = link.target:sub(2,-1)
local type = target:match('^(%a+):')
if crossref_prefixes:find(type) then
link.target = '#' .. type .. ':' .. prefix
.. target:match('^%a+:(.*)')
return link
end
end
end
-- function to add prefixes to pandoc-crossref citations
-- looking for keys starting with `fig:`, `sec:`, `eq:`, ...
local add_prefix_to_crossref_cites = function (cite)
for i = 1, #cite.citations do
local type, identifier = cite.citations[i].id:match('^(%a+):(.*)')
if type and identifier and crossref_prefixes:find(type) then
-- put the type in lowercase to match Fig: and fig:
-- note that sec: cites might refer to an old identifier
-- that doesn't start with sec:
local stype = pandoc.text.lower(type)
if old_identifiers:find(stype..':'..identifier) or
(stype == 'sec' and old_identifiers:find(identifier))
then
cite.citations[i].id = type..':'..prefix..identifier
end
end
end
return cite
end
-- function to process links and cites in some blocks
process_links = function(blocks)
local div = pandoc.walk_block(pandoc.Div(blocks), {
Link = add_prefix_to_link,
Cite = pandoc_crossref and add_prefix_to_crossref_cites
})
return div.content
end
-- process links and cites in doc and in metablocks fields
for key,val in pairs(doc.meta) do
if type(val) == 'Blocks' then
doc.meta[key] = pandoc.MetaBlocks(
process_links(pandoc.List(val))
)
elseif type(val) == 'List' then
for i = 1, #val do
if type(val[i]) == 'Blocks' then
doc.meta[key][i] = pandoc.MetaBlocks(
process_links(pandoc.List(val[i]))
)
end
end
end
end
doc.blocks = process_links(doc.blocks)
-- set metadata (in case prefix-ids is ran later on)
-- save a list of ids changed
if not doc.meta['prefix-ids'] then
doc.meta['prefix-ids'] = pandoc.MetaMap({})
end
doc.meta['prefix-ids'].ignoreids = pandoc.MetaList(new_identifiers)
-- return the result
return doc
end
-- # Main filter
return {
{
Meta = get_options,
Pandoc = function(doc)
if pandoc_crossref then return process_doc(doc) end
end,
}
}
]]
prefix_ids_filter = [[
---# Global variables
---@alias crossref.Prefixes 'fig'|'sec'|'eq'|'tbl'|'lst'
---@alias citeproc.Prefixes 'ref'
---@type string user's custom prefix
local prefix = ''
---@type pandoc.List identifers removed
local old_identifiers = pandoc.List:new()
---@type pandoc.List identifiers to ignore
local ids_to_ignore = pandoc.List:new()
---@type boolean whether to process pandoc-crossref links
local pandoc_crossref = true
---@type pandoc.List list of identifier prefixes for crossref
local crossref_prefixes = pandoc.List:new({'fig','sec','eq','tbl','lst'})
---@type pandoc.List list of identifier prefixes for citeproc
local citeproc_prefixes = pandoc.List:new({'ref'})
---@type pandoc.List list of identifier prefixes appearing in Str elements
local crossref_str_prefixes = pandoc.List:new({'eq','tbl','lst'})
---@type boolean whether pandoc-cross ref codeBlockCaptions option is on
local codeblock_captions = true
---type: pandoc-friendly type function
---pandoc.utils.type is only defined in Pandoc >= 2.17
---if it isn't, we extend Lua's type function to give the same values
---as pandoc.utils.type on Meta objects: Inlines, Inline, Blocks, Block,
---string and booleans
---Caution: not to be used on non-Meta Pandoc elements, the
---results will differ (only 'Block', 'Blocks', 'Inline', 'Inlines' in
--->=2.17, the .t string in <2.17).
local type = pandoc.utils.type or function (obj)
local tag = type(obj) == 'table' and obj.t and obj.t:gsub('^Meta', '')
return tag and tag ~= 'Map' and tag or type(obj)
end
---get_options: get filter options for document's metadata
---@param meta pandoc.Meta
---@return pandoc.Meta
function get_options(meta)
-- syntactic sugar: options aliases
-- merging behaviour: aliases prevail
local aliases = {'prefix', 'pandoc-crossref'}
for _,alias in ipairs(aliases) do
if meta['prefix-ids-' .. alias] ~= nil then
-- create a 'prefix-ids' key if needed
if not meta['prefix-ids'] then
meta['prefix-ids'] = pandoc.MetaMap({})
end
meta['prefix-ids'][alias] = meta['prefix-ids-' .. alias]
meta['prefix-ids-' .. alias] = nil
end
end
-- save options in global variables
if meta['prefix-ids'] then
if meta['prefix-ids']['prefix'] then
prefix = pandoc.utils.stringify(meta['prefix-ids']['prefix'])
end
if meta['prefix-ids']['pandoc-crossref'] ~= nil
and meta['prefix-ids']['pandoc-crossref'] == false then
pandoc_crossref = false
end
if meta['prefix-ids'].ignoreids and
type(meta['prefix-ids'].ignoreids) == 'List' then
ids_to_ignore:extend(meta['prefix-ids'].ignoreids)
end
end
-- pandoc-crossref option: meta.codeBlockCaptions
-- if meta.codeBlockCaptions is false then we should *not*
-- process `lst:label` identifiers that appear in Str elements
-- (that is, in codeblock captions). We will still convert
-- those that appear as CodeBlock attributes
if not meta.codeBlockCaptions then
codeblock_captions = false
crossref_str_prefixes = crossref_str_prefixes:filter(
function(item) return item ~= 'lst' end)
end
return meta
end
---process_doc: process the pandoc document.
---generates a prefix if needed, walks through the document
---and adds a prefix to all elements with identifier.
---@TODO meta fields may contain identifiers? abstract, thanks
---@param doc pandoc.Doc
---@return pandoc.Doc
function process_doc(doc)
-- add_prefix function
-- adds prefix to an identifier
-- do not add prefixes to empty identifiers
-- store the old identifiers in order to fix links
add_prefix = function (el)
if el.identifier and el.identifier ~= ''
and not ids_to_ignore:find(el.identifier) then
-- if citeproc type, add the prefix after "ref-"
local type, identifier = el.identifier:match('^(%a+)%-(.*)')
if type and identifier and citeproc_prefixes:find(type) then
old_identifiers:insert(el.identifier)
el.identifier = type..'-'..prefix..identifier
return el
end
-- if pandoc-crossref type, we add the prefix after "fig:", "tbl:", ...
-- though (like pandoc-crossref) we must ignore #lst:label unless there's
-- a caption attribute or the codeblock caption syntax is on
if pandoc_crossref then
local type, identifier = el.identifier:match('^(%a+):(.*)')
if type and identifier and crossref_prefixes:find(type) then
-- special case in which we don't touch it:
-- a codeblock with #lst:label id but no caption
-- nor caption table syntax on
if el.t == 'CodeBlock' and not codeblock_captions
and type == 'lst' and (not el.attributes
or not el.attributes.caption) then
return
-- in all other cases we add prefix between `type`
-- and `identifier`
-- NOTE: in principle we should check that if it's
-- a codeblock it has a caption paragraph before or
-- after, but that requires going through the doc
-- el by el, not worth it.
else
old_identifiers:insert(el.identifier)
el.identifier = type..':'..prefix..identifier
return el
end
end
end
-- if no pandoc_crossref action was taken, apply simple prefix
old_identifiers:insert(el.identifier)
el.identifier = prefix .. el.identifier
return el
end
end
-- add_prefix_string function
-- same as add_prefix but for pandoc-crossref "{eq:label}" strings
-- crossref_srt_prefixes tell us which ones to convert
add_prefix_string = function(el)
local type, identifier = el.text:match('^{#(%a+):(.*)}')
if type and identifier
and crossref_str_prefixes:find(type)
and not ids_to_ignore:find(type .. ':' .. identifier) then
old_identifiers:insert(type .. ':' .. identifier)
local new_id = type .. ':' .. prefix .. identifier
return pandoc.Str('{#' .. new_id .. '}')
end
end
-- process_identifiers function
-- apply the add_prefix and add_prefix_strings functions to blocks
process_identifiers = function(blocks)
local div = pandoc.walk_block(pandoc.Div(blocks), {
Span = add_prefix,
Link = add_prefix,
Image = add_prefix,
Code = add_prefix,
Div = add_prefix,
Header = add_prefix,
Table = add_prefix,
CodeBlock = add_prefix,
Str = pandoc_crossref and add_prefix_string
})
return div.content
end
-- function to add prefixes to links
local add_prefix_to_link = function (link)
if link.target:sub(1,1) == '#'
and old_identifiers:find(link.target:sub(2,-1)) then
local target = link.target:sub(2,-1)
-- handle citeproc targets
local type, identifier = target:match('^(%a+)%-(.*)')
if type and identifier and citeproc_prefixes:find(type) then
target = '#'..type..'-'..prefix..identifier
-- handle pandoc-crossref types targets if needed
elseif pandoc_crossref then
local type, identifier = target:match('^(%a+):(.*)')
if type and crossref_prefixes:find(type) then
target = '#' .. type .. ':' .. prefix .. identifier
else
target = '#' .. prefix .. target
end
else
target = '#' .. prefix .. target
end
link.target = target
return link
end
end
-- function to add prefixes to pandoc-crossref citations
-- looking for keys starting with `fig:`, `sec:`, `eq:`, ...
add_prefix_to_crossref_cites = function (cite)
for i = 1, #cite.citations do
local type, identifier = cite.citations[i].id:match('^(%a+):(.*)')
if type and identifier and crossref_prefixes:find(type) then
-- put the type in lowercase to match Fig: and fig:
-- note that sec: cites might refer to an old identifier
-- that doesn't start with sec:
local stype = pandoc.text.lower(type)
if old_identifiers:find(stype..':'..identifier) or
(stype == 'sec' and old_identifiers:find(identifier))
then
cite.citations[i].id = type..':'..prefix..identifier
end
end
end
return cite
end
-- function to process links and cites in some blocks
local process_links = function(blocks)
local div = pandoc.walk_block(pandoc.Div(blocks), {
Link = add_prefix_to_link,
Cite = pandoc_crossref and add_prefix_to_crossref_cites
})
return div.content
end
-- MAIN BODY
-- generate prefix if needed
if prefix == '' then
prefix = pandoc.utils.sha1(pandoc.utils.stringify(doc.blocks))
end
-- prefix identifiers in doc and in metadata fields with blocks content
for key,val in pairs(doc.meta) do
if type(val) == 'Blocks' then
doc.meta[key] = pandoc.MetaBlocks(
process_identifiers(pandoc.List(val))
)
elseif type(val) == 'List' then
for i = 1, #val do
if type(val[i]) == 'Blocks' then
doc.meta[key][i] = pandoc.MetaBlocks(
process_identifiers(pandoc.List(val[i]))
)
end
end
end
end
doc.blocks = process_identifiers(doc.blocks)
-- process links and cites in metadata fields and in body
for key,val in pairs(doc.meta) do
if type(val) == 'Blocks' then
doc.meta[key] = pandoc.MetaBlocks(
process_links(pandoc.List(val))
)
elseif type(val) == 'List' then
for i = 1, #val do
if type(val[i]) == 'Blocks' then
doc.meta[key][i] = pandoc.MetaBlocks(
process_links(pandoc.List(val[i]))
)
end
end
end
end
doc.blocks = process_links(doc.blocks)
-- return the result
return doc
end
-- # Main filter
return {
{
Meta = get_options,
Pandoc = process_doc
}
}
]]
-- # Collection functions
--- gather_and_replace: gather or replace selected keys in the main
-- document based on source files metadata.
-- Keys to be gather or replaced are listed in `setup.gather` and
-- `setup.replace`.
-- Gather behaviour: if a key preexists we make a list with the old and
-- new values; it both are lists we merge them. Useful for bibliographies,
-- header-includes.
-- Replace behaviour: if a key preexists we replace it with the source's
-- values; if present in different sources, the last one prevails. Useful
-- in offprint mode (single source) or for keys that don't overlap across
-- sources.
-- If a key is set to be gathered and replaced, it will be replaced.
function gather_and_replace(meta)
for _,item in ipairs(meta.imports) do
-- we know `item `is a MetaMap, but not if it has a `file` key
-- if it doesn't we skip it
if not item.file then
goto continue
end
-- read and parse the file
local filepath = stringify(item.file)
local file = io.open(filepath, 'r')
local itemdoc = pandoc.read(file:read('a'))
file:close()
-- for each key to gather, we check if it exists
-- and we import it in our document's metadata
-- merging behaviour:
-- if the preexisting is a list, we append
-- if the preexisting key has a value, we turn into
-- a list and we append
if setup.gather then
for _,key in ipairs(setup.gather) do
if itemdoc.meta[key] then
if not meta[key] then
meta[key] = itemdoc.meta[key]:clone()
else
if type(meta[key]) ~= 'List' then
meta[key] = pandoc.MetaList(meta[key])
end
if type(itemdoc.meta[key]) == 'List' then
meta[key]:extend(itemdoc.meta[key])
else
meta[key]:insert(itemdoc.meta[key])
end
end
end
end
end
-- for each key to replace, we import it in the document's meta
if setup.replace then
for _,key in ipairs(setup.replace) do
if itemdoc.meta[key] then
meta[key] = itemdoc.meta[key]
end
end
end
:: continue ::
end
return meta
end
--- globalize_and_pass: globalize selected metadata fields into
-- the `global-metadata` map and place selected metadata fields into
-- the `child-metadata` map.
-- merging behaviour: warn and erase preexisting keys in `global/child
-- -metadata`.
-- creates `global/child-metadata` keys only if needed
-- @param meta
-- @param setup.globalize global variable
-- @return metadata document
function globalize_and_pass(meta)
-- mapping `setup` keys with `meta` keys
local map = {
globalize = 'global-metadata',
pass = 'child-metadata'
}
for setupkey, metakey in pairs(map) do
-- are there `setup.globalize` or `setup.pass` keys?
if setup[setupkey] then
for _,key in ipairs(setup[setupkey]) do
-- is there a `key` field in metadata to copy?
if meta[key] then
-- copy in the `metakey` field, warn if merging
if not meta[metakey] then
meta[metakey] = pandoc.MetaMap({})
meta[metakey][key] = meta[key]
elseif not meta[metakey][key] then
meta[metakey][key] = meta[key]
else
-- "pass/globalize `key` replaced `key` in `metakey`"
message('WARNING', 'Metadata: ' .. setupkey ..
' `' .. key .. '` replaced `' .. key ..
'` in `' .. metakey .. '`.')
meta[metakey][key] = meta[key]
end
end
end
end
end
return meta
end
--- import_sources: import sources into the main document
-- passes metadata to sources using a temporary metadata file,
-- runs pandoc on them in the required mode and places the
-- result in the main document
function import_sources(doc, tmpdir)
-- CONSTANTS
local acceptable_modes = pandoc.List:new({'native', 'raw', 'direct'})
-- save_yaml_if_needed: if element isn't a map, assume it's
-- a filepath to metadata file; otherwise save a temp
-- yaml file `default_filename`. Either way, returns a filepath.
function save_yaml_if_needed(element, default_filename)
if type(element) ~= 'table' and type(element) ~= 'Meta' then
return stringify(element)
else
local filepath = path.join({tmpdir, default_filename})
save_meta_as_yaml(element,filepath)
return filepath
end
end
-- GENERIC import features: metadata, defaults, mode, isolation
local generic_meta_fpath = ''
local generic_defaults_fpath = ''
local generic_mode = 'native'
-- Do we need to pass generic metadata?
-- If yes, prepare a temp file
if doc.meta['global-metadata'] or doc.meta['child-metadata'] then
generic_meta_fpath = path.join( { tmpdir , 'generic_meta.yaml' })
-- `child-metadata` goes to the root, `global-metadata` is
-- inserted as is
local metamap = pandoc.MetaMap(doc.meta['child-metadata'] or {})
metamap['global-metadata'] = doc.meta['global-metadata'] or nil
save_meta_as_yaml(metamap, generic_meta_fpath)
end
-- Do we need to pass generic defaults?
-- If yes, are we given a file or a map to save as a temp file?
-- (yes if `collection` has a `defaults` key that is a map)
if doc.meta.collection and doc.meta.collection.defaults then
generic_defaults_fpath = save_yaml_if_needed(
doc.meta.collection.defaults, 'generic_defaults.yaml')
end
-- DEBUG: display a temp yaml files
-- local file = io.open(generic_meta_fpath, 'r')
-- print('yaml file ', generic_meta_fpath, ':')
-- print(file:read('a'))
-- file:close()
-- set a generic import mode
if doc.meta.collection and doc.meta.collection['mode'] then
str = stringify(doc.meta.collection['mode'])
if acceptable_modes:find(str) then
generic_mode = str
end
end
-- will we need our internal filters?
-- if yes, save them as tmp files
-- for the isolate filter, get any user-specified custom prefix pattern
local isolate_prefix_pattern = "c%d-"
local prefix_ids_filter_fpath = ''
local prefix_crossref_ids_filter_fpath = ''
if setup.needs_isolate_filter then
if doc.meta.collection['isolate-prefix-pattern'] then
isolate_prefix_pattern = stringify(doc.meta.collection['isolate-prefix-pattern'])
end
prefix_ids_filter_fpath = path.join({tmpdir, 'prefix-ids.lua'})
prefix_crossref_ids_filter_fpath = path.join({tmpdir,
'prefix-crossref-ids.lua'})
local file = io.open(prefix_ids_filter_fpath, 'w')
file:write(prefix_ids_filter)
file:close()
local file = io.open(prefix_crossref_ids_filter_fpath, 'w')
file:write(prefix_crossref_ids_filter)
file:close()
end
-- MAIN LOOP to import each item in the list
-- `i` will be used as unique identifier if needed
for i = 1, #doc.meta.imports do
item = doc.meta.imports[i]
-- we can rely on item being a MetaMap
-- but if it doesn't have a `file` field nothing to do,
-- move on to the next one
if not item.file then
goto continue
end
-- LOCAL import features: source, metadata, defaults, mode, merge
local source = stringify(item.file)
local local_meta_fpath = ''
local local_defaults_fpath = ''
local mode = generic_mode
local merge_defaults = false
local merge_meta = false
local isolate = false
-- do we need local metadata?
if item['child-metadata'] then
local_meta_fpath = save_yaml_if_needed(
item['child-metadata'], 'local_meta.yaml' )
end
-- DEBUG: display local yaml file
-- if local_meta_fpath ~= '' then
-- print('IMPORT #', i, local_meta_fpath)
-- local file = io.open(local_meta_fpath, 'r')
-- print(file:read('a'))
-- file:close()
-- end
-- do we need local defaults?
if item['defaults'] then
local_defaults_fpath = save_yaml_if_needed(
item['defaults'], 'local_defaults.yaml' )
end
-- do we have a local mode?
if item.mode then
local str = stringify(item.mode)
if acceptable_modes:find(str) then
mode = str
end
end