From d316ed10ae4ddc8dc175c79a298e358802e90c8b Mon Sep 17 00:00:00 2001 From: Ahmed AbouZaid Date: Thu, 31 Jan 2019 08:46:20 +0100 Subject: [PATCH 1/4] tidy up and make it easy to change book name --- bootstrap.sh | 47 ++++++++++++++++++++++++++++++----------------- generate.rb | 4 ++-- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/bootstrap.sh b/bootstrap.sh index 05e57b3..b29a07f 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -2,7 +2,13 @@ set -euo pipefail IFS=$'\n\t' -TOC_URL="https://landing.google.com/sre/sre-book/toc/index.html" +# Vars. +export BOOK_NAME="sre-book" +export BOOK_NAME_FULL="Site Reliability Engineering" +BOOK_FILE="google-${BOOK_NAME}" +TOC_URL="https://landing.google.com/sre/${BOOK_NAME}/toc/index.html" +IMGS_DOMAIN="lh3.googleusercontent.com" + # Make sure that links are relative \ # # Remove the /sre/ directories # Save stuff in html/ directory @@ -11,33 +17,40 @@ TOC_URL="https://landing.google.com/sre/sre-book/toc/index.html" # Images are hosted elsewhere, download them as well. # We need to go up a level from /toc/ where we start wget \ - --convert-links \ + --convert-links \ --directory-prefix=html \ - --page-requisites \ - --adjust-extension \ - --span-hosts \ - --trust-server-names \ - --backup-converted \ - --mirror \ - --no-verbose \ - --recursive \ - --domains=lh3.googleusercontent.com,landing.google.com https://landing.google.com/sre/sre-book/toc/index.html + --page-requisites \ + --adjust-extension \ + --span-hosts \ + --trust-server-names \ + --backup-converted \ + --mirror \ + --no-verbose \ + --recursive \ + --domains=${IMGS_DOMAIN},landing.google.com ${TOC_URL} +# MODE=${1:-} if [ "$MODE" != "docker" ];then bundle install fi +# ruby generate.rb -pushd html/landing.google.com/sre/sre-book/toc -pandoc -f html -t epub -o ../../../../../google-sre.epub --epub-metadata=../../../../../metadata.xml --epub-cover-image=../../../../../cover.jpg complete.html +# +pushd html/landing.google.com/sre/${BOOK_NAME}/toc +pandoc -f html -t epub -o ../../../../../${BOOK_FILE}.epub --epub-metadata=../../../../../metadata.xml --epub-cover-image=../../../../../cover.jpg complete.html popd -ebook-convert google-sre.epub google-sre.mobi -ebook-convert google-sre.epub google-sre.pdf +# +for EXTENSION in mobi pdf; do + ebook-convert ${BOOK_FILE}.epub ${BOOK_FILE}.${EXTENSION} +done + +# if [ "$1"=="docker" ]; then - chown -v $(id -u):$(id -g) google-sre.* - mv -f google-sre.* /output + chown -v $(id -u):$(id -g) ${BOOK_FILE}.* + mv -f ${BOOK_FILE}.* /output fi diff --git a/generate.rb b/generate.rb index d98ea63..aa335a9 100644 --- a/generate.rb +++ b/generate.rb @@ -4,7 +4,7 @@ require 'fileutils' # First we get the list of all the book sections: -Dir.chdir("html/landing.google.com/sre/sre-book/toc") +Dir.chdir("html/landing.google.com/sre/%s/toc" % ENV['BOOK_NAME']) chapter_links = Nokogiri::HTML(open("index.html")) .css('#drop-down a') .map {|l| l.attribute('href').value} @@ -13,7 +13,7 @@ html = < - Site Reliability Engineering + #{ENV['BOOK_NAME_FULL']} From 15ba3a80c52cca9fcc8af2a09a0c76643ce94825 Mon Sep 17 00:00:00 2001 From: Ahmed AbouZaid Date: Thu, 31 Jan 2019 08:47:14 +0100 Subject: [PATCH 2/4] set images extension to fix missing images in mobi format --- Dockerfile | 1 + bootstrap.sh | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/Dockerfile b/Dockerfile index 6085fe3..f0147c7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,6 +17,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ ruby-dev \ wget \ zlib1g-dev \ + file \ && gem install bundler --no-ri --no-rdoc \ && bundle install \ && apt-get clean \ diff --git a/bootstrap.sh b/bootstrap.sh index b29a07f..fa687d6 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -36,6 +36,22 @@ if [ "$MODE" != "docker" ];then bundle install fi +# Add extension to files. +# That because `pandoc` cannot generate the right `mime type` without the extension. +# https://github.com/captn3m0/google-sre-ebook/issues/19 +IMGS_FILES="$(ls /src/html/${IMGS_DOMAIN}/*)" +for FILE_NAME_FULL in ${IMGS_FILES}; do + + # Get file vars. + FILE_NAME_BASE="$(basename ${FILE_NAME_FULL})" + FILE_TYPE=$(file -b -- "${FILE_NAME_FULL}" | cut -f1 -d " ") + + # Rename and replace file. + mv "${FILE_NAME_FULL}" "${FILE_NAME_FULL}.${FILE_TYPE,,}" && + grep -rl "${FILE_NAME_BASE}" ./html | xargs sed -i "s/${FILE_NAME_BASE}/${FILE_NAME_BASE}.${FILE_TYPE,,}/g" + +done + # ruby generate.rb From ae44b14dadc2403a088cea3807fb5b92fe7eca79 Mon Sep 17 00:00:00 2001 From: Nemo Date: Thu, 31 Jan 2019 15:25:03 +0530 Subject: [PATCH 3/4] Adds support for multiple covers/metadata files --- bootstrap.sh | 4 ++-- cover.jpg => sre-book.jpg | Bin metadata.xml => sre-book.xml | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename cover.jpg => sre-book.jpg (100%) rename metadata.xml => sre-book.xml (100%) diff --git a/bootstrap.sh b/bootstrap.sh index f93f697..c94246e 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -56,8 +56,8 @@ ruby generate.rb pushd html/landing.google.com/sre/${BOOK_FILE}/toc pandoc --from=html --to=epub \ --output=../../../../../${BOOK_FILE}.epub \ - --epub-metadata=../../../../../metadata.xml \ - --epub-cover-image=../../../../../cover.jpg \ + --epub-metadata=../../../../../${BOOK_NAME}.xml \ + --epub-cover-image=../../../../../${BOOK_NAME}.jpg \ complete.html popd diff --git a/cover.jpg b/sre-book.jpg similarity index 100% rename from cover.jpg rename to sre-book.jpg diff --git a/metadata.xml b/sre-book.xml similarity index 100% rename from metadata.xml rename to sre-book.xml From c08e0b6fde17d041c9bfe8ce06b0071ba849c6cc Mon Sep 17 00:00:00 2001 From: Nemo Date: Thu, 31 Jan 2019 15:31:32 +0530 Subject: [PATCH 4/4] Switch to relative paths so it works without docker as well --- README.md | 2 +- bootstrap.sh | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index edf609d..dbd93dd 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ Requirements: - Ruby - `gem install bundler` -- `gem install nokogiri` +- `bundle install` - `brew install pandoc` - `brew cask install calibre` - `brew install wget` diff --git a/bootstrap.sh b/bootstrap.sh index c94246e..2aa6b78 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -39,7 +39,7 @@ fi # Add extension to files. # That because `pandoc` cannot generate the right `mime type` without the extension. # https://github.com/captn3m0/google-sre-ebook/issues/19 -IMGS_FILES="$(ls /src/html/${IMGS_DOMAIN}/*)" +IMGS_FILES="$(ls html/${IMGS_DOMAIN}/*)" for FILE_NAME_FULL in ${IMGS_FILES}; do # Get file vars. @@ -52,8 +52,8 @@ for FILE_NAME_FULL in ${IMGS_FILES}; do done -ruby generate.rb -pushd html/landing.google.com/sre/${BOOK_FILE}/toc +bundle exec ruby generate.rb +pushd html/landing.google.com/sre/${BOOK_NAME}/toc pandoc --from=html --to=epub \ --output=../../../../../${BOOK_FILE}.epub \ --epub-metadata=../../../../../${BOOK_NAME}.xml \