diff --git a/converters/headers.lua b/converters/headers.lua new file mode 100644 index 0000000..4168764 --- /dev/null +++ b/converters/headers.lua @@ -0,0 +1,69 @@ +local pagebreak = { + asciidoc = '<<<\n\n', + context = '\\page', + epub = '

', + html = '
', + latex = '\\newpage{}', + ms = '.bp', + ooxml = '', + odt = '' +} +local title = ''; +local title_inHaders = true; + +local stringify_orig = (require 'pandoc.utils').stringify + +local function stringify(x) + return type(x) == 'string' and x or stringify_orig(x) +end + +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 + +function Meta(meta) + title = (meta.title and stringify(meta.title)) or title + if title ~= '' then + title_inHaders = false; + end +end + +function Header(el) + if title_inHaders then + if el.level == 1 then + title = el.content + return {} + else + el.level = el.level - 1; + end + end + + if el.level == 1 or el.level == 2 then + return { newpage(FORMAT), el } + end +end + +return { + {Meta = Meta}, + {Header = Header}, + {Meta = function (meta) meta.title = title; return meta end} +} diff --git a/converters/mdToLatex.sh b/converters/mdToLatex.sh index 60b3480..199ce97 100644 --- a/converters/mdToLatex.sh +++ b/converters/mdToLatex.sh @@ -34,27 +34,20 @@ do sed -i "$BUILD_DIR/$(basename "$md_src")" \ -e 's|\[toc\]||' \ -e 's|^\[parent\].*$||' \ - -e 's|^# |\\newpage\n# |' \ - -e 's|^## |\\newpage\n## |' \ -e 's|\[\([^]]*\)\](#\([^)]*\))|[\1](#\L\2)|' \ -e "s|https://live.kladjes.nl/uploads|${BASE_DIR}/latex/images|" \ -e "s|\`\`\`mermaid|\`\`\`{.mermaid loc=${BASE_DIR}/latex/images/$(basename "$md_src")}|" sed -i "$TEMP_MD_FILE" \ - -e "s|^\!\[.*\]($md_src)\$|\`\`\`\\{.include\\}\n$(basename "$md_src")\n\`\`\`|" + -e "s|^\!\[.*\]($md_src)\$|\`\`\`\\{.include shift-heading-level-by=1\\}\n$(basename "$md_src")\n\`\`\`|" done download_images "$TEMP_MD_FILE" -title="$(grep '^# ' "$MD_FILE" | head -n 1 | sed 's|^# ||')" - sed -i "$TEMP_MD_FILE" \ -e 's|\[toc\]|\\tableofcontents|' \ -e 's|^\[parent\].*$||' \ - -e 's|^# .*$||' \ - -e 's|^#||' \ - -e 's|^# |\\newpage\n# |' \ -e 's|\[\([^]]*\)\](#\([^)]*\))|[\1](#\L\2)|' \ -e "s|https://live.kladjes.nl/uploads|${BASE_DIR}/latex/images|" \ -e "s|\`\`\`mermaid|\`\`\`{.mermaid loc=${BASE_DIR}/latex/images/$(basename "$MD_FILE")}|" @@ -63,7 +56,9 @@ sed -i "$TEMP_MD_FILE" \ mkdir -p ${BASE_DIR}/latex/images/$(basename "$MD_FILE") cd "$BUILD_DIR" -pandoc --lua-filter=../../converters/include-files.lua \ +pandoc \ + --lua-filter=../../converters/include-files.lua \ + --lua-filter=../../converters/headers.lua \ --to=latex \ --from=markdown+abbreviations \ --template "${BASE_DIR}/converters/template.latex" \ @@ -71,15 +66,3 @@ pandoc --lua-filter=../../converters/include-files.lua \ --dpi 300 \ "$(basename "$TEMP_MD_FILE")" cd "$BASE_DIR" - -# for line in $(grep '^!\[.*\](.*\.md)$' "$TEMP_MD_FILE" | sed 's/ /%20;/g') -# do -# src=$(echo "$line" | sed -e 's/^.*(//' -e 's/).*$//' -e 's/%20;/ /g') - -# sed -i "$TEMP_MD_FILE" \ -# -e "s/^!\[.*\]($src)\$/\\include{$src}/" -# done - -sed --in-place \ - -e "s|?title?|$title|" \ - "$TEX_FILE" diff --git a/converters/pagebreak.lua b/converters/pagebreak.lua new file mode 100644 index 0000000..b931051 --- /dev/null +++ b/converters/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} +} diff --git a/converters/template.latex b/converters/template.latex index 25ada9d..c58f2c4 100644 --- a/converters/template.latex +++ b/converters/template.latex @@ -76,7 +76,7 @@ $endif$ . \vskip 10em \begin{center} - {\Huge\fontUbuntu ?title? \par} + {\Huge\fontUbuntu $title$ \par} \vskip 3em {\huge\fontUbuntu $sub_title$ \par} \end{center} @@ -96,7 +96,7 @@ $endif$ \pagestyle{fancy} \fancyhead{} % clear all header fields -\fancyhead[LO]{\color{gray}\fontUbuntu ?title?} +\fancyhead[LO]{\color{gray}\fontUbuntu $title$} \fancyhead[RO]{\color{gray}\fontUbuntu $sub_title$} \fancyfoot{} % clear all footer fields \fancyfoot[LO]{\color{gray}\fontUbuntu $for(auther)$$auther.name_short$${sep}, $endfor$}