From 91e7cf44510ecfea2ea9dd60d105f07309806a08 Mon Sep 17 00:00:00 2001 From: Hyperling Date: Thu, 23 Oct 2025 14:51:27 -0700 Subject: [PATCH] Create the eBook based on the individual files. --- convert.sh | 140 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 124 insertions(+), 16 deletions(-) diff --git a/convert.sh b/convert.sh index 8f528e2..7e8acd8 100755 --- a/convert.sh +++ b/convert.sh @@ -2,33 +2,141 @@ # 2024-03-06 Hyperling # PanDoc Usage: https://pandoc.org/MANUAL.html#general-writer-options +## Setup ## + DIR="$(dirname -- "${BASH_SOURCE[0]}")" -cd $DIR +DIR="`pwd`" +echo "$DIR" date -ebook="TechnologyAlternatives.pdf" +## Global Variables ## -if [[ -e $ebook ]]; then - echo -n "Old: " - du -h $ebook - rm -v $ebook +ebook_prefix="TechnologyAlternatives" +work_dir="/tmp/$ebook_prefix" +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" + echo -e "\n" >> "$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=" " + 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 + rm -v "$DIR"/*.pdf fi -cp ebook.md /tmp/ -sed -i "s/{REVISION_DATE}/`date +'%Y-%m-%d'`/g" /tmp/ebook.md +log "Clean working directory '$work_dir'" +if [[ -e "$work_dir" ]]; then + rm -rv "$work_dir" +fi -pandoc /tmp/ebook.md \ - -s -N --toc --toc-depth=4 \ - -o $ebook -status=$? +## Main ## -pdfbook2 -n $ebook +mkdir -pv "$work_dir" +cd "$work_dir" +cp -v "$DIR/$header" ./ +sed -i "s/{REVISION_DATE}/`date +'%Y-%m-%d'`/g" "$header" -echo "Completed with status '$status'." +# Primary, All In One (AIO) +name="$ebook_prefix" +combine "$name" "$header" `ls "$DIR"/content*.md` +convert "$name.md" "" "$DIR" -echo -n "New: " -du -h $ebook +# Secondary, Mobile + + +# Secondary, Desktop + + +# Secondary, Server + + +# Secondary, Other + + + +## Finish! ## + +cd "$DIR" + +echo "New: " +du -had1 *.pdf date