Skip to content

Commit 2b1441f

Browse files
committed
feat: Add facility to convert documents with pandoc
Add a facility to, with relative ease, create OpenDocument Text and Word documents from the original Markdown. To use, run tox -e to-odt -- -o output.odt input1.md input2.md input3.md or tox -e to-docx -- -o output.docx input1.md input2.md input3.md To achieve this, a simple script concatenates the input files, processes them against mkdocs.yml using the Jinja2 CLI, and feeds the result to pandoc with appropriate command-line options. For branding, reference.docx and reference.odt files can be placed into the pandoc subdirectory. Also, add bashate testing for the script itself.
1 parent 2ec2fe3 commit 2b1441f

File tree

4 files changed

+81
-2
lines changed

4 files changed

+81
-2
lines changed

.github/workflows/tox.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
# necessary here, but for some reason tox invokes no testenvs
2525
# at all if invoked as just "tox" from the GitHub Actions
2626
# workflow. Until that is fixed, name the testenvs.
27-
run: tox -e gitlint,yamllint,build
27+
run: tox -e gitlint,yamllint,bashate,build
2828
- name: Upload build
2929
uses: actions/upload-artifact@v2
3030
with:

pandoc/README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
This directory is meant to be used as a Pandoc user data directory,
2+
i.e. something to be referenced with `pandoc`’s `--data-dir` option.
3+
4+
This directory can include, among others,
5+
6+
* a `reference.odt` template for use with `pandoc -t odt` (for
7+
OpenDocument Text output, as used by LibreOffice),
8+
* a `reference.docx` template for use with `pandoc -t docx` (for
9+
Microsoft Word documents).

scripts/from-markdown.sh

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
#
3+
# from-markdown.sh — a wrapper script to convert Markdown to
4+
# Pandoc-supported output formats
5+
6+
set -e
7+
8+
usage() {
9+
echo "Usage: $0 [-t format] [-o outfile] infile..." >&2
10+
}
11+
12+
output="-"
13+
format="docx"
14+
15+
while getopts "o:t:h" opt; do
16+
case "${opt}" in
17+
o)
18+
output=${OPTARG}
19+
;;
20+
t)
21+
format=${OPTARG}
22+
;;
23+
h)
24+
usage
25+
exit 0
26+
;;
27+
*)
28+
usage
29+
exit 1
30+
;;
31+
esac
32+
done
33+
shift $((OPTIND-1))
34+
35+
# Concatenate all input files and preprocess them with Jinja2, then
36+
# feed the result to pandoc on stdin
37+
tempmd=`mktemp --suffix .md`
38+
cat $* > $tempmd
39+
jinja2 $tempmd mkdocs.yml \
40+
| pandoc \
41+
--data-dir=pandoc \
42+
--no-highlight \
43+
-f gfm \
44+
-t ${format} \
45+
-o ${output} \
46+
-i -
47+
48+
# Clean up
49+
rm $tempmd

tox.ini

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tox]
2-
envlist = yamllint,build
2+
envlist = yamllint,bashate,build
33

44
[testenv]
55
envdir = {toxworkdir}/mkdocs
@@ -33,6 +33,27 @@ commands =
3333
commands =
3434
mkdocs build --strict
3535

36+
[testenv:bashate]
37+
envdir = {toxworkdir}/bashate
38+
deps =
39+
bashate
40+
commands =
41+
bashate {toxinidir}/scripts/from-markdown.sh
42+
43+
[testenv:to-odt]
44+
envdir = {toxworkdir}/pandoc
45+
deps =
46+
jinja2-cli[yaml]
47+
commands =
48+
{toxinidir}/scripts/from-markdown.sh -t odt {posargs}
49+
50+
[testenv:to-docx]
51+
envdir = {toxworkdir}/pandoc
52+
deps =
53+
jinja2-cli[yaml]
54+
commands =
55+
{toxinidir}/scripts/from-markdown.sh -t docx {posargs}
56+
3657
[testenv:gitlint]
3758
envdir = {toxworkdir}/gitlint
3859
deps = gitlint

0 commit comments

Comments
 (0)