Skip to content

Instantly share code, notes, and snippets.

@austinjp
Created September 11, 2022 17:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save austinjp/47540f5cd7ec68e8c6f0bef5f1d75ea4 to your computer and use it in GitHub Desktop.
Save austinjp/47540f5cd7ec68e8c6f0bef5f1d75ea4 to your computer and use it in GitHub Desktop.
-- Extension of answer by Tarleb on Stackoverflow (https://stackoverflow.com/a/52131435/3888000) to include docx section ends with portrait/landscape orientation changes.
-- Also uses officer package syntax to create sections breaks
local function newpage(format)
if format == 'docx' then
local pagebreak = '<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\"><w:r><w:br w:type="page"/></w:r></w:p>'
return pandoc.RawBlock('openxml', pagebreak)
else
return pandoc.Para{pandoc.Str '\f'}
end
end
local function endLandscape(format)
if format == 'docx' then
local pagebreak = '<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\"><w:pPr><w:sectPr><w:pgSz w:w="16838" w:h="11906" w:orient="landscape"/></w:sectPr></w:pPr></w:p>'
return pandoc.RawBlock('openxml', pagebreak)
else
return pandoc.Para{pandoc.Str '\f'}
end
end
local function endPortrait(format)
if format == 'docx' then
local pagebreak = '<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\"><w:pPr><w:sectPr><w:pgSz w:w="11906" w:h="16838"/></w:sectPr></w:pPr></w:p>'
return pandoc.RawBlock('openxml', pagebreak)
else
return pandoc.Para{pandoc.Str '\f'}
end
end
local function endContinuous(format)
if format == 'docx' then
local pagebreak = '<w:p xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:w14=\"http://schemas.microsoft.com/office/word/2010/wordml\"><w:pPr><w:sectPr><w:officersection/><w:type w:val=\"continuous\"/></w:sectPr></w:pPr></w:p>'
return pandoc.RawBlock('openxml', pagebreak)
else
return pandoc.Para{pandoc.Str '\f'}
end
end
-- Filter function called on each RawBlock element.
function RawBlock (el)
if el.text:match '\\newpage' then
return newpage(FORMAT)
elseif el.text:match '\\newpageLandscape' then
return newpageLandscape(FORMAT)
elseif el.text:match '\\endLandscape' then
return endLandscape(FORMAT)
elseif el.text:match '\\endContinuous' then
return endContinuous(FORMAT)
elseif el.text:match '\\endPortrait' then
return endPortrait(FORMAT)
end
-- otherwise, leave the block unchanged
return nil
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment