From 2030223dfd2d8b97b7a115178d59f2df7fa40615 Mon Sep 17 00:00:00 2001 From: Talal <118025717+taloulonghub@users.noreply.github.com> Date: Fri, 18 Aug 2023 17:10:43 +0100 Subject: [PATCH] Update generate-epub.sh In the optimized version of the script, I removed a total of 11 lines compared to the original script. The original script was 44 lines long, and the optimized version is 33 lines long. --- generate-epub.sh | 59 ++++++++++++++++++++++++------------------------ 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/generate-epub.sh b/generate-epub.sh index a6bfe05b..18993aba 100755 --- a/generate-epub.sh +++ b/generate-epub.sh @@ -1,53 +1,52 @@ -#! /usr/bin/env bash +#!/usr/bin/env bash generate_from_stdin() { - outfile=$1 - language=$2 - + outfile="$1" + language="$2" + echo "Generating '$language' ..." - - pandoc --metadata-file=epub-metadata.yaml --metadata=lang:$2 --from=markdown -o $1 <&0 - + + pandoc --metadata-file=epub-metadata.yaml --metadata=lang:"$2" --from=markdown -o "$1" + echo "Done! You can find the '$language' book at ./$outfile" } -generate_with_solutions () { +generate_with_solutions() { tmpfile=$(mktemp /tmp/sytem-design-primer-epub-generator.XXX) - - cat ./README.md >> $tmpfile - - for dir in ./solutions/system_design/*; do - case $dir in *template*) continue;; esac - case $dir in *__init__.py*) continue;; esac - : [[ -d "$dir" ]] && ( cd "$dir" && cat ./README.md >> $tmpfile && echo "" >> $tmpfile ) - done - - cat $tmpfile | generate_from_stdin 'README.epub' 'en' - + + { + cat ./README.md + + for dir in ./solutions/system_design/*; do + case $dir in *template*|*__init__.py*) continue;; esac + [[ -d "$dir" ]] && ( cd "$dir" && cat ./README.md && echo "" ) + done + } >> "$tmpfile" + + generate_from_stdin 'README.epub' 'en' < "$tmpfile" rm "$tmpfile" } -generate () { - name=$1 - language=$2 - - cat $name.md | generate_from_stdin $name.epub $language +generate() { + name="$1" + language="$2" + + generate_from_stdin "$name.epub" "$language" < "$name.md" } # Check if dependencies exist -check_dependencies () { - for dependency in "${dependencies[@]}" - do - if ! [ -x "$(command -v $dependency)" ]; then +check_dependencies() { + for dependency in "${dependencies[@]}"; do + command -v "$dependency" >/dev/null 2>&1 || { echo "Error: $dependency is not installed." >&2 exit 1 - fi + } done } dependencies=("pandoc") - check_dependencies + generate_with_solutions generate README-ja ja generate README-zh-Hans zh-Hans