diff options
author | Jonas Smedegaard <dr@jones.dk> | 2025-06-04 11:09:43 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2025-06-07 08:55:46 +0200 |
commit | c7e52d08d2fb40fa69eee58d4d542868d1fec39b (patch) | |
tree | 2098ef9a13d093e9960e7bfdb90bcbde22ac8d04 /_extensions/js/multicol-nohead/multicol-nohead.lua | |
parent | a818d7acc5a06f614ab3d34b81aac0594bd99119 (diff) |
refactor to gain access to surrounding elements
Diffstat (limited to '_extensions/js/multicol-nohead/multicol-nohead.lua')
-rw-r--r-- | _extensions/js/multicol-nohead/multicol-nohead.lua | 59 |
1 files changed, 33 insertions, 26 deletions
diff --git a/_extensions/js/multicol-nohead/multicol-nohead.lua b/_extensions/js/multicol-nohead/multicol-nohead.lua index 2b98798..a5bba1b 100644 --- a/_extensions/js/multicol-nohead/multicol-nohead.lua +++ b/_extensions/js/multicol-nohead/multicol-nohead.lua @@ -7,41 +7,48 @@ local multicol_end = pandoc.RawBlock('latex', [[ \end{multicols} ]]) -local multicol_now = false - -- TODO: include header level 2 and author paragraph above multicolumn function is_author(elem) return elem.t == Para and elem.content[1].text == "Forfatter:" end -function multicol_delimit(elem) - if multicol_now == true then - return {multicol_end, elem, multicol_begin} - else - multicol_now = true - return {elem, multicol_begin} - end -end - -function Header(elem) +function Pandoc(doc) if FORMAT:match 'latex' then - if elem.level == 1 then - return multicol_delimit(elem) + + -- locate topmost-only elements where multicol state should change + local multicol_now = false + local multicol_places = {} + for i, elem in ipairs(doc.blocks) do + if elem.t == "Header" and elem.level == 1 then + multicol_places[i] = multicol_now and "renew" or "begin" + multicol_now = true + elseif elem.t == "HorizontalRule" then + if multicol_now then + multicol_places[i] = "end" + multicol_now = false + end + end + end + if multicol_now then + multicol_places[#doc.blocks] = "end" end - end -end -function HorizontalRule(elem) - if FORMAT:match 'latex' then - multicol_now = false - return {multicol_end, elem} - end -end + -- apply multicol, in reverse order since it shifts later indices + for i = #doc.blocks, 1, -1 do + local state = multicol_places[i] + if state ~= nil then + if state == "begin" then + doc.blocks:insert(i + 1, multicol_begin) + elseif state == "renew" then + doc.blocks:insert(i + 1, multicol_begin) + doc.blocks:insert(i, multicol_end) + elseif state == "end" then + doc.blocks:insert(i, multicol_end) + end + end + end -function Pandoc(doc) - if FORMAT:match 'latex' then - if multicol_now == true then - doc.blocks:insert(multicol_end) + if #multicol_places >= 1 then return doc end end |