pandoc-lua-filters/conclusions/conclusions.lua

101 lines
2.6 KiB
Lua
Raw Permalink Normal View History

2024-05-24 12:41:54 +01:00
-- conclusions.lua
-- A Pandoc Lua filter to distinguish conclusions from the surrounding text.
-- Copyright 2024 Bastien Dumont (bastien.dumont [at] posteo.net)
-- This file is under the MIT License: see LICENSE for more details
local MAX_HEADER_LEVEL = 6
local config = {
innermost_level_header = 2,
innermost_level_space = 3,
ccl_explicit_titles = {
'Conclusion de la partie',
'Conclusion du chapitre'
}
}
local function check_validity(key, value)
if key ~= 'ccl-explicit-titles'
and (value == nil or value < 1 or value > MAX_HEADER_LEVEL)
then
error(key .. ' must be a numeric value ' ..
'comprised between 1 and ' .. MAX_HEADER_LEVEL .. '.\n')
end
end
local function get_config(meta)
if meta.conclusions then
for key, value in pairs(meta.conclusions) do
if key ~= 'ccl-explicit-titles' then
value = math.tointeger(value[1].text)
end
check_validity(key, value)
config[string.gsub(key, '-', '_')] = value
end
end
end
local function set_raw_space()
local raw_space
if FORMAT == 'native' then
FORMAT = pandoc.system.environment().TESTED_FORMAT
set_raw_space()
elseif FORMAT == 'context' then
raw_space = pandoc.RawBlock('tex', '\\blank[big]')
elseif FORMAT == 'docx' then
raw_space = pandoc.RawBlock('openxml', '<w:p/>')
elseif FORMAT == 'latex' then
raw_space = pandoc.RawBlock('tex', '\\bigskip')
else
error(FORMAT ..
' output not supported by complex-paragraphs.lua\n')
end
return raw_space
end
local raw_space = set_raw_space()
local function get_level(div)
local level = MAX_HEADER_LEVEL + 1
local set_value = div.attributes.level
if set_value then
set_value = math.tointeger(set_value)
check_validity('level', set_value)
level = set_value
else
io.stderr:write('level attribute missing to a div ' ..
'with class conclusion or ccl, nothing done.\n')
end
return level
end
local function get_separator_as_list(level)
local separator = {}
if level <= config.innermost_level_header then
local header =
pandoc.Header(level + 1, config.ccl_explicit_titles[level])
table.insert(header.classes, 'unnumbered')
separator = { header }
elseif level <= config.innermost_level_space then
separator = { raw_space }
end
return separator
end
function Div(div)
local classes = div.classes
if classes:includes('conclusion')
or classes:includes('ccl')
then
local level = get_level(div)
local formatted_conclusion = get_separator_as_list(level)
table.insert(formatted_conclusion, div)
return formatted_conclusion
end
end
return {
{ Meta = get_config },
{ Div = Div }
}