various code improvements

This commit is contained in:
Bastien Dumont
2025-12-07 21:47:29 +01:00
parent 5f69e0a5ec
commit 17da4d81d8

View File

@ -87,13 +87,13 @@ end
local config = {}
function add_rendering(config, class,
local function add_rendering(cfg, class,
body_text, note_text,
docx_body_text, docx_note_text,
odt_body_text, odt_note_text,
csname, position)
-- The values are plain strings (not the empty string!) or nil.
config[class] = {
cfg[class] = {
body_text = body_text,
note_text = note_text,
docx_body_text = docx_body_text,
@ -117,19 +117,19 @@ local function get_renderings_config(meta)
for key, value in pairs(meta) do
if key == 'mrgnn-define-renderings' then
for i = 1, #value do
this_value = value[i]
local definition = value[i]
add_rendering(config,
meta_to_str(this_value.class),
meta_to_str(this_value['body-text']),
meta_to_str(this_value['note-text']),
meta_to_str(this_value['docx-body-text']),
meta_to_str(this_value['docx-note-text']),
meta_to_str(this_value['odt-body-text']
or this_value['body-text']),
meta_to_str(this_value['odt-note-text']
or this_value['note-text']),
meta_to_str(this_value['csname']),
meta_to_str(this_value['position']) or POS_DEFAULT
meta_to_str(definition.class),
meta_to_str(definition['body-text']),
meta_to_str(definition['note-text']),
meta_to_str(definition['docx-body-text']),
meta_to_str(definition['docx-note-text']),
meta_to_str(definition['odt-body-text']
or definition['body-text']),
meta_to_str(definition['odt-note-text']
or definition['note-text']),
meta_to_str(definition['csname']),
meta_to_str(definition['position']) or POS_DEFAULT
)
end
end
@ -256,6 +256,7 @@ local function get_replacement(placeholder,
a plain string otherwise.
Markdown is interpreted in the first case only.
]]--
local replacement
if placeholder == 'content' then
if replacement_is_list then
replacement = instance_content
@ -266,7 +267,7 @@ local function get_replacement(placeholder,
local key = get_substring(placeholder, #'attr.' + 1)
if replacement_is_list then
local replacement_markdown = instance_attr[key]
or '**' .. UNDEFINED .. '**'
or ('**' .. UNDEFINED .. '**')
replacement = to_pandoc_inlines(replacement_markdown)
else
replacement = instance_attr[key] or UNDEFINED
@ -457,17 +458,17 @@ local function template_to_function(template)
end
end
local function define_rendering_functions(meta)
local function define_rendering_functions()
local format_prefix = ''
if FORMAT == 'opendocument' then
format_prefix = 'odt_'
elseif FORMAT == 'openxml' then
format_prefix = 'docx_'
end
for class_name, class_config in pairs(config) do
config[class_name].render = {}
for _, class_config in pairs(config) do
class_config.render = {}
for _, part in ipairs({'body', 'note'}) do
config[class_name].render[part] =
class_config.render[part] =
template_to_function(class_config[format_prefix .. part .. '_text'])
end
end
@ -484,7 +485,7 @@ end
local function Meta(meta)
get_renderings_config(meta)
define_rendering_functions(meta)
define_rendering_functions()
set_macro_definition(meta)
return meta
end
@ -544,9 +545,9 @@ end
local function render_margin_notes(span)
for class_name, class_config in pairs(config) do
if span.classes:includes(class_name) then
local render_note = config[class_name].render.note
local render_body = config[class_name].render.body
local note_position = config[class_name].position
local render_note = class_config.render.note
local render_body = class_config.render.body
local note_position = class_config.position
local note_id = span.identifier
local margin_note = {}
local body = {}
@ -561,7 +562,7 @@ local function render_margin_notes(span)
local span_with_note = {}
if note_position == 'before' then
span_with_note = margin_note
insert_in_table(span_with_note, span)
insert_in_table(span_with_note, span)
elseif note_position == 'after' then
span_with_note = { span, unpack_table(margin_note) }
else