Skip to content

Instantly share code, notes, and snippets.

@bpj
Last active October 20, 2018 15:08
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 bpj/d48e78a4ad5ef3ca416a5fd93066438c to your computer and use it in GitHub Desktop.
Save bpj/d48e78a4ad5ef3ca416a5fd93066438c to your computer and use it in GitHub Desktop.
Let Pandoc combine several files with some markup inserted between them

This Makefile does the trick of producing a file with Pandoc from a directory of Markdown files, with some Markdown appended after the content of each Markdown file — by default three blocks of raw markup which will insert a pagebreak in DOCX, HTML or LaTeX output, but it can be taken from a file and so can be anything — before producing a single output file (by default a DOCX file) from the combined inputs with the appendage file inserted after each input file.

The content to append is taken from the file ./append.md. If it doesn’t already exist it will be created with the following content:

````{=openxml}
<w:p><w:r><w:br w:type="page" /></w:r></w:p>
````

````{=html}
<div style="page-break-after: always;"></div>
````

````{=latex}
\newpage
````

The Makefile has been tested with GNU make and the dash shell. It should work with bash too.

The temporary directory can be removed by running make clean.

There must be no space or hyphen characters in file or directory names!

The following make variables can be used to influence the selection of source files, the location of temporary copies and the name of the output DOCX file:

-------------------------------------------------------------------------------------------------
Variable            Default                                       Description
------------------- --------------------------------------------- -------------------------------
`MD_GLOB`           `*.md`                                        Glob pattern for Markdown files

`MD_DIR`            `./md_files`                                  Glob pattern for directory with
                                                                    Markdown files

`MD_FILES`          `$(wildcard $(MD_DIR)/$(MD_GLOB))`            List of Markdown files

`APPEND_MD`         `./append.md`                                 Name of Markdown file to append
                                                                    to each `MD_FILES`

`PANDOC`            `pandoc`                                      (Path to) Pandoc executable

`TO_FMT`            (empty)                                       The Pandoc output format

`FILTERS`           (empty)                                       List of Pandoc JSON filters

`FILTER_OPTS`       `$(addprefix --filter=,$(FILTERS))`           Expanded list of JSON filters

`LUA_FILTERS`       (empty)                                       List of Pandoc Lua filters

`LUA_FILTER_OPTS`   `$(addprefix --lua-filter=,$(LUA_FILTERS))`   Expanded list of Lua filters

`PANDOC_OPTS`       (empty)                                       (Other) options to Pandoc

`OUTPUT`            `./combined.docx`                             (Path to) name of output (DOCX)
                                                                    file
-------------------------------------------------------------------------------------------------

Feel free to adjust these variables as appropriate in your copy of the Makefile!

This software is Copyright (c) 2018 by Benct Philip Jonsson.

This is free software, licensed under:

The MIT (X11) License

See http://www.opensource.org/licenses/mit-license.php.

# This Makefile does the trick of producing a file with Pandoc from a
# directory of Markdown files, with some Markdown appended after the
# content of each Markdown file -- by default three blocks of raw markup
# which will insert a pagebreak in DOCX, HTML or LaTeX output, but it can
# be taken from a file and so can be anything -- before producing a single
# output file (by default a DOCX file) from the combined inputs with the
# appendage file inserted after each input file.
# More info and latest version:
#
# <https://gist.github.com/bpj/d48e78a4ad5ef3ca416a5fd93066438c>
# This software is Copyright (c) 2018 by Benct Philip Jonsson.
#
# This is free software, licensed under:
#
# The MIT (X11) License
#
# See <http://www.opensource.org/licenses/mit-license.php>.
# Glob pattern for Markdown files
MD_GLOB ?= *.md
# Glob pattern for directory with Markdown files
MD_DIR ?= ./md_files
# List of Markdown files
MD_FILES ?= $(wildcard $(MD_DIR)/$(MD_GLOB))
# Name of Markdown file to append to each MD_FILES
APPEND_MD ?= ./append.md
# Input files: each MD_FILES with APPEND_MD after it
INPUTS ?= $(foreach x,$(MD_FILES),$(x) $(APPEND_MD))
# (Path to) Pandoc executable
PANDOC ?= pandoc
# List of Pandoc JSON filters
FILTERS ?=
# Expanded list of JSON filters
FILTER_OPTS ?= $(addprefix --filter=,$(FILTERS))
# List of Pandoc Lua filters
LUA_FILTERS ?=
# Expanded list of Lua filters
LUA_FILTER_OPTS ?= $(addprefix --lua-filter=,$(LUA_FILTERS))
# Options to Pandoc
PANDOC_OPTS ?=
# Output format
TO_FMT ?=
# --to option
TO_OPT = $(if $(TO_FMT),--to=$(TO_FMT))
# (Path to) name of output (DOCX) file
OUTPUT ?= ./combined.docx
$(OUTPUT): $(INPUTS)
$(PANDOC) $(TO_OPT) $(FILTER_OPTS) $(LUA_FILTER_OPTS) $(PANDOC_OPTS) -o $@ $(INPUTS)
# use printf because echo does non-funny things to backslashes...
$(APPEND_MD):
printf '\n\n````{=openxml}\n<w:p><w:r><w:br w:type="page" /></w:r></w:p>\n````\n\n' >> $(APPEND_MD)
printf '\n\n````{=html}\n<div style="page-break-after: always;"></div>\n````\n\n' >> $(APPEND_MD)
printf '\n\n````{=latex}\n\\newpage\n````\n\n' >> $(APPEND_MD)
.PHONY: showvars
showvars:
@echo 'MD_GLOB="$(MD_GLOB)"'
@echo 'MD_DIR="$(MD_DIR)"'
@echo 'MD_FILES="$(MD_FILES)"'
@echo 'TEMP_DIR="$(TEMP_DIR)"'
@echo 'APPEND_MD="$(APPEND_MD)"'
@echo 'INPUTS="$(INPUTS)"'
@echo 'PANDOC="$(PANDOC)"'
@echo 'TO_FMT="$(TO_FMT)"'
@echo 'TO_OPT="$(TO_OPT)"'
@echo 'FILTERS="$(FILTERS)"'
@echo 'FILTER_OPTS="$(FILTER_OPTS)"'
@echo 'LUA_FILTERS="$(LUA_FILTERS)"'
@echo 'LUA_FILTER_OPTS="$(LUA_FILTER_OPTS)"'
@echo 'PANDOC_OPTS="$(PANDOC_OPTS)"'
@echo 'OUTPUT="$(OUTPUT)"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment