Commit 96e659bd authored by Lunar's avatar Lunar

Add support for splitting PDF documents

parent 04db44e4
......@@ -31,6 +31,7 @@ amount in each part.
To learn more about these kind of workshops, see the [article
written by the collective La
Trouvaille](https://la-trouvaille.org/arpentage/) (in French).
*If you are looking for facilitators, please feel free to
contact [La Dérivation](https://dérivation.fr/).*
......@@ -38,12 +39,14 @@ Dependencies
------------
* [Pandoc][] >= 2.0
* `pdfinfo` from [Poppler][]
On a Debian-based system, you can usually install Pandoc by issuing:
sudo apt install pandoc
sudo apt install pandoc poppler-utils
[Pandoc]: https://pandoc.org/
[Poppler]: https://poppler.freedesktop.org/
Usage
-----
......@@ -60,6 +63,10 @@ are in the same format.
The output path is used as a template: `output.html` will become
`output-part-1.html`, `output-part-2.html`, …
`arpentage` can also handle PDF documents. In this case it will
divide the original document into multiple PDF with the same
number of pages in every part.
The command line format is the following:
arpentage -p|--parts <count> [options] <input>
......@@ -107,6 +114,10 @@ The process is as follow:
2. Then, for each part, we will run Pandoc and the filter so it can
remove all content that should not end up in this particular part.
When working on PDF files, it will use `pdfinfo` to determine the
title and the number of pages. Pandoc will then be used to drive the
`pdfpages` LaTeX package in order to produce the output PDFs.
Acknowledgements
----------------
......
......@@ -144,6 +144,39 @@ make_output_template() {
echo "${prefix:+${prefix}-}part-%0${number_char}d${extension:+.$extension}"
}
split_as_pdf() {
local pdfinfo title pages pages_per_part part output_path end_of_range
pdfinfo="$(pdfinfo "$SOURCE")"
title="$(echo "$pdfinfo" | sed -n -e 's/^Title: *//p')"
pages="$(echo "$pdfinfo" | sed -n -e 's/^Pages: *//p')"
info "PDF is “$title” with $pages pages."
if ! [ "$pages" -gt "$PARTS" ]; then
error_exit "Not enough pages ($pages) to have at least one in each part."
fi
pages_per_part=$(($pages / $PARTS))
info "This means $pages_per_part pages per part."
for part in $(seq 1 $PARTS); do
output_path=$(printf "$OUTPUT_TEMPLATE" "$part")
info "Writing part $part/$PARTS to $output_path"
if [ "$part" -eq "$PARTS" ]; then
end_of_range="" # means all the way to the end of the file
else
end_of_range="$(($part * pages_per_part))"
fi
pandoc -f markdown+raw_tex -t latex -o "$output_path" <<EOF
---
title: "$title $(printf "$APPEND_TO_TITLE" $part $PARTS)"
header-includes: |
\usepackage{pdfpages}
\renewcommand{\maketitle}{}
---
\includepdf[pages=$((($part - 1) * pages_per_part + 1))-$end_of_range]{$SOURCE}
EOF
done
}
call_pandoc() {
local words_per_part current_part
......@@ -189,14 +222,26 @@ create_part() {
--self-contained
}
split_with_pandoc() {
WORDS_PER_PART=$(get_words_per_part)
for part in $(seq 1 "$PARTS"); do
create_part "$part"
done
}
VERBOSE=1
parse_opts "$@"
LUA_FILTER_PATH=$(realpath --canonicalize-missing "$0"/../arpentage-filter.lua)
OUTPUT_TEMPLATE=$(make_output_template)
RESSOURCE_PATH=$(realpath --canonicalize-missing "$SOURCE"/..)
WORDS_PER_PART=$(get_words_per_part)
for part in $(seq 1 "$PARTS"); do
create_part "$part"
done
# Look at extensions of both source and output
case "${SOURCE##*.}_${OUTPUT_TEMPLATE##*.}" in
[pP][dD][fF]_[pP][dD][fF])
split_as_pdf
;;
*)
split_with_pandoc
;;
esac
info "Done! 🎉"
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment