summaryrefslogtreecommitdiff
path: root/_extensions/js/multicol-nohead/multicol-nohead.lua
blob: d242ac71b557e8eb2642828a4ef6bdae771c68f7 (plain)
  1. -- Render content below header in three columns
  2. local multicol_begin = pandoc.RawBlock('latex', [[
  3. \begin{multicols}{3}\raggedcolumns
  4. ]])
  5. local multicol_end = pandoc.RawBlock('latex', [[
  6. \end{multicols}
  7. ]])
  8. -- TODO: detect pre-quarto-filter pattern too
  9. local function is_newpage_command(elem)
  10. return elem.t == "RawBlock" and elem.text == "\\newpage{}"
  11. end
  12. -- TODO: include header level 2 and author paragraph above multicolumn
  13. function is_author(elem)
  14. return elem.t == Para and elem.content[1].text == "Forfatter:"
  15. end
  16. function Pandoc(doc)
  17. if FORMAT:match 'latex' then
  18. -- locate topmost-only elements where multicol state should change
  19. local multicol_now = false
  20. local multicol_places = {}
  21. for i, elem in ipairs(doc.blocks) do
  22. if elem.t == "Header" and elem.level == 1 then
  23. multicol_places[i] = multicol_now and "renew" or "begin"
  24. multicol_now = true
  25. elseif is_newpage_command(elem) then
  26. if multicol_now then
  27. multicol_places[i] = "end"
  28. multicol_now = false
  29. end
  30. elseif elem.t == "HorizontalRule" then
  31. if multicol_now then
  32. multicol_places[i] = "end"
  33. multicol_now = false
  34. end
  35. end
  36. end
  37. if multicol_now then
  38. multicol_places[#doc.blocks] = "end"
  39. end
  40. -- apply multicol, in reverse order since it shifts later indices
  41. for i = #doc.blocks, 1, -1 do
  42. local state = multicol_places[i]
  43. if state ~= nil then
  44. if state == "begin" then
  45. doc.blocks:insert(i + 1, multicol_begin)
  46. elseif state == "renew" then
  47. doc.blocks:insert(i + 1, multicol_begin)
  48. doc.blocks:insert(i, multicol_end)
  49. elseif state == "end" then
  50. doc.blocks:insert(i, multicol_end)
  51. end
  52. end
  53. end
  54. if #multicol_places >= 1 then
  55. return doc
  56. end
  57. end
  58. end