| 1 |
|
| 2 |
ts |
| 3 |
|
| 4 |
l |
| 5 |
o |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
dofile(os.getenv 'RMARKDOWN_LUA_SHARED') |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
if (not pandocAvailable {2,1}) then |
| 29 |
io.stderr:write("[WARNING] (pagebreak.lua) requires at least Pandoc 2.1. Lua Filter skipped.\n") |
| 30 |
return {} |
| 31 |
end |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
local stringify_orig = (require 'pandoc.utils').stringify |
| 36 |
|
| 37 |
local function stringify(x) |
| 38 |
return type(x) == 'string' and x or stringify_orig(x) |
| 39 |
end |
| 40 |
|
| 41 |
r. |
| 42 |
local pagebreak = { |
| 43 |
epub = '<p style="page-break-after: always;"> </p>', |
| 44 |
html = '<div style="page-break-after: always;"></div>', |
| 45 |
latex = '\\newpage{}', |
| 46 |
ooxml = '<w:p><w:r><w:br w:type="page"/></w:r></w:p>', |
| 47 |
odt = '<text:p text:style-name="Pagebreak"/>', |
| 48 |
context = '\\page' |
| 49 |
} |
| 50 |
|
| 51 |
local function pagebreaks_from_config (meta) |
| 52 |
local html_class = |
| 53 |
(meta.newpage_html_class and stringify(meta.newpage_html_class)) |
| 54 |
or os.getenv 'PANDOC_NEWPAGE_HTML_CLASS' |
| 55 |
if html_class and html_class ~= '' then |
| 56 |
pagebreak.html = string.format('<div class="%s"></div>', html_class) |
| 57 |
end |
| 58 |
|
| 59 |
local odt_style = |
| 60 |
(meta.newpage_odt_style and stringify(meta.newpage_odt_style)) |
| 61 |
or os.getenv 'PANDOC_NEWPAGE_ODT_STYLE' |
| 62 |
if odt_style and odt_style ~= '' then |
| 63 |
pagebreak.odt = string.format('<text:p text:style-name="%s"/>', odt_style) |
| 64 |
end |
| 65 |
end |
| 66 |
|
| 67 |
|
| 68 |
local function newpage(format) |
| 69 |
if format == 'docx' then |
| 70 |
return pandoc.RawBlock('openxml', pagebreak.ooxml) |
| 71 |
elseif format:match 'latex' then |
| 72 |
return pandoc.RawBlock('tex', pagebreak.latex) |
| 73 |
elseif format:match 'odt' then |
| 74 |
return pandoc.RawBlock('opendocument', pagebreak.odt) |
| 75 |
elseif format:match 'html.*' then |
| 76 |
return pandoc.RawBlock('html', pagebreak.html) |
| 77 |
elseif format:match 'epub' then |
| 78 |
return pandoc.RawBlock('html', pagebreak.epub) |
| 79 |
elseif format:match 'context' then |
| 80 |
return pandoc.RawBlock('context', pagebreak.context) |
| 81 |
else |
| 82 |
|
| 83 |
return pandoc.Para{pandoc.Str '\f'} |
| 84 |
end |
| 85 |
end |
| 86 |
|
| 87 |
local function is_newpage_command(command) |
| 88 |
return command:match '^\\newpage%{?%}?$' |
| 89 |
or command:match '^\\pagebreak%{?%}?$' |
| 90 |
end |
| 91 |
|
| 92 |
|
| 93 |
function RawBlock (el) |
| 94 |
|
| 95 |
if FORMAT:match 'tex$' then |
| 96 |
return nil |
| 97 |
end |
| 98 |
|
| 99 |
|
| 100 |
if el.format:match 'tex' and is_newpage_command(el.text) then |
| 101 |
|
| 102 |
|
| 103 |
return newpage(FORMAT) |
| 104 |
end |
| 105 |
|
| 106 |
return nil |
| 107 |
end |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
function Para (el) |
| 112 |
if #el.content == 1 and el.content[1].text == '\f' then |
| 113 |
return newpage(FORMAT) |
| 114 |
end |
| 115 |
end |
| 116 |
|
| 117 |
return { |
| 118 |
{Meta = pagebreaks_from_config}, |
| 119 |
{RawBlock = RawBlock, Para = Para} |
| 120 |
} |