diff options
author | Jonas Smedegaard <dr@jones.dk> | 2025-06-08 17:52:04 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2025-06-08 18:07:03 +0200 |
commit | bfb36fee29aa52237374b9147c998776d1ca866c (patch) | |
tree | 4876f84086de82a8efdb30476eb6b77e77338e78 | |
parent | f3d58030df3d16ebad184c3b6dc6be200a3f5f0d (diff) |
handle advertisements
-rw-r--r-- | _extensions/js/multicol-nohead/multicol-nohead.lua | 81 |
1 files changed, 80 insertions, 1 deletions
diff --git a/_extensions/js/multicol-nohead/multicol-nohead.lua b/_extensions/js/multicol-nohead/multicol-nohead.lua index d242ac7..4b753a4 100644 --- a/_extensions/js/multicol-nohead/multicol-nohead.lua +++ b/_extensions/js/multicol-nohead/multicol-nohead.lua @@ -7,9 +7,41 @@ local multicol_end = pandoc.RawBlock('latex', [[ \end{multicols} ]]) +local ad_template = [[ +\begin{minipage}[%s][%s\textheight][c]{\textwidth} +\centering +\includegraphics[width=\linewidth,height=%s\textheight,keepaspectratio]{%s} +\end{minipage} +]] + +local function has_class(elem, class) + local classes = elem.classes + if not classes then + return false + end + for _, c in ipairs(classes) do + if c == class then + return true + end + end + return false +end + -- TODO: detect pre-quarto-filter pattern too local function is_newpage_command(elem) - return elem.t == "RawBlock" and elem.text == "\\newpage{}" + return (elem.t == "RawBlock" and elem.text == "\\newpage{}") + or (elem.t == "Para" and #elem.content == 5 and elem.content[3] == "pagebreak") +end + +-- a paragraph where initial element has class .ad is an advertising +local function is_advertising(elem) + if elem.t == "Para" then + local elem1 = elem.content[1] + if elem1.t == "Image" and has_class(elem1, "ad") then + return true + end + end + return false end -- TODO: include header level 2 and author paragraph above multicolumn @@ -17,6 +49,48 @@ function is_author(elem) return elem.t == Para and elem.content[1].text == "Forfatter:" end +-- wrap images with class .ad to span half or full page +local function ad(elem) + local amount = #elem.content + + -- 1 ad, spanning full page except with class .top or .bottom + if amount == 1 then + local elem1 = elem.content[1] + if elem1.t ~= "Image" or not elem1.src then + error("failed to parse advertisement") + return elem + end + if has_class(elem1, "top") then + return pandoc.RawBlock('latex', "\\noindent\n" + .. string.format(ad_template, "t", "0.5", "0.5", elem1.src)) + elseif has_class(elem1, "bottom") then + return pandoc.RawBlock('latex', "\\vspace*{\\fill}\\noindent\n" + .. string.format(ad_template, "b", "0.5", "0.5", elem1.src)) + end + return pandoc.RawBlock('latex',"\\noindent\n" + .. string.format(ad_template, "t", "1", "1", elem1.src)) + end + + -- 2 ads spanning full page + if amount == 3 then + local elem1 = elem.content[1] + local elem2 = elem.content[3] + if elem1.t ~= "Image" or not elem1.src + or elem2.t ~= "Image" or not elem2.src + then + error("failed to parse 2-element advertisement") + return elem + end + return pandoc.RawBlock('latex', "\\noindent\n" + .. string.format(ad_template, "t", "0.5", "0.5", elem1.src) + .. string.format(ad_template, "b", "0.5", "0.5", elem2.src)) + end + + error("failed to parse advertisement, wrong amount of elements: " + .. amount) + return elem +end + function Pandoc(doc) if FORMAT:match 'latex' then @@ -27,6 +101,11 @@ function Pandoc(doc) if elem.t == "Header" and elem.level == 1 then multicol_places[i] = multicol_now and "renew" or "begin" multicol_now = true + elseif is_advertising(elem) then + doc.blocks[i] = ad(elem) + if multicol_now then + multicol_places[i] = "renew" + end elseif is_newpage_command(elem) then if multicol_now then multicol_places[i] = "end" |