Skip to content

Commit 18830b6

Browse files
committed
feat: allow to specify height/width for landscape (docx)
1 parent 3cea512 commit 18830b6

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

src/resources/filters/quarto-post/landscape.lua

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,38 @@
11
-- landscape.lua
22
-- Copyright (C) 2024-2024 Posit Software, PBC
33
--
4-
-- Author: [Edvin Syk](https://github.com/edvinsyk/)
4+
-- Author: [Edvin Syk](https://github.com/edvinsyk/)
55

66
function landscape_div()
77
local ooxml = function(s)
88
return pandoc.RawBlock('openxml', s)
99
end
10-
10+
11+
local function to_twips(x)
12+
local num_unit = { x:match("([%d%.]+)%s*(%a+)") }
13+
local num = tonumber(num_unit[1])
14+
local unit = num_unit[2]
15+
if unit == "cm" then
16+
return num * 1440 / 2.54
17+
elseif unit == "in" then
18+
return num * 1440
19+
else
20+
error("Unsupported unit: " .. tostring(unit))
21+
end
22+
end
23+
24+
local docx_section_open = '<w:p><w:pPr><w:sectPr>'
25+
local docx_section_close = '</w:sectPr></w:pPr></w:p>'
1126
-- Define the end of a portrait section for DOCX
12-
local end_portrait_section = ooxml '<w:p><w:pPr><w:sectPr></w:sectPr></w:pPr></w:p>'
13-
14-
-- Define the end of a landscape section for DOCX
15-
local end_landscape_section = ooxml [[
16-
<w:p>
17-
<w:pPr>
18-
<w:sectPr>
19-
<w:pgSz w:h="11906" w:w="16838" w:orient="landscape" />
20-
</w:sectPr>
21-
</w:pPr>
22-
</w:p>
23-
]]
24-
27+
local end_portrait_section = ooxml(docx_section_open .. docx_section_close)
28+
2529
-- LateX commands for starting and ending a landscape section
2630
local landscape_start_pdf = pandoc.RawBlock('latex', '\\begin{landscape}')
2731
local landscape_end_pdf = pandoc.RawBlock('latex', '\\end{landscape}')
28-
32+
2933
local landscape_start_typst = pandoc.RawBlock('typst', '#set page(flipped: true)')
3034
local landscape_end_typst = pandoc.RawBlock('typst', '#set page(flipped: false)')
31-
35+
3236
local function Meta(meta)
3337
metaInjectLatex(meta, function(inject)
3438
inject("\\usepackage{pdflscape}")
@@ -40,6 +44,15 @@ function landscape_div()
4044
if div.classes:includes('landscape') then
4145
if FORMAT:match 'docx' then
4246
-- DOCX-specific landscape orientation
47+
local height = pandoc.utils.stringify(div.attributes.height) or "8.5cm"
48+
local width = pandoc.utils.stringify(div.attributes.width) or "11cm"
49+
50+
-- Define the end of a landscape section for DOCX
51+
local end_landscape_section = ooxml(
52+
docx_section_open ..
53+
'<w:pgSz w:h="' .. to_twips(height) .. '" w:w="' .. to_twips(width) .. '" w:orient="landscape" />' ..
54+
docx_section_close
55+
)
4356
div.content:insert(1, end_portrait_section)
4457
div.content:insert(end_landscape_section)
4558
elseif FORMAT:match 'latex' then

0 commit comments

Comments
 (0)