-
Notifications
You must be signed in to change notification settings - Fork 6
/
pandoc-ling.lua
1573 lines (1352 loc) · 43.4 KB
/
pandoc-ling.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
--[[
pandoc-linguex: make interlinear glossing with pandoc
Version 1.6
Copyright © 2021, 2022 Michael Cysouw <cysouw@mac.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
]]
-- because of new table structure:
PANDOC_VERSION:must_be_at_least '2.10'
---------------------
-- 'global' variables
---------------------
local counter = 0 -- actual numbering of examples
local chapter = 0 -- numbering of chapters
local counterInChapter = 0 -- counter reset for each chapter
local indexEx = {} -- global lookup for example IDs
local orderInText = 0 -- order of references for resolving "Next"-style references
local indexRef = {} -- key/value: order in text = refID/exID
local rev_indexRef = {} -- "reversed" indexRef, i.e. key/value: refID/exID = order-number in text
------------------------------------
-- User Settings with default values
------------------------------------
local formatGloss = false -- format interlinear examples
local samePage = true -- keep example on one page in Latex
local xrefSuffixSep = " " -- separator to be inserted after number in example references
local restartAtChapter = false -- restart numbering at highest header without adding local chapternumbers
local addChapterNumber = false -- add chapternumbers to counting and restart at highest header
local latexPackage = "linguex"
local topDivision = "section"
local noFormat = false
local documentclass = "article"
function getUserSettings (meta)
if meta.formatGloss ~= nil then
formatGloss = meta.formatGloss
end
if meta.samePage ~= nil then
samePage = meta.samePage
end
if meta.noFormat ~= nil then
noFormat = meta.noFormat
end
if meta.xrefSuffixSep ~= nil then
xrefSuffixSep = pandoc.utils.stringify(meta.xrefSuffixSep)
end
if meta.restartAtChapter ~= nil then
restartAtChapter = meta.restartAtChapter
end
if meta.addChapterNumber ~= nil then
addChapterNumber = meta.addChapterNumber
end
if meta.latexPackage ~= nil then
latexPackage = pandoc.utils.stringify(meta.latexPackage)
end
if meta["top-level-division"] ~= nil then
topDivision = pandoc.utils.stringify(meta["top-level-division"])
end
if meta.documentclass ~= nil then
documentclass = pandoc.utils.stringify(meta.documentclass)
end
end
------------------------------------------
-- add latex dependencies: langsci-gb4e is not on CTAN!
-- restarting of counters is not working right for gb4e
------------------------------------------
function addFormatting (meta)
local tmp = pandoc.MetaList{meta['header-includes']}
if meta['header-includes'] ~= nil then
tmp = meta['header-includes']
end
if FORMAT:match "html" then
-- add specific CSS for layout of examples
-- building on classes set in this filter
-- local f = io.open("pandoc-ling.css")
-- local css = f:read("*a")
-- f:close()
local css = [[
<!-- CSS added by lua-filter 'pandoc-ling' -->
<style>
.linguistic-example {
margin: 0;
}
.linguistic-example caption {
margin-bottom: 0;
}
.linguistic-example tbody {
border-top: none;
border-bottom: none;
}
.linguistic-example-preamble {
height: 1em;
vertical-align: top;
}
.linguistic-example td {
padding-left: 0;
}
.linguistic-example-content {
vertical-align: top;
}
.linguistic-example-label {
vertical-align: top;
}
.linguistic-example-judgement {
vertical-align: top;
padding-right: 2px;
}
</style>
]]
tmp[#tmp+1] = pandoc.MetaBlocks(pandoc.RawBlock("html", css))
meta['header-includes'] = tmp
end
local function add (s)
tmp[#tmp+1] = pandoc.MetaBlocks(pandoc.RawBlock("tex", s))
end
if FORMAT:match "latex" or FORMAT:match "beamer" then
if latexPackage == "linguex" then
add("\\usepackage{linguex}")
-- no brackets
add("\\renewcommand{\\theExLBr}{}")
add("\\renewcommand{\\theExRBr}{}")
-- space for judgements
add("\\newcommand{\\jdg}[1]{\\makebox[0.4em][r]{\\normalfont#1\\ignorespaces}}")
-- chapternumbes
add("\\usepackage{chngcntr}")
if addChapterNumber then
add("\\counterwithin{ExNo}{"..topDivision.."}")
add("\\renewcommand{\\Exarabic}{\\the"..topDivision..".\\arabic}")
elseif restartAtChapter then
add("\\counterwithin*{ExNo}{"..topDivision.."}")
end
elseif latexPackage == "gb4e" then
add("\\usepackage{"..latexPackage.."}")
add("\\noautomath")
-- nnext package does not work with added top level number
-- add("\\usepackage[noparens]{nnext}")
add("\\usepackage{chngcntr}")
if addChapterNumber then
add("\\counterwithin{xnumi}{"..topDivision.."}")
add("\\counterwithin{exx}{"..topDivision.."}")
add("\\exewidth{(9.123)}")
elseif restartAtChapter then
add("\\counterwithin*{exx}{"..topDivision.."}")
end
elseif latexPackage == "langsci-gb4e" then
add("\\usepackage{"..latexPackage.."}")
-- nnext package does not work with added top level number
-- add("\\usepackage[noparens]{nnext}")
add("\\usepackage{chngcntr}")
if addChapterNumber then
add("\\counterwithin{xnumi}{"..topDivision.."}")
--add("\\counterwithin{exx}{"..topDivision.."}")
add("\\exewidth{(9.123)}")
elseif restartAtChapter then
add("\\counterwithin*{xnumi}{"..topDivision.."}")
end
elseif latexPackage == "expex" then
add("\\usepackage{expex}")
add("\\lingset{ \
belowglpreambleskip = -1.5ex, \
aboveglftskip = -1.5ex, \
exskip = 0ex, \
interpartskip = -0.5ex, \
belowpreambleskip = -2ex \
}")
if addChapterNumber then
if documentclass == "book" then
add("\\lingset{exnotype=chapter.arabic}")
end
end
if restartAtChapter then
--add("\\usepackage{epltxchapno}")
add("\\usepackage{etoolbox}")
add("\\pretocmd{\\"..topDivision.."}{\\excnt=1}{}{}")
end
end
meta['header-includes'] = tmp
end
return meta
end
-------------------------------------------
-- add temporary divs to first header level
-------------------------------------------
-- will be removed again once the chapters are counted
function addDivToHeader (head)
if head.tag == "Header"
and head.level == 1
and head.classes[1] ~= "unnumbered"
then
head.attr = {id = head.identifier, class = "restart"}
return pandoc.Div(head)
end
end
----------------------------
-- parse/rewrite example div
----------------------------
function processDiv (div)
-- keep track of chapters (header == 1)
-- included in this loop by trick "addDivToHeader"
if div.content[1].tag == "Header" and div.content[1].classes[1] == "restart" then
chapter = chapter + 1
counterInChapter = 0
div.content[1].attr = {id = div.content[1].identifier, class = ""}
-- remove div
return div.content
end
-- only do formatting for divs with class "ex"
if div.classes[1] == "ex" then
-- check format override per example
local saveGlobalformatGloss = formatGloss
if div.attributes.formatGloss ~= nil then
formatGloss = div.attributes.formatGloss
end
local saveGlobalsamePage = samePage
if div.attributes.samePage ~= nil then
samePage = div.attributes.samePage
end
local saveGlobalnoFormat = noFormat
if div.attributes.noFormat ~= nil then
noFormat = div.attributes.noFormat
end
-- parse!
local parsedDiv = parseDiv(div)
-- add temporary Cite to resolve "Next"-type references
-- will be removed after cross-references are in place
local tmpCite = pandoc.Plain(pandoc.Cite(
{pandoc.Str("@Target")},
{pandoc.Citation(parsedDiv.exID,"NormalCitation")}))
-- reformat!
local example
if FORMAT:match "latex" or FORMAT:match "beamer" then
example = texMakeExample(parsedDiv)
else
example = parsedDiv.examples
example = pandocMakeExample(parsedDiv)
example = pandoc.Div(example)
example.attr = {id = "ex"..parsedDiv.number}
end
-- return to global setting
formatGloss = saveGlobalformatGloss
samePage = saveGlobalsamePage
noFormat = saveGlobalnoFormat
return { tmpCite, example }
end
end
------------
-- parse div
------------
function parseDiv (div)
-- keep count of examples
counter = counter + 1
counterInChapter = counterInChapter + 1
-- format the numbering
local number = counter
if addChapterNumber then
number = chapter.."."..counterInChapter
elseif restartAtChapter then
number = counterInChapter
end
-- make identifier for example
local exID = ""
if div.identifier == "" then
if restartAtChapter then
-- to resolve clashes with same number used in different chapters
exID = "ex"..chapter.."."..counterInChapter
else
-- use actual number
exID = "ex"..number
end
else
-- or keep user-provided identifier
exID = div.identifier
end
-- keep global index of ids/numbers for crossreference
indexEx[exID] = number
-- extract preamble
local preamble = nil
local data = div.content[1]
if #div.content== 2 then
preamble = pandoc.Plain(div.content[1].content)
data = div.content[2]
end
-- extract judgements and content of examples
local judgements = {}
local examples = {}
local kind = {}
if noFormat then
preamble = nil
kind[1] = "single"
judgements[1] = nil
examples[1] = div
elseif data.tag == "OrderedList" or data.tag == "BulletList" then
for i=1,#data.content do
judgements[i], examples[i], kind[i] = parseExample(data.content[i][1])
end
else
judgements[1], examples[1], kind[1] = parseExample(data)
end
return { kind = kind, -- list of single/interlinear
preamble = preamble, -- preamble is Plain
judgements = judgements, -- judgements is list of Str
examples = examples, -- examples is list of (list of) Plain
number = number, -- number, exID are bare string
exID = exID
}
end
----------------------------------
-- parse various kinds of examples
----------------------------------
function parseExample (data)
local judgement, example, kind
-- either a single line example
if data.tag == "Para" or data.tag == "Plain" then
judgement, example = splitJudgement(data.content)
example = pandoc.Plain(example)
kind = "single"
-- or an interlinear example
elseif data.tag == "LineBlock" then
judgement, example = parseInterlinear(data.content)
kind = "interlinear"
end
-- judgement is Str, example is (list of) Plain
return judgement, example, kind
end
-------------------------------
function parseInterlinear (block)
local interlinear = {}
local judgement, source = splitJudgement( block[2] )
-- header
interlinear["header"] = pandoc.Plain( block[1] )
-- source
interlinear["source"] = splitSource(source)
-- gloss
interlinear["gloss"] = splitGloss( block[3] )
-- translation
interlinear["trans"] = getTrans( block[#block] )
-- judgement is Str
-- (header, trans) in interlinear is Plain
-- (source, gloss) in interlinear is list of Plain
return judgement, interlinear
end
----------------------------------------
-- helper functions to parse interlinear
----------------------------------------
function splitSource (line)
local splitSource = splitPara(line)
if formatGloss then
-- remove format and make emph throughout
for i=1,#splitSource do
local string = pandoc.utils.stringify(splitSource[i])
splitSource[i] = pandoc.Plain(pandoc.Emph(string))
end
end
-- list of Plain
return splitSource
end
-------------------------------
function splitGloss (line)
local splitGloss = splitPara(line)
if formatGloss then
-- remove format and turn capital-sequences into smallcaps
for i=1,#splitGloss do
local string = pandoc.utils.stringify(splitGloss[i])
splitGloss[i] = pandoc.Plain(formatGlossLine(string))
end
end
-- list of Plain
return splitGloss
end
-------------------------------
function getTrans (line)
if formatGloss then
-- remove quotes and add singlequote througout
if line[1].tag == "Quoted" then
line = line[1].content
end
line = pandoc.Quoted("SingleQuote", line)
end
return pandoc.Plain(line)
end
------------------------------------------
-- helper functions for (lists of) inlines
------------------------------------------
function formatGlossLine (s)
-- turn uppercase in gloss into small caps
local split = {}
for lower,upper in string.gmatch(s, "(.-)([%u%d]+)") do
if lower ~= "" then
lower = pandoc.Str(lower)
table.insert(split, lower)
end
upper = pandoc.SmallCaps(pandoc.text.lower(upper))
table.insert(split, upper)
end
for leftover in string.gmatch(s, "[%u%d]+(.-[^%u%s])$") do
leftover = pandoc.Str(leftover)
table.insert(split, leftover)
end
if #split == 0 then
if s == "~" then s = " " end -- sequence "space-nobreakspace-space"
table.insert(split, pandoc.Str(s))
end
-- result is list of inlines
return split
end
function splitPara (p)
-- remove quotes, they interfere with the splitting
if p[1].tag == "Quoted" then
p = p[1].content
end
-- push down emphasis to the individual words
if p[1].tag == "Emph" then
p = pandoc.walk_inline( p, {
Str = function(s) return Emph(s) end
} )
end
-- split paragraph in subtables at Space
-- to insert paragraph into pandoc.Table
-- Is there a better way to do this in Pandoc-Lua?
local start = 1
local result = {}
for i=1,#p do
if p[i].tag == "Space" then
-- take everythins from start up to the space
local chunk = table.move(p, start, i-1, 1, {})
table.insert(result, pandoc.Plain(chunk) )
-- move start to after space
start = i + 1
end
end
-- everything after the last space
if start <= #p then
local chunk = table.move(p, start, #p, 1, {})
table.insert(result, pandoc.Plain(chunk) )
end
-- result is list of Plain chunks
return result
end
function splitJudgement (line)
local judgement = nil
local first = pandoc.utils.stringify(line[1])
-- complex judgements, e.g. with formatting
if first == "^" then
judgement = line[2]
for i=1,3 do table.remove(line, 1) end
-- simple judgement, only a string
elseif string.sub(first, 1, 1) == "^" then
judgement = pandoc.Str(string.sub(first, 2))
for i=1,2 do table.remove(line, 1) end
end
-- judgement is Str, line is list of inlines
return judgement, line
end
--------------
-- rewrite div
--------------
function pandocMakeExample (parsedDiv)
-- sequences of 'single' will be combined into one output table
local kind = parsedDiv.kind
local onlySingle = true
for i=1,#kind do
if kind[i] ~= "single" then
onlySingle = false
end
end
-- prepare the examples for output as tables
local example = {}
if noFormat then
example[1] = pandocNoFormat(parsedDiv)
elseif #kind == 1 and kind[1] == "single" then
example[1] = pandocMakeSingle(parsedDiv)
elseif #kind == 1 and kind[1] == "interlinear" then
example[1] = pandocMakeInterlinear(parsedDiv)
elseif #kind > 1 and onlySingle then
example[1] = pandocMakeList(parsedDiv)
else
example = pandocMakeMixedList(parsedDiv)
end
-- Add example number to top left of first table
local numberParen = pandoc.Plain( "("..parsedDiv.number..")" )
example[1].bodies[1].body[1].cells[1].contents[1] = numberParen
-- set class and vertical align for noFormat
if noFormat then
example[1].bodies[1].body[1].cells[1].attr = {class = "linguistic-example-number", style = "vertical-align: middle;"}
else
example[1].bodies[1].body[1].cells[1].attr = {class = "linguistic-example-number", style = "vertical-align: top;"}
end
return example
end
function pandocNoFormat (parsedDiv)
-- make a simple 1x2 table with the whole div in the second cell
local example = turnIntoTable({{ {}, {parsedDiv.examples[1]} } } , 2, 0)
-- set class of content
example.bodies[1].body[1].cells[2].attr = {class = "linguistic-example-content"}
return example
end
function pandocMakeSingle (parsedDiv)
-- basic content
local exampleLine = parsedDiv.examples[1]
local rowContent = { {{ exampleLine }} }
-- set dimensions
local nCols = 1
local nRows = 1
local judgeCol = 1
-- add judgements
local judgement = parsedDiv.judgements[1]
if judgement ~= nil then
rowContent = addCol ( rowContent )
nCols = nCols + 1
judgeCol = judgeCol + 1
rowContent[1][1][1] = pandoc.Plain(judgement)
end
-- add preamble
local preamble = parsedDiv.preamble
if preamble ~= nil then
if judgement ~= nil then
table.insert(rowContent, 1, { {}, { preamble } } )
else
table.insert(rowContent, 1, {{ preamble }} )
end
nRows = nRows + 1
end
-- add number column
rowContent = addCol(rowContent)
nCols = nCols + 1
-- make into table
local example = turnIntoTable(rowContent, nCols, judgeCol)
-- set class of content
example.bodies[1].body[nRows].cells[nCols].attr = {class = "linguistic-example-content"}
-- set class of preamble
if preamble ~= nil then
example.bodies[1].body[1].cells[nCols].attr = {class = "linguistic-example-preamble"}
end
-- set class of judgment
if judgeCol > 1 then
example.bodies[1].body[nRows].cells[judgeCol].attr = {class = "linguistic-example-judgement"}
end
return example
end
function pandocMakeInterlinear (parsedDiv, label, forceJudge)
-- basic content
local selection = 1
if label ~= nil then
selection = label
end
local interlinear = parsedDiv.examples[selection]
local header = {{ interlinear.header }}
local headerPresent = interlinear.header.content[1] ~= nil
local source = interlinear.source
for i=1,#source do source[i] = { source[i] } end
local gloss = interlinear.gloss
for i=1,#gloss do gloss[i] = { gloss[i] } end
local trans = {{ interlinear.trans }}
local rowContent = { trans }
table.insert(rowContent, 1, gloss )
table.insert(rowContent, 1, source )
if headerPresent then
table.insert(rowContent, 1, header )
end
-- set dimensions
local nCols = #source
local nRows = #rowContent
local judgeCol = 0
-- add judgements
local judgement = parsedDiv.judgements[selection]
if judgement ~= nil or forceJudge then
rowContent = addCol ( rowContent )
nCols = nCols + 1
judgeCol = judgeCol + 2
if judgement == nil then judgement = "" end
if headerPresent then
rowContent[2][1][1] = pandoc.Plain(judgement)
else
rowContent[1][1][1] = pandoc.Plain(judgement)
end
end
-- add labels
if label ~= nil then
rowContent = addCol(rowContent)
rowContent[1][1][1] = pandoc.Plain(string.char(96+label)..".")
nCols = nCols + 1
judgeCol = judgeCol + 1
end
-- add preamble
local preamble = parsedDiv.preamble
if label ~= nil then preamble = nil end
if preamble ~= nil then
table.insert(rowContent, 1, {{ preamble }} )
nRows = nRows + 1
end
-- add number column
rowContent = addCol(rowContent)
nCols = nCols + 1
-- make into table
local example = turnIntoTable(rowContent, nCols, judgeCol)
local ps = 0 --preambleshift
local ls = 0 --labelshift
local hs = 0 --headershift
local js = 0 --judgementshift
if judgeCol > 1 then js = 1 end
-- set class of preamble and extend cell
if preamble ~= nil then
ps = 1
example.bodies[1].body[1].cells[2].attr = {class = "linguistic-example-preamble"}
example.bodies[1].body[1].cells[2].col_span = nCols - 1
end
-- set class of label
if label ~= nil then
ls = 1
example.bodies[1].body[1].cells[2+ps].attr = {class = "linguistic-example-label"}
end
-- set class of header and extend cell
if headerPresent then
hs = 1
example.bodies[1].body[1+ps].cells[2+ls+js].attr = {class = "linguistic-example-header linguistic-example-content"}
example.bodies[1].body[1+ps].cells[2+ls+js].col_span = nCols-1-ls-js
end
-- set class of translation and extend cell
example.bodies[1].body[nRows].cells[2+ls+js].attr = {class = "linguistic-example-translation linguistic-example-content"}
example.bodies[1].body[nRows].cells[2+ls+js].col_span = nCols-1-ls-js
-- set class of judgment
if judgeCol > 1 then
example.bodies[1].body[1+hs+ps].cells[2+ls].attr = {class = "linguistic-example-judgement"}
end
-- set class of source and gloss
local ssCol = 3 + ls -- sourcestart columns
local ssRow = 1 + hs + ps -- sourcestart row
for i=ssCol,nCols do
example.bodies[1].body[ssRow].cells[i].attr = {class = "linguistic-example-source linguistic-example-content"}
example.bodies[1].body[ssRow+1].cells[i].attr = {class = "linguistic-example-gloss linguistic-example-content"}
end
return example
end
function pandocMakeList (parsedDiv, from, to, forceJudge)
-- for a group of subsequent single examples
local lines = parsedDiv.examples
local judgements = parsedDiv.judgements
if from == nil then from = 1 end
if to == nil then to = #lines end
-- basic content
local rowContent = { {{ lines[from] }} }
for i=from+1,to do
table.insert(rowContent, {{ lines[i] }} )
end
-- set dimensions
local nCols = 1
local nRows = #rowContent
local judgeCol = 0
-- add judgements
for i=from,to do
if judgements[i] ~= nil or forceJudge then
rowContent = addCol ( rowContent )
nCols = nCols + 1
judgeCol = judgeCol + 2
break
end
end
for i=from,to do
if judgements[i] ~= nil then
rowContent[i][1][1] = pandoc.Plain(judgements[i])
end
end
-- add labels
local labels = {}
for i=from,to do
local label = pandoc.Str(string.char(96+i)..".")
table.insert(rowContent[i - from + 1], 1, { pandoc.Plain(label) })
end
nCols = nCols + 1
judgeCol = judgeCol + 1
-- add preamble
local preamble = parsedDiv.preamble
if from ~= 1 then preamble = nil end
if preamble ~= nil then
table.insert(rowContent, 1, {{ preamble }} )
nRows = nRows + 1
end
-- add number column
rowContent = addCol(rowContent)
nCols = nCols + 1
-- make into table
local example = turnIntoTable(rowContent, nCols, judgeCol)
-- set class of content and labels
local start = 1
if preamble ~= nil then start = 2 end
for i=start,nRows do
example.bodies[1].body[i].cells[nCols].attr = {class = "linguistic-example-content"}
example.bodies[1].body[i].cells[2].attr = {class = "linguistic-example-label"}
end
-- set class of judgment
if judgeCol > 1 then
for i=start,#example.bodies[1].body do
example.bodies[1].body[i].cells[judgeCol].attr = {class = "linguistic-example-judgement"}
end
end
-- set class of preamble and extend cell
if preamble ~= nil then
example.bodies[1].body[1].cells[2].attr = {class = "linguistic-example-preamble"}
example.bodies[1].body[1].cells[2].col_span = nCols - 1
end
return example
end
-----------------------------------------
function pandocMakeMixedList (parsedDiv)
-- mix of interlinear and (groups of) single examples
-- returns a list of tables
local result = {}
local isInterlinear = {} -- whether result at index i is an interlinear example
local resultCount = 1
local from = 1
-- harmonize judgementcolumn across tables, possibly forced
local judgements = parsedDiv.judgements
local judgeSize = 0
local forceJudge = false
for i=1,#judgements do
judgeSize = math.max(judgeSize, utf8.len(judgements[i]))
end
-- if judgeSize > 0 then
forceJudge = true
-- end
for i=1,#parsedDiv.kind do
if parsedDiv.kind[i] == "interlinear" then
local label = i
result[resultCount] = pandocMakeInterlinear(parsedDiv, label, forceJudge)
isInterlinear[resultCount] = true
resultCount = resultCount + 1
elseif parsedDiv.kind[i] == "single" then
if i==1 or parsedDiv.kind[i-1] ~= "single" then
from = i
end
if parsedDiv.kind[i+1] ~= "single" then
local to = i
result[resultCount] = pandocMakeList(parsedDiv, from, to, forceJudge)
isInterlinear[resultCount] = false
resultCount = resultCount + 1
end
end
end
-- rough approximations to align multiple tables
local spaceForNumber = string.rep(" ", 2*(string.len(parsedDiv.number)+1))
local spaceForJudge = tostring(15 + 5*judgeSize)
for i=1,#result do
-- For better alignment with example number
-- add invisibles in first column
-- not a very elegant solution, but portable across formats
result[i].bodies[1].body[1].cells[1].contents[1] =
pandoc.Plain(spaceForNumber)
-- For even better alignment, add column-width to judgement column
-- note: this is probably not portable outside html
if forceJudge then
if isInterlinear[i] then
result[i].bodies[1].body[2].cells[3].attr =
pandoc.Attr(nil, { "linguistic-judgement" }, { width = spaceForJudge.."px"} )
else
local row = 1
if i == 1 and parsedDiv.preamble ~= nil then
row = 2
end
result[i].bodies[1].body[row].cells[3].attr =
pandoc.Attr(nil, { "linguistic-judgement" }, { width = spaceForJudge.."px"} )
end
end
end
return result
end
--------------------------------------
-- helper functions to format examples
--------------------------------------
function addCol (lines)
for i=1,#lines do
table.insert(lines[i], 1, {})
end
return lines
end
function sLength (j)
if j == nil then
return 0
else
return utf8.len(pandoc.utils.stringify(j))
end
end
-------------------------------
function turnIntoTable (rowContent, nCols, judgeCol)
-- turn examples into Tables for alignment
-- use simpleTable for construction
local caption = {}
local headers = {}
local aligns = {}
for i=1,nCols do aligns[i] = "AlignLeft" end
aligns[1] = "AlignDefault"
if judgeCol > 1 then
aligns[judgeCol] = "AlignRight" -- Column for grammaticality judgements
end
local widths = {}
for i=1,nCols do widths[i] = 0 end
local rows = rowContent
local result = pandoc.SimpleTable(
caption,
aligns,
widths,
headers,
rows
)
-- turn into fancy new tables
result = pandoc.utils.from_simple_table(result)
-- set class of table to "example" for styling via CSS
result.attr = { class = "linguistic-example" }
return result
end
--------------------------
-- make markup in Latex
--------------------------
-- convenience functions for Latex
function texFront (tex, pndc)
return table.insert(pndc, 1, pandoc.RawInline("tex", tex))
end
function texEnd (tex, pndc)
return table.insert(pndc, pandoc.RawInline("tex", tex))
end
function texCombine (separated)
-- to align source/gloss they are separated
-- for Latex, they have to returned to one object
local result = pandoc.List()
for i=1,#separated do
separated[i]=separated[i].content
table.insert(separated[i], pandoc.Space())
result:extend(separated[i])
end
return(result)
end
function texSquashMulti (multi)
-- for 'noFormat' content
local result = pandoc.List()
if multi.tag == "Div" then
for i=1,#multi.content do
result:extend(multi.content[i].content)
texEnd("\\\\\n ", result)
end
else
result = multi.content
end
return result
end
-- send request to different packages
function texMakeExample (parsedDiv)
local result
if latexPackage == "expex" then
result = texMakeExpex(parsedDiv)
elseif latexPackage == "linguex" then
result = texMakeLinguex(parsedDiv)
elseif latexPackage == "gb4e" then
result = texMakeGb4e(parsedDiv)
elseif latexPackage == "langsci-gb4e" then
result = texMakeLangsci(parsedDiv)
end
return result
end
-------------------------
-- Different tex packages
-------------------------
function texMakeExpex (parsedDiv)
-- prepare parsed chunks
local kind = parsedDiv.kind
local ID = parsedDiv.exID
local preamble = parsedDiv.preamble
local judgements = parsedDiv.judgements
local line, header, source, gloss, trans
if preamble == nil then
preamble = pandoc.List()
else
preamble = preamble.content
texEnd("\\\\", preamble)