Debian Codegraph @

inst/rmarkdown/lua/pagebreak.lua

raw symbols SCIP
1 --[[
2 pagebreak – convert raw LaTeX page breaks to other formats
3
4 Copyright © 2017-2019 Benct Philip Jonsson, Albert Krewinkel
5 Copyright © 2019-2021 Christophe Dervieux, Romain Lesur, Atsushi Yasumoto
6
7 Permission to use, copy, modify, and/or distribute this software for any
8 purpose with or without fee is hereby granted, provided that the above
9 copyright notice and this permission notice appear in all copies.
10
11 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 ]]
19
20 -- REQUIREMENTS: Load shared lua filter - see `shared.lua` for more details.
21 dofile(os.getenv 'RMARKDOWN_LUA_SHARED')
22
23 --[[
24 About the requirement:
25 * PANDOC_VERSION -> 2.1
26 * pandoc.utils -> 2.1
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 -- START OF THE FILTER'S FUNCTIONS --
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 --- configs – these are populated in the Meta filter.
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 --- Return a block element causing a page break in the given format.
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 -- fall back to insert a form feed character
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 -- Filter function called on each RawBlock element.
93 function RawBlock (el)
94 -- Don't do anything if the output is TeX
95 if FORMAT:match 'tex$' then
96 return nil
97 end
98 -- check that the block is TeX or LaTeX and contains only
99 -- \newpage or \pagebreak.
100 if el.format:match 'tex' and is_newpage_command(el.text) then
101 -- use format-specific pagebreak marker. FORMAT is set by pandoc to
102 -- the targeted output format.
103 return newpage(FORMAT)
104 end
105 -- otherwise, leave the block unchanged
106 return nil
107 end
108
109 -- Turning paragraphs which contain nothing but a form feed
110 -- characters into line breaks.
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 }