From 84d1e273ec16050bc7aa48f6b45d0a8a2acb3f0f Mon Sep 17 00:00:00 2001 From: Nishant Srivastava Date: Sun, 26 Jul 2020 00:23:59 +0200 Subject: [PATCH] - Add a bash script to split the existing Readme.md into chapters+summary files which mdBook can consume. - Enables generating a book version - Fixes issue #11 --- book.toml | 6 +++++ createBookFromReadme.sh | 54 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 book.toml create mode 100755 createBookFromReadme.sh diff --git a/book.toml b/book.toml new file mode 100644 index 0000000..914920d --- /dev/null +++ b/book.toml @@ -0,0 +1,6 @@ +[book] +authors = ["David MacLeod"] +language = "en" +multilingual = false +src = "src" +title = "Easy Rust" \ No newline at end of file diff --git a/createBookFromReadme.sh b/createBookFromReadme.sh new file mode 100755 index 0000000..838e411 --- /dev/null +++ b/createBookFromReadme.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash + +# Execute this script to generate a mdBook version from the single Readme.md file present in this repository. +# Usage: ./createBookFromReadme.sh + +# -------------------- Utility Methods -------------------- +# Cleanup the src directory before starting +function cleanupBeforeStarting(){ + rm -rf ./src + mkdir src +} + +# Splits the Readme.md file based on the header in markdown and creates chapters +# Note: +# Get gcsplit via homebrew on mac: brew install coreutils +function splitIntoChapters(){ + gcsplit --prefix='Chapter_' --suffix-format='%d.md' --elide-empty-files README.md '/^# /' '{*}' -q +} + +# Moves generated chapters into src directory +function moveChaptersToSrcDir(){ + for f in Chapter_*.md; do + mv $f src/$f + done +} + +# Creates the summary from the generated chapters +function createSummary(){ + cd ./src + touch SUMMARY.md + echo '# Summary' > SUMMARY.md + echo "" >> SUMMARY.md + for f in $(ls -tr | grep Chapter_); do + # Get the first line of the file + local firstLine=$(sed -n '1p' $f) + local cleanTitle=$(echo $firstLine | cut -c 3-) + echo "- [$cleanTitle](./$f)" >> SUMMARY.md; + done + cd .. +} + +# Builds the mdBook version from src directory and starts serving locally. +# Note: +# Install mdBook as per instructions in their repo https://github.com/rust-lang/mdBook +function buildAndServeBookLocally(){ + mdBook build && mdBook serve +} + +# -------------------- Steps to create the mdBook version -------------------- +cleanupBeforeStarting +splitIntoChapters +moveChaptersToSrcDir +createSummary +buildAndServeBookLocally