Last active
November 15, 2024 10:07
-
-
Save rbf/6064734 to your computer and use it in GitHub Desktop.
Script to recursively generate PDF files from markdown files in the current directory and its subfolders using "pandoc".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# The MIT License (MIT) | |
# | |
# Copyright (c) 2013 https://gist.github.com/rbf | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy of | |
# this software and associated documentation files (the "Software"), to deal in | |
# the Software without restriction, including without limitation the rights to | |
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | |
# the Software, and to permit persons to whom the Software is furnished to do so, | |
# subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
printhelp(){ | |
cat << EOF | |
Usage: | |
$ ./generate-pdf.sh [path-pattern [target-path]] | |
$ ./generate-pdf.sh --help | |
Examples: | |
$ ./generate-pdf.sh --help Prints this help and exits | |
$ ./generate-pdf.sh All .md and .markdown files in the current directory and its | |
subdirectories are converted in pdf in the ./target directory, | |
maintaining its original sub-folder structure | |
$ ./generate-pdf.sh api All .md and .markdown files in the current directory and its | |
subdirectories containing "api" (case sensitive) in the file name or | |
the path are converted in pdf in the ./target directory, maintaining | |
its original sub-folder structure | |
$ ./generate-pdf.sh ./doc/*api All .md and .markdown files in the "doc/" subdirectory and its | |
subdirectories containing "api" (case sensitive) in the file name or | |
the path are converted in pdf in the ./target directory, maintaining | |
its original sub-folder structure | |
$ ./generate-pdf.sh api pdfs All .md and .markdown files in the current directory and its | |
subdirectories containing "api" (case sensitive) in the file name or | |
the path are converted in pdf in the ./pdfs directory, maintaining | |
its original sub-folder structure | |
$ ./generate-pdf.sh api . All .md and .markdown files in the current directory and its | |
subdirectories containing "api" (case sensitive) in the file name or | |
the path are converted in pdf in the current directory, maintaining | |
its original sub-folder structure, i.e. next to their .md counterparts | |
$ ./generate-pdf.sh . . All .md and .markdown files in the current directory and its | |
subdirectories are converted in pdf in the current directory, | |
maintaining its original sub-folder structure, i.e. next to their .md | |
counterparts | |
EOF | |
exit 0 | |
} | |
if [ "${1}" == "--help" ] | |
then | |
printhelp | |
elif [ "${1:0:2}" == "--" ] | |
then | |
echo "Invalid option: ${1}" | |
echo | |
printhelp | |
fi | |
GP_BASE_DIR="$(pwd)" | |
GP_TARGET_DIR="${2:-target}" | |
for file in $(find . -type f -path "*${1}*" \( -iname "*.md" -or -iname "*.markdown" \) -print) | |
do | |
echo -n "[INFO] Processing file: $file " | |
GP_CURRENT_DIR=$(dirname "$file") | |
GP_CURRENT_FILENAME=$(basename "$file") | |
GP_TARGET_CURRENT_DIR="${GP_TARGET_DIR}/${GP_CURRENT_DIR}" | |
GP_TARGET_CURRENT_FILEPATH="${GP_TARGET_CURRENT_DIR}/$(basename "$file" ".md").pdf" | |
# Remove unnecessary (but not wrong) "/./" from path for enhanced prompt in stdout | |
GP_TARGET_CURRENT_FILEPATH="${GP_TARGET_CURRENT_FILEPATH/\/.\///}" | |
GP_TARGET_CURRENT_FILEPATH="./${GP_TARGET_CURRENT_FILEPATH#./}" | |
mkdir -p ${GP_TARGET_CURRENT_DIR} | |
# Since pandoc look for images relatives to where it is called (https://github.com/jgm/pandoc/issues/917) | |
# we have to cd into the directory of the given file before pandoc'ing it, | |
# and we have to pass the full path in the -o parameter. | |
cd "${GP_CURRENT_DIR}" | |
GP_PANDOC_OUTPUT=$(pandoc --toc --number-sections "${GP_CURRENT_FILENAME}" -o ${GP_BASE_DIR}/${GP_TARGET_CURRENT_FILEPATH#./} 2>&1) # to capture pandoc stderr output | |
if [ "$?" == 0 ] | |
then | |
echo "--> ${GP_TARGET_CURRENT_FILEPATH}" | |
else | |
echo -e "\r[ERROR] Processing file: $file - Detail below" | |
GP_ERRORS+=("$(echo -e "[ERROR] ${file} - Failed generation of PDF\n[ERROR] Expected target file: ${GP_TARGET_CURRENT_FILEPATH}\n[ERROR] ${GP_PANDOC_OUTPUT}\n\n\n")") | |
fi | |
cd "${GP_BASE_DIR}" | |
done | |
if [ ${#GP_ERRORS[@]} -eq 0 ] | |
then | |
exit 0 | |
fi | |
echo | |
echo "[ERROR] ====================================================================" | |
echo "[ERROR] pandoc failed to generate PDF for ${#GP_ERRORS[@]} file(s)" | |
echo "[ERROR] ====================================================================" | |
for i in "${GP_ERRORS[@]}" | |
do | |
echo | |
echo "${i}" | |
echo | |
done | |
exit 1 |
I like the idea but...
/home/howie/bin/process-md: line 188: syntax error near unexpected token `newline'
/home/howie/bin/process-md: line 188: ` done'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thats's sounds awesome... Do you know how to make a similar thing on Windows?