Improved and simplified code to support labels on notes in ConTeXt

The former redefinition of \footnote was less elegant and caused errors when a \footnote without a final label was the last element in a group.'
This commit is contained in:
Bastien Dumont 2023-03-15 00:00:00 +01:00
parent 8772547d05
commit df1ae4566b

View File

@ -21,28 +21,13 @@ local RAW_ATTRIBUTE
and redefine the macro so that it takes it into account.
]]--
local function tweak_footnote_if_ConTeXt(metadata)
local function support_footnote_label_ConTeXt(metadata)
if RAW_ATTRIBUTE == 'context' then
local footnote_redefinition = [[
\catcode`\@=11
\let\origfootnote\footnote
\def\footnote#1#2{
\def\tcrf@secondArg{#2}%
\def\tcrf@bracket{[}%
\ifx\tcrf@secondArg\tcrf@bracket
\def\tcrf@todo{\tcrf@footnote@withlabel{#1}#2} %
\else
\def\tcrf@todo{\origfootnote{#1}#2}%
\fi
\tcrf@todo
}
\def\tcrf@footnote@withlabel#1[#2]{\origfootnote[#2]{#1}}
\catcode`\@=13
]]
local label_macro_def = '\n\\def\\withfirstopt[#1]#2{#2[#1]}\n'
if not metadata['header-includes'] then
metadata['header-includes'] = pandoc.MetaBlocks(pandoc.RawBlock('context', ''))
end
metadata['header-includes']:insert(pandoc.RawBlock('context', footnote_redefinition))
metadata['header-includes']:insert(pandoc.RawBlock('context', label_macro_def))
end
return metadata
end
@ -254,20 +239,20 @@ local collect_note_labels = {
local function make_notelabel(pos)
-- About the strategy followed with ConTeXt,
-- see above tweak_footnote_if_ConTeXt.
-- see above support_footnote_label_ConTeXt.
local raw_code = ''
if pos == 'begin' then
if RAW_ATTRIBUTE == 'openxml' then
raw_code = string.gsub(
'<w:bookmarkStart w:id="{{label}}_Note" w:name="{{label}}_Note"/>',
'{{label}}', current_note_labels[1])
elseif RAW_ATTRIBUTE == 'context' then
raw_code = '\\withfirstopt[note:' .. current_note_labels[1] .. ']'
end
elseif pos == 'end' then
if RAW_ATTRIBUTE == 'openxml' then
raw_code = string.gsub('<w:bookmarkEnd w:id="{{label}}_Note"/>',
'{{label}}', current_note_labels[1])
elseif RAW_ATTRIBUTE == 'context' then
raw_code = '[note:' .. current_note_labels[1] .. ']'
end
end
return pandoc.RawInline(RAW_ATTRIBUTE, raw_code)
@ -617,7 +602,7 @@ end
return {
{ Meta = configure },
{ Meta = tweak_footnote_if_ConTeXt },
{ Meta = support_footnote_label_ConTeXt },
{ Note = set_notelabels },
{ Note = map_spans_to_notelabels },
{ Span = control_label_placement },