Warn instead of erroring.

This commit is contained in:
Bastien Dumont 2024-05-29 15:06:16 +02:00
parent ea71cae43c
commit 5c60b8f226

View File

@ -15,6 +15,10 @@ local PLACE_LABEL_ATTR = 'refanchor'
local IS_CONFIG_ARRAY = { ['additional_types'] = true }
local RAW_ATTRIBUTE
local function warning(message)
io.stderr:write('WARNING [text-crossrefs]: ' .. message .. '\n')
end
-- ConTeXt-specific tweak in order to add the label to the footnote
--[[
Placing the label in square brackets immediatly after \footnote
@ -212,8 +216,8 @@ local function control_label_placement(span)
span.content:insert(pandoc.Span({}, { id = id .. '-end' }))
span.identifier = nil
elseif label_placement ~= 'beg' then
error('Invalid value ' .. label_placement .. ' on attribute ' .. PLACE_LABEL_ATTR .. ': ' ..
'shoud be “beg” (default), “end” of “both”.')
warning('Invalid value ' .. label_placement .. ' on attribute ' .. PLACE_LABEL_ATTR .. ': ' ..
'shoud be “beg” (default), “end” of “both”. Using the defaults.')
end
end
return span
@ -237,14 +241,14 @@ local function labelize_span(span)
end
end
local current_note_labels = {}
local labels_in_current_note = {}
local collect_note_labels = {
Span = function(span)
if span.identifier ~= ''
and (config.only_explicit_labels == 'false' or span.classes:includes('label'))
then
table.insert(current_note_labels, span.identifier)
table.insert(labels_in_current_note, span.identifier)
end
end
}
@ -257,14 +261,14 @@ local function make_notelabel(pos)
if RAW_ATTRIBUTE == 'openxml' then
raw_code = string.gsub(
'<w:bookmarkStart w:id="{{label}}_Note" w:name="{{label}}_Note"/>',
'{{label}}', current_note_labels[1])
'{{label}}', labels_in_current_note[1])
elseif RAW_ATTRIBUTE == 'context' then
raw_code = '\\withfirstopt[note:' .. current_note_labels[1] .. ']'
raw_code = '\\withfirstopt[note:' .. labels_in_current_note[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])
'{{label}}', labels_in_current_note[1])
end
end
return pandoc.RawInline(RAW_ATTRIBUTE, raw_code)
@ -277,18 +281,18 @@ local function labelize_note(note)
return { label_begin, note, label_end }
end
local function map_text_to_note_labels(current_note_labels)
local note_label = 'note:' .. current_note_labels[1]
for _, text_label in ipairs(current_note_labels) do
text_to_note_labels[text_label] = note_label
local function map_text_to_note_labels(labels_in_current_note)
local note_label = 'note:' .. labels_in_current_note[1]
for _, label in ipairs(labels_in_current_note) do
text_to_note_labels[label] = note_label
end
end
function set_notelabels(note)
current_note_labels = {}
labels_in_current_note = {}
pandoc.walk_inline(note, collect_note_labels)
if #current_note_labels > 0 then
map_text_to_note_labels(current_note_labels)
if #labels_in_current_note > 0 then
map_text_to_note_labels(labels_in_current_note)
return labelize_note(note)
end
end
@ -338,8 +342,8 @@ local function parse_next_reference(raw_references, beg_of_search)
if beg_of_search < #raw_references then
-- The delimiter can be composed of more than one character.
local delim_beg, delim_end = string.find(raw_references,
config.references_enum_separator,
beg_of_search, true)
config.references_enum_separator,
beg_of_search, true)
if delim_beg then
reference = string.sub(raw_references, beg_of_search, delim_beg - 1)
next_ref_beg = delim_end + 1
@ -366,15 +370,17 @@ local function parse_references_enum(raw_references)
end
local function error_on_attr(attr_key, attr_value, span_content)
error('Invalid value "' .. attr_value .. '" for attribute "' .. attr_key ..
warning('Invalid value "' .. attr_value .. '" for attribute "' .. attr_key ..
'" in the span with class "' .. TEXT_CROSSREF_CLASS ..
'" whose content is "' .. stringify(span_content) .. '".')
'" whose content is "' .. stringify(span_content) .. '". ' ..
'Using the defaults.')
end
local function get_ref_type(span)
local ref_type = span.attributes[REF_TYPE_ATTR] or config.default_reftype
if not accepted_types[ref_type] then
error_on_attr(REF_TYPE_ATTR, ref_type, span.content)
ref_type = config.default_reftype
end
return ref_type
end
@ -383,6 +389,7 @@ local function if_prefixed(span)
local prefixed_attr_value = span.attributes[PREFIXED_ATTR] or config.default_prefixref
if prefixed_attr_value ~= 'yes' and prefixed_attr_value ~= 'no' then
error_on_attr(PREFIXED_ATTR, prefixed_attr_value, span.content)
prefixed_attr_value = config.default_prefixref
end
local is_prefixed = true
if prefixed_attr_value == 'no' then is_prefixed = false end
@ -425,7 +432,12 @@ local function make_crossrefenum_references_list(refs, ref_type)
if FORMAT == 'context'
and (ref_type == 'note' or ref_type == 'pagenote')
then
anchor = text_to_note_labels[anchor]
local note_label = text_to_note_labels[anchor]
if note_label then
anchor = note_label
else
warning('Wrong reference to non-existent label "' .. anchor .. '".')
end
end
local texified_ref = '{' .. anchor
if ref.end_of_range then