Files
ebook-technology-alternatives/convert.sh

151 lines
2.9 KiB
Bash
Executable File

#!/bin/bash
# 2024-03-06 Hyperling
# PanDoc Usage: https://pandoc.org/MANUAL.html#general-writer-options
## Setup ##
DIR="$(dirname -- "${BASH_SOURCE[0]}")"
DIR="`pwd`"
echo "$DIR"
date
## Global Variables ##
ebook_prefix="TechnologyAlternatives"
work_dir="$DIR/build"
header="header.md"
## Functions ##
function log {
echo -e "*** $1 ***"
}
# Start Log Section
function logs {
echo -e "\n***** $1 *****"
}
# Finish Log Section
function logf {
echo -e "***** $1 *****\n"
}
# Copy a file into another file.
function insert {
input="$1"
ebook="$2"
log "Adding '$input' to '$ebook'."
cat "$input" >> "$ebook"
}
# Compile all the snippets into one Markdown file.
function combine {
log "Combining documents based on parameters '`echo $@`'."
ebook="$1.md"
shift
for addition in "$@"; do
insert "$addition" "$ebook"
done
}
# Create the PDF and booklets them move them back to the project directory.
function convert {
source="$1"
version="$2"
out_dir="$3"
ebook="${source//.md/.pdf}"
logs "Creating '$out_dir/$ebook' from '$source'."
book="${ebook//.pdf/-book.pdf}"
booklet="${ebook//.pdf/-Booklet.pdf}"
rotated="${ebook//.pdf/-Rotated.pdf}"
log "Set VERSION to '$version'."
if [[ -z "$version" ]]; then
log "Version is null, removing the extra space."
space=" "
else
space=""
fi
sed -i "s/$space{VERSION}/$version/g" "$source"
log "Compiling '$source' to '$ebook'."
pandoc "$source" \
-s -N --toc --toc-depth=4 \
-o "$ebook"
status=$?
log "Creating booklet of '$ebook'."
pdfbook2 -n "$ebook"
mv -v "$book" "$booklet"
log "Creating rotated booklet of '$ebook'."
pdfbook2 -ns "$ebook"
mv -v "$book" "$rotated"
log "Moving '$ebook' and its booklets to '$out_dir'."
mv -v *.pdf "$out_dir"/
logf "Complete!"
}
## Cleanup ##
log "Clean PDFs"
if [[ -e "$DIR/$ebook_prefix.pdf" ]]; then
echo "Old: "
du -had1 "$DIR"/*.pdf | sort -h
rm -v "$DIR"/*.pdf
fi
log "Clean working directory '$work_dir'"
if [[ -e "$work_dir" ]]; then
rm -rv "$work_dir"
fi
## Main ##
mkdir -pv "$work_dir"
cd "$work_dir"
cp -v "$DIR/$header" ./
sed -i "s/{REVISION_DATE}/`date +'%Y-%m-%d'`/g" "$header"
# Primary, All In One (AIO)
name="$ebook_prefix""_AllPlatforms"
combine "$name" "$header" `ls "$DIR"/content*.md`
convert "$name.md" "" "$DIR"
# Secondary, Mobile
name="$ebook_prefix""_MobileOnly"
combine "$name" "$header" "$DIR/content00"* "$DIR/content10"* \
"$DIR/content70"* "$DIR/content90"*
convert "$name.md" "(Mobile Only)" "$DIR"
# Secondary, Desktop
name="$ebook_prefix""_DesktopOnly"
combine "$name" "$header" "$DIR/content00"* "$DIR/content20"* \
"$DIR/content70"* "$DIR/content90"*
convert "$name.md" "(Desktop Only)" "$DIR"
# Secondary, Server
name="$ebook_prefix""_ServerOnly"
combine "$name" "$header" "$DIR/content00"* "$DIR/content30"* \
"$DIR/content70"* "$DIR/content90"*
convert "$name.md" "(Server Only)" "$DIR"
## Finish! ##
cd "$DIR"
echo "New: "
du -had1 *.pdf | sort -h
date
exit $status