From c6eb3fa97592a63fe52349cff4f7132b5529251a Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 23 Apr 2021 11:57:30 -0400 Subject: [PATCH] add pagebreak script Signed-off-by: Christine Dodrill --- src/00_overhead/overhead.md | 2 +- src/build.sh | 7 ++- src/pagebreak.lua | 109 ++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 src/pagebreak.lua diff --git a/src/00_overhead/overhead.md b/src/00_overhead/overhead.md index c20028c..031055a 100644 --- a/src/00_overhead/overhead.md +++ b/src/00_overhead/overhead.md @@ -1,4 +1,4 @@ -
+\pagebreak
diff --git a/src/build.sh b/src/build.sh index b0f1104..1a649b3 100755 --- a/src/build.sh +++ b/src/build.sh @@ -7,7 +7,12 @@ FILES="$(ls ./**/*.md | grep -v plans)" echo $FILES -pandoc -o ../out/Spellblade.epub --resource-path=. --css style.css title.txt $FILES +pandoc \ + -o ../out/Spellblade.epub \ + --resource-path=. \ + --css style.css \ + --lua-filter ./pagebreak.lua \ + title.txt $FILES ebook-convert ../out/Spellblade.epub ../out/Spellblade.mobi pandoc --lua-filter wordcount.lua $FILES diff --git a/src/pagebreak.lua b/src/pagebreak.lua new file mode 100644 index 0000000..b931051 --- /dev/null +++ b/src/pagebreak.lua @@ -0,0 +1,109 @@ +--[[ +pagebreak – convert raw LaTeX page breaks to other formats + +Copyright © 2017-2021 Benct Philip Jonsson, Albert Krewinkel + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +]] +local stringify_orig = (require 'pandoc.utils').stringify + +local function stringify(x) + return type(x) == 'string' and x or stringify_orig(x) +end + +--- configs – these are populated in the Meta filter. +local pagebreak = { + asciidoc = '<<<\n\n', + context = '\\page', + epub = '

', + html = '
', + latex = '\\newpage{}', + ms = '.bp', + ooxml = '', + odt = '' +} + +local function pagebreaks_from_config (meta) + local html_class = + (meta.newpage_html_class and stringify(meta.newpage_html_class)) + or os.getenv 'PANDOC_NEWPAGE_HTML_CLASS' + if html_class and html_class ~= '' then + pagebreak.html = string.format('
', html_class) + end + + local odt_style = + (meta.newpage_odt_style and stringify(meta.newpage_odt_style)) + or os.getenv 'PANDOC_NEWPAGE_ODT_STYLE' + if odt_style and odt_style ~= '' then + pagebreak.odt = string.format('', odt_style) + end +end + +--- Return a block element causing a page break in the given format. +local function newpage(format) + if format:match 'asciidoc' then + return pandoc.RawBlock('asciidoc', pagebreak.asciidoc) + elseif format == 'context' then + return pandoc.RawBlock('context', pagebreak.context) + elseif format == 'docx' then + return pandoc.RawBlock('openxml', pagebreak.ooxml) + elseif format:match 'epub' then + return pandoc.RawBlock('html', pagebreak.epub) + elseif format:match 'html.*' then + return pandoc.RawBlock('html', pagebreak.html) + elseif format:match 'latex' then + return pandoc.RawBlock('tex', pagebreak.latex) + elseif format:match 'ms' then + return pandoc.RawBlock('ms', pagebreak.ms) + elseif format:match 'odt' then + return pandoc.RawBlock('opendocument', pagebreak.odt) + else + -- fall back to insert a form feed character + return pandoc.Para{pandoc.Str '\f'} + end +end + +local function is_newpage_command(command) + return command:match '^\\newpage%{?%}?$' + or command:match '^\\pagebreak%{?%}?$' +end + +-- Filter function called on each RawBlock element. +function RawBlock (el) + -- Don't do anything if the output is TeX + if FORMAT:match 'tex$' then + return nil + end + -- check that the block is TeX or LaTeX and contains only + -- \newpage or \pagebreak. + if el.format:match 'tex' and is_newpage_command(el.text) then + -- use format-specific pagebreak marker. FORMAT is set by pandoc to + -- the targeted output format. + return newpage(FORMAT) + end + -- otherwise, leave the block unchanged + return nil +end + +-- Turning paragraphs which contain nothing but a form feed +-- characters into line breaks. +function Para (el) + if #el.content == 1 and el.content[1].text == '\f' then + return newpage(FORMAT) + end +end + +return { + {Meta = pagebreaks_from_config}, + {RawBlock = RawBlock, Para = Para} +}