summaryrefslogtreecommitdiff
path: root/rst_context.lua
blob: c3ec32383d738bc7a89fdb237fbe07835bcaeb8e (plain)
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
#!/usr/bin/env texlua
--------------------------------------------------------------------------------
--         FILE:  rst_context.lua
--        USAGE:  ./rst_context.lua 
--  DESCRIPTION:  
--      OPTIONS:  ---
-- REQUIREMENTS:  ---
--       AUTHOR:  Philipp Gesang (Phg), <megas.kapaneus@gmail.com>
--      VERSION:  1.0
--      CREATED:  31/08/10 19:35:15 CEST
--------------------------------------------------------------------------------
--
--- TODO
-- - Find an appropriate way to handle generic tables irrespective of the grid
--   settings. The problem is:
--   http://archive.contextgarden.net/message/20100912.112605.8a1aaf13.en.html
--   Seems we'll have to choose either the grid or split tables as default. Not
--   good.


require "lpeg"
help = require "rst_helpers"

local dbg_write = help.dbg_writef

local C, Cb, Cc, Cg, Cmt, Cp, Cs, Ct, P, R, S, V, match = lpeg.C, lpeg.Cb, lpeg.Cc, lpeg.Cg, lpeg.Cmt, lpeg.Cp, lpeg.Cs, lpeg.Ct, lpeg.P, lpeg.R, lpeg.S, lpeg.V, lpeg.match

-- This one should ignore escaped spaces.
do
    local stripper = P{
        [1] = "stripper",
        stripper = V"space"^0 * C((V"space"^0 * (V"escaped" + V"nospace")^1)^0),
        space    = S(" \t\v\n"),
        nospace  = 1 - V"space",
        escaped  = P"\\" * V"space"
    }
    function string.strip(str)
        return stripper:match(str) or ""
    end 
end

local rst_context = {}


rst_context.collected_references = {}
rst_context.collected_adornments = {}
rst_context.last_section_level   = 0
rst_context.anonymous_links      = 0
rst_context.context_references   = {}

rst_context.current_footnote_number   = 0
rst_context.current_symbolnote_number = 0

function rst_context.footnote_reference (label)
    local tf = state.footnotes
    if label:match("^%d+$") then -- all digits
        local c = tonumber(label)
        print("creating footnote nr " .. c)
        return [[\\footnote{\\getbuffer[__footnote_number_]].. c .."]}"
    elseif label == "#" then --autonumber
        local rc = rst_context.current_footnote_number
        rc = rc + 1
        --rst_context.current_footnote_number = rst_context.current_footnote_number + 1
        print("creating footnote nr " .. rc)
        rst_context.current_footnote_number = rc
        return [[\\footnote{\\getbuffer[__footnote_number_]].. rc .."]}"
    elseif label:match("^#.+$") then
        local thelabel = label:match("^#(.+)$")
        print("creating labeled footnote " .. thelabel)
        return [[\\footnote{\\getbuffer[__footnote_label_]].. thelabel .."]}"
    elseif label == "*" then
        local rc = rst_context.current_symbolnote_number
        rc = rc + 1
        print("creating symbolnote nr " .. rc)
        rst_context.current_symbolnote_number = rc
        return [[\\symbolnote{\\getbuffer[__footnote_symbol_]].. rc .."]}"
    else -- “citation reference” for now treating them like footnotes
        local rc = rst_context.current_footnote_number
        rc = rc + 1
        --rst_context.current_footnote_number = rst_context.current_footnote_number + 1
        print("creating footnote nr " .. rc)
        rst_context.current_footnote_number = rc
        return [[\\footnote{\\getbuffer[__footnote_number_]].. rc .."]}"
        --return [[\\cite{]] .. label .. "}"
    end
end

function rst_context.addsetups(item)
    state.addme[item] = state.addme[item] or true
    return 0
end

-- So we can use crefs[n][2] to refer to the place where the reference was
-- created.
local function get_context_reference (str)
    local crefs = rst_context.context_references
    refstring = "__contextref__" .. tostring(#crefs + 1)
    crefs[#crefs + 1] = { refstring, str }
    return refstring
end

function rst_context.emphasis (str)
    return [[{\\em ]] .. str .. [[}]]
end

function rst_context.strong_emphasis (str)
    return [[{\\sc ]] .. str .. [[}]]
end

function rst_context.literal (str)
    str = str:gsub([[\]], [[\\]]) -- evade escaping of backslashes
    return [[\\type{]] .. str .. [[}]]
end


function rst_context.interpreted_text (...)
    local tab = { ... }
    --print (tab, #tab, tab[1], tab[2], tab[3])
    local role, str
    role = tab[1]:match("^:(.*):$") or tab[3]:match("^:(.*):$")
    str  = tab[2]

    if not role then -- implicit role
        role = "emphasis"
    end

    --print(role, str)

    return rst_context[role](str)
end

function rst_context.link_standalone (str)
    return "\n"
        .. [[\\goto{\\hyphenatedurl{]]
        .. str
        .. [[}}[url(]]
        .. str
        .. [=[)]]=]
end

function rst_context.reference (str)
    str = str:match("[^_]*")
    local link = rst_context.collected_references[str]
    if not link then -- TODO make warning instead
        return([[{\\sc UNDEFINED REFERENCE ]] .. str .. [[}.]])
    end
    return [[\\goto{]]
        .. str
        .. [[}[url(]]
        .. link
        .. [=[)]]=]
end

function rst_context.target (tab)
    --print("GOT ONE!")
    --local tab = { ... }
    local refs = rst_context.collected_references
    local target = tab[#tab] -- Ct + C could be clearer but who cares
    tab[#tab] = nil

    local function resolve_indirect (r)
        if r and r:match(".*_$") then -- pointing elsewhere
            return resolve_indirect (refs[r:match("(.*)_$")]) or "need another run!" -- TODO multiple runs && data collection
        end
        return r
    end

    local function create_anonymous ()
        rst_context.anonymous_links = rst_context.anonymous_links + 1
        return "__anon__" .. rst_context.anonymous_links
    end


    target = resolve_indirect (target)

    for i=1,#tab do
        local id = tab[i]:gsub("\\:",":") -- deescaping
        id = id ~= "" and id or create_anonymous ()
        refs[id] = refs[id] or target
    end
    return ""
end

function rst_context.escape (str)
    str = str:gsub("\\(.)", "%1")
    return str
end

function rst_context.joinindented (tab)
    return table.concat (tab, "")
end

local inline_parser = P{
    [1] = "block",

    block = Cs((V"inline_element" + 1)^1),


    inline_element = Cs((V"strong_emphasis"
                     + V"emphasis"
                     + V"inline_literal"
                     + V"interpreted_text"
--                   + V"inline_internal_target" -- TODO
                     + V"reference"
                     + V"footnote_reference"     -- TODO
--                   + V"substitution_reference" -- TODO
                     + V"link_standalone")
                    * V"succede_inline"),

    space = P" ",
    whitespace = (P" " + Cs(P"\t") / "        " + Cs(S"\v") / " "),
    spacing = V"whitespace"^1,

    eol = P"\n",
    inline_delimiters = P"‐" + P"‑" + P"‒" + P"–" + V"emdash" + V"space",        -- inline markup
    --inline_delimiter = P"**" + P"``" + S"*`",
    asterisk = P"*",
    double_asterisk = V"asterisk" * V"asterisk",
    bareia = P"`",
    backslash = P"\\",
    bar = P"|",
    double_bareia = V"bareia" * V"bareia",
    escaped_bareia = (Cs(V"backslash") / "" * V"bareia") + 1,
    colon = P":",
    semicolon = P";",
    underscore = P"_",
    double_underscore = V"underscore" * V"underscore",
    dot = P".",
    interpunct = P"·",
    comma = P",",
    dash = P"-",
    emdash = P"—",
    ellipsis   = P"…" + P"...",
    exclamationmark = P"!",
    questionmark = P"?",
    interrobang = P"‽",
    double_dash = V"dash" * V"dash",
    triple_dash = V"double_dash" * V"dash",
    hyphen = P"‐",
    dashes = V"dash" + P"‒" + P"–" + V"emdash" + P"―",
    groupchars = S"()[]{}",
    apostrophe = P"’" + P"'",

    guillemets = P"«" + P"»",
    quotationmarks= P"‘" + P"’" + P"“" + P"”",
    solidus= P"⁄",
    slash = P"/",

    gartenzaun = P"#",
    lsquare = P"[",
    rsquare = P"]",

    digit  = R"09",
    letter = R"az" + R"AZ",

    punctuation = V"apostrophe"
                + V"colon" 
                + V"comma" 
                + V"dashes"
                + V"dot" 
                + V"ellipsis"
                + V"exclamationmark"
                + V"guillemets"
                + V"hyphen"
                + V"interpunct"
                + V"interrobang"
                + V"questionmark" 
                + V"quotationmarks"
                + V"semicolon" 
                + V"slash"
                + V"solidus"
                + V"underscore"
                ,

    precede_inline = V"spacing"
                   + V"eol"
                   + S[['"([{<-/:]]
                   + P"‘" + P"“" + P"’" + P"«" + P"¡" + P"¿"
                   + V"inline_delimiters"
                   + P"„", -- not in standard Murkin reST

    succede_inline = V"spacing"
                   + S[['")]}>-/:.,;!?\]]
                   + P"’" + P"”" + P"»"
                   + V"inline_delimiters"
                   + P"“", -- non-standard again but who cares

    emphasis        = (V"asterisk" - V"double_asterisk") 
                    * Cs((1 - V"spacing" - V"eol" - V"asterisk")
                       * ((1 - (1 * V"asterisk"))^0 
                        * (1 - V"spacing" - V"eol" - V"asterisk"))^-1) 
                    * V"asterisk" 
                    / rst_context.emphasis,

    strong_emphasis = V"double_asterisk" 
                    * Cs((1 - V"spacing" - V"eol" - V"asterisk")
                       * ((1 - (1 * V"double_asterisk"))^0 
                        * (1 - V"spacing" - V"eol" - V"asterisk"))^-1) 
                    * V"double_asterisk"  
                    / rst_context.strong_emphasis,

    inline_literal  = V"double_bareia"
                    * C ((V"escaped_bareia" - V"spacing" - V"eol" - V"bareia")
                       * ((V"escaped_bareia" - (1 * V"double_bareia"))^0
                        * (V"escaped_bareia" - V"spacing" - V"eol" - V"bareia"))^-1)
                    * V"double_bareia"
                    / rst_context.literal,

    interpreted_text = C(V"role_marker"^-1)
                     * (V"bareia" - V"double_bareia")
                     * C ((1 - V"spacing" - V"eol" - V"bareia")
                        * ((1 - (1 * V"bareia"))^0
                         * (1 - V"spacing" - V"eol" - V"bareia"))^-1)
                     * V"bareia"
                     * C(V"role_marker"^-1)
                     / rst_context.interpreted_text,

    role_marker = V"colon" * (V"letter" + V"dash" + V"underscore" + V"dot")^1 * V"colon",

    link_standalone = C(V"uri")
                    / rst_context.link_standalone,

    reference = Cs(V"_reference")
              / rst_context.reference,

    _reference = (1 - V"underscore" - V"spacing" - V"eol" - V"punctuation" - V"groupchars")^1 * V"underscore",

    footnote_reference = V"lsquare" 
                       * Cs(V"footnote_label" + V"citation_reference_label") 
                       * V"rsquare" 
                       * V"underscore"
                       / rst_context.footnote_reference
                       ,

    footnote_label = V"digit"^1
                   + V"gartenzaun" * V"letter"^1
                   + V"gartenzaun"
                   + V"asterisk"
                   ,

    citation_reference_label = V"letter" * (1 - V"rsquare")^1,

--------------------------------------------------------------------------------
-- Urls
--------------------------------------------------------------------------------
    uri = V"url_protocol" * V"url_domain" * (V"slash" * V"url_path")^0,

    url_protocol = (P"http" + P"ftp" + P"shttp" + P"sftp") * P"://",
    url_domain_char = 1 - V"dot" - V"spacing" - V"eol" - V"punctuation",
    url_domain = V"url_domain_char"^1 * (V"dot" * V"url_domain_char"^1)^0,
    url_path_char = R("az", "AZ", "09") + S"-_.!~*'()",
    url_path = V"slash" * (V"url_path_char"^1 * V"slash"^-1)^1,
}

function rst_context.paragraph (data)
    local str
    if not data then
        return ""
    elseif type(data) == "table" then
        str = inline_parser:match(table.concat(data, " "))
    else
        str = data
    end
    return string.format([[

\\startparagraph
%s
\\stopparagraph
]], str)
end

local sectionlevels = {
    [1] = "chapter",
    [2] = "section",
    [3] = "subsection",
    [4] = "subsubsection",
    [5] = "subsubsubsection",
}

local function get_line_pattern (chr)
    return P(chr)^1 * (-P(1))
end

function rst_context.section (...)  -- TODO general cleanup; move validity
    local tab = { ... }             -- checking to parser.
    local section, str = true, ""
    local adornchar 
    if #tab == 3 then -- TODO use unicode length with ConTeXt
        --print(">>"..tab[1].."<>"..tab[2].."<<")
        adornchar = tab[1]:sub(1,1)
        --  overline == underline && len(overline) = len(sectionstring)
        section = tab[1] == tab[3] and #tab[1] >= #tab[2] 
        -- if overline consists only of one char then keep truth value else
        -- false
        section = get_line_pattern(adornchar):match(tab[1]) ~= nil and section
        str = string.strip(tab[2])
    else -- no overline
        --print(">>"..tab[1].."<>"..tab[2].."<<")
        adornchar = tab[2]:sub(1,1)
        section = #tab[1] <= #tab[2]
        section = get_line_pattern(adornchar):match(tab[2]) ~= nil and section
        str = tab[1]
    end

    if section then -- determine level
        local level = rst_context.last_section_level
        local rca = rst_context.collected_adornments
        if rca[adornchar] then
            level = rca[adornchar]
        else
            level = level + 1
            rca[adornchar] = level
            rst_context.last_section_level = level
        end

        ref = get_context_reference (str)

        str = string.format("\n\\\\%s[%s]{%s}\n", sectionlevels[level], ref, str)
    end

    return section and str or ""
end

-- Prime time for the fancybreak module.
function rst_context.transition (str)
    return "\n\\hrule\n"
end

function rst_context.bullet_marker(str)
    return "marker"
end

-- This one should ignore escaped spaces.
do
    local stripper = P{
        [1] = "stripper",
        stripper = V"space"^0 * C((V"space"^0 * V"nospace"^1)^0),
        space    = S(" \t\v\n"),
        escaped  = P"\\" * V"space",
        nospace  = V"escaped" + (1 - V"space"),
    }
    function string.strip(str)
        return stripper:match(str) or ""
    end 
end

local enumeration_types = {
    ["*"] = "*", -- unordered bulleted
    ["+"] = "*",
    ["-"] = "*",
    ["•"] = "*",
    ["‣"] = "*",
    ["⁃"] = "*",

    ["#"] = "1", -- numbered lists and conversion
    ["A"] = "A",
    ["a"] = "a",
    ["I"] = "R",
    ["i"] = "r",
}

-- \setupitemize[left=(, right=), margin=4em, stopper=]

local stripme   = S"()."
local dontstrip = 1 - stripme
local itemstripper = stripme^0 * C(dontstrip^1) * stripme^0
local function parse_itemstring(str)
    local setup = ",fit][itemalign=flushright,"
    -- string.match is slightly faster than string.find
    if str:match("^%(") then
        setup = setup .. [[left=(,]]
    end
    if str:match("%)$") then
        setup = setup .. [[right=)]]
    end
    if str:match("%.$") then
        setup = setup .. [[stopper=.]]
    end
    setup = setup

    str = itemstripper:match(str)
    return {setup = setup, str=str}
end

function rst_context.startitemize(str)
    local setup = ""
    str = string.strip(str)

    local listtype = enumeration_types[str] or parse_itemstring(str)

    if type(listtype) == "table" then
        --print(type(listtype), listtype[2])
        setup = listtype.setup
        listtype = listtype.str
    end

    return [[
\\startitemize[]] .. listtype .. setup .. [[]
]] 
end

function rst_context.stopitemize(str)
    return str .. [[
\\stopitemize
]]
end

function rst_context.bullet_item (str)
    return [[

\\item ]] .. inline_parser:match(str) .. [[

]]
end

--------------------------------------------------------------------------------
-- Definition lists 
--------------------------------------------------------------------------------
-- TODO define proper setups (probably bnf-like and some narrower for def-paragraphs)

function rst_context.deflist (str)
    return [[

\\startdefinitionlist
]] .. str .. [[

\\stopdefinitionlist
]]
end

function rst_context.deflist_item (str)
    return [[\\definitionitem{%]] .. str .. [[}% end definition item]]
end

function rst_context.deflist_classifier (str)
    return [[\\definitionclassifier{]] .. str .. [[}]]
end

function rst_context.deflist_term (str)
    return [[
    
  \\definitionterm{]] .. str .. [[}]]
end

function rst_context.deflist_def (str)
    return [[
    
  \\definitiondef{%]] .. str .. [[}]]
end

--------------------------------------------------------------------------------
-- Field lists
--------------------------------------------------------------------------------

-- TODO Do something useful with field lists. For now I'm not sure what as the
-- bibliography directives from the reST specification seem to make sense only
-- when using docinfo and, after all, we have .bib files that are portable.

function rst_context.field_list (str)
    return [[

\\startfieldlist]] .. str .. [[\\stopfieldlist
]]
end

function rst_context.field_name (str)
    return [[\\fieldname{]] .. str .. [[}]]
end

function rst_context.field_body (str)
    return [[\\fieldbody{]] .. str .. [[}]]
end

function rst_context.field (tab)
    local name, body = tab[1], tab[2]
    return string.format([[

  \\startfield
    \\fieldname{%s}
    \\fieldbody{%s}
  \\stopfield
]], name, inline_parser:match(body))
end

function rst_context.line_comment (str)
    return "% " .. str
end

function rst_context.block_comment (str)
    return string.format([[

\iffalse
%s
\fi
]], str)
end

function rst_context.option_list (str)
    return [[
\\setupTABLE[c][first] [background=color, backgroundcolor=grey, style=\tt]
\\setupTABLE[c][each]  [frame=off]
\\setupTABLE[r][each]  [frame=off]
\\bTABLE[split=yes,option=stretch]
\\bTABLEhead
\\bTR
  \\bTH  Option \\eTH
  \\bTH  Description \\eTH
\\eTR
\\eTABLEhead
\\bTABLEbody
]] .. inline_parser:match(str) .. [[

\\eTABLEbody
\\eTABLE
]]
end

function rst_context.option_item (tab)
    return string.format([[\\bTR\\bTC %s \\eTC\\bTC %s \\eTC\\eTR
]], tab[1], tab[2])
end

function rst_context.literal_block (str)
    local indent = P" "^1
    local stripme = indent:match(str) or 0

    local strip = P{
        [1] = "strip",
        strip = Cs(V"line"^1),
        eol = P"\n",
        restofline = (1 - V"eol")^0,
        stop = Cs(V"eol" * P" "^0) * -P(1) / "", -- remove trailing blank lines
        line = Cs(V"restofline" * (V"stop" + V"eol")) / function (line)
            return #line > stripme and line:sub(stripme) or line
        end,
    }

    str = strip:match(str)
    --strip:print() -- grammar consists of 45 rules only; wheras a plain
                    -- pattern has 60+
    return [[

\\starttyping[lines=hyphenated]
]] .. str .. [[

\\stoptyping
]]
end

function rst_context.line_block (str)
    return [[

\\startlines
]] .. inline_parser:match(str) .. [[\\stoplines
]]
end

function rst_context.line_block_line(str)
    str = str:gsub("\n", " ")
    return str .. "\n"
end

function rst_context.line_block_empty()
    return "\n"
end

function rst_context.block_quote (tab)
    local str = [[
\\setupdelimitedtext  [blockquote][style={\\setupbodyfont[11pt]}] % awful placeholder
\\definedelimitedtext[attribution][blockquote]
\\setupdelimitedtext [attribution][style={\\setupbodyfont[11pt]\\it}]

\\startlinecorrection
\\startblockquote
]] .. inline_parser:match(tab[1]) .. [[

\\stopblockquote
]]

    return tab[2] and str .. [[
\\startattribution
]] .. tab[2] .. [[
\\stopattribution
\\stoplinecorrection
]]  or str .. [[
\\stoplinecorrection
]] 
end

--function rst_context.table (str)
    --return [[
--\\startlinecorrection
--]] .. str .. [[

--\\stoplinecorrection
--]]
--end

function rst_context.grid_table (tab)
    local body = ""
    local nr = 1
    local head
    if tab.has_head then
        head = [[
\\setupTABLE[c][each]  [frame=on]
\\setupTABLE[r][each]  [frame=on]
%\\startlinecorrection
\\bTABLE[split=repeat,option=stretch]
\\bTABLEhead
]]
        while nr <= tab.head_end do
            local r = tab.rows[nr]
        --for i,r in ipairs(tab.rows) do
            local isempty = true
            for n, cell in ipairs(r) do
                if cell.variant == "normal" then
                    isempty = false
                    break
                end
            end

            if not isempty then
                local row = [[\\bTR]]
                for n,c in ipairs(r) do
                    if not (c.parent or
                            c.variant == "separator") then
                        local celltext = inline_parser:match(c.stripped)
                        if c.span.x or c.span.y then
                            local span_exp = "["
                            if c.span.x then
                                span_exp = span_exp .. "nc=" .. c.span.x .. ","
                            end
                            if c.span.y then
                                span_exp = span_exp .. "nr=" .. c.span.y
                            end
                            celltext  = span_exp .. "] " .. celltext

                        end

                        row = row .. "\n  " .. [[\\bTH ]] .. celltext .. [[\\eTH]]
                    end
                end
                head = head .. row .. "\n" .. [[\\eTR]] .. "\n"
            end
            nr = nr + 1
        end
        head = head .. [[
\\eTABLEhead
\\bTABLEbody
]] 
    else
        head = [[
\\setupTABLE[c][each]  [frame=on]
\\setupTABLE[r][each]  [frame=on]
%\\startlinecorrection
\\bTABLE[split=repeat,option=stretch]
\\bTABLEbody
]] 
    end
    while nr <= #tab.rows do
        local r = tab.rows[nr]
    --for i,r in ipairs(tab.rows) do
        local isempty = true
        for n, cell in ipairs(r) do
            if cell.variant == "normal" then
                isempty = false
                break
            end
        end

        if not isempty then
            local row = [[\\bTR]]
            for n,c in ipairs(r) do
                if not (c.parent or
                        c.variant == "separator") then
                    local celltext = inline_parser:match(c.stripped)
                    if c.span.x or c.span.y then
                        local span_exp = "["
                        if c.span.x then
                            span_exp = span_exp .. "nc=" .. c.span.x .. ","
                        end
                        if c.span.y then
                            span_exp = span_exp .. "nr=" .. c.span.y
                        end
                        celltext  = span_exp .. "] " .. celltext

                    end

                    row = row .. "\n  " .. [[\\bTC ]] .. celltext .. [[\\eTC]]
                end
            end
            body = body .. row .. "\n" .. [[\\eTR]] .. "\n"
        end
        nr = nr + 1
    end
    local tail = [[

\\eTABLEbody
\\eTABLE
%\\stoplinecorrection
]]
    return head .. body .. tail
end


function rst_context.simple_table(tab)
    local head
    local nr = 1
    if tab.head_end then
        head = [[
\\setupTABLE[c][each]  [frame=on]
\\setupTABLE[r][each]  [frame=on]
%\\startlinecorrection
\\bTABLE[split=yes,option=stretch]
\\bTABLEhead
]]
        while nr <= tab.head_end do
            local row = tab[nr]
            if not row.ignore then
                dbg_write(">hr>" .. #row)
                head = head .. [[\\bTR]]
                for nc,cell in ipairs(row) do
                    dbg_write("%7s | ", cell.content)
                    local celltext = inline_parser:match(cell.content)
                    if cell.span then
                        head = head .. string.format([=[\\bTH[nc=%s]%s\\eTH]=], cell.span.x, celltext)
                    else
                        head = head .. [[\\bTH ]] .. celltext .. [[\\eTH]]
                    end
                end
                dbg_write("\n")
                head = head .. "\\\\eTR\n"
            end
            nr = nr + 1
        end

        head = head .. [[
\\eTABLEhead
\\bTABLEbody
]] 
    else
        head = [[
\\setupTABLE[c][each]  [frame=on]
\\setupTABLE[r][each]  [frame=on]
%\\startlinecorrection
\\bTABLE[split=yes,option=stretch]
\\bTABLEbody
]] 
    end
    local tail = [[

\\eTABLEbody
\\eTABLE
%\\stoplinecorrection
]]
    local body = ""
    while nr <= #tab do
        local row = tab[nr]
        if not row.ignore then
            dbg_write(">tr>" .. #row)
            body = body .. [[\\bTR]]
            for nc,cell in ipairs(row) do
                dbg_write("%7s | ", cell.content)
                local celltext = inline_parser:match(cell.content)
                if cell.span then
                    body = body .. string.format([=[\\bTC[nc=%s]%s\\eTC]=], cell.span.x, celltext)
                else
                    body = body .. [[\\bTC ]] .. celltext .. [[\\eTC]]
                end
            end
            dbg_write("\n")
            body = body .. "\\\\eTR\n"
        end
        nr = nr + 1
    end
    return head .. body .. tail
end

function rst_context.footnote(label, content)
    local tf = state.footnotes
    rst_context.addsetups("footnotes")
    if label:match("^%d+$") then -- all digits
        tf.numbered[tonumber(label)] = rst_context.escape(inline_parser:match(content))
    elseif label == "#" then --autonumber
        repeat -- until next unrequested number
            tf.autonumber = tf.autonumber + 1
        until tf.numbered[tf.autonumber] == nil
        tf.numbered[tf.autonumber] = rst_context.escape(inline_parser:match(content))
    elseif label:match("^#.+$") then
        local thelabel = label:match("^#(.+)$")
        tf.autolabel[thelabel] = rst_context.escape(inline_parser:match(content))
    elseif label == "*" then
        rst_context.addsetups("footnote_symbol")
        tf.symbol[#tf.symbol+1] = rst_context.escape(inline_parser:match(content))
    else -- “citation reference” treated like ordinary footnote
        repeat -- until next unrequested number
            tf.autonumber = tf.autonumber + 1
        until tf.numbered[tf.autonumber] == nil
        tf.numbered[tf.autonumber] = rst_context.escape(inline_parser:match(content))
    end
    return ""
end

optional_setups = {}
function optional_setups.footnote_symbol ()
    local setup = [[
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Footnotes with symbol conversion                              %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\definenote[symbolnote][footnote]
\setupnote [symbolnote][way=bypage,numberconversion=set 2]
]]
    return setup
end

function optional_setups.footnotes ()
    local tf = state.footnotes
    local fn = [[

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Footnotes                                                     %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
]]
    local buffer = [[

%% %s
\startbuffer[%s]
%s\stopbuffer
]]
    
    for nf, note in next, tf.numbered do
        fn = fn .. string.format(buffer, "Autonumbered footnote", "__footnote_number_"..nf, note)
    end
    for nf, note in next, tf.autolabel do
        fn = fn .. string.format(buffer, "Labeled footnote", "__footnote_label_"..nf, note)
    end
    for nf, note in next, tf.symbol do
        fn = fn .. string.format(buffer, "Symbol footnote", "__footnote_symbol_"..nf, note)
    end
    return fn
end

return rst_context