diff --git a/.github/workflows/github-pages.yml b/.github/workflows/github-pages.yml new file mode 100644 index 0000000..3117346 --- /dev/null +++ b/.github/workflows/github-pages.yml @@ -0,0 +1,30 @@ +name: github pages + +on: + push: + branches: + - master + +jobs: + deploy: + runs-on: ubuntu-18.04 + steps: + - uses: actions/checkout@v2 + - run: sudo apt install linuxbrew-wrapper + - run: brew install coreutils + #- run: cargo install mdbook + - run: chmod 777 -R ./ + - run: make github_pages + + - name: Setup mdBook + uses: peaceiris/actions-mdbook@v1 + with: + mdbook-version: 'latest' + + - run: mdbook build + + - name: Deploy + uses: peaceiris/actions-gh-pages@v3 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: ./book \ No newline at end of file diff --git a/Makefile b/Makefile index 6cbd250..daaba77 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,9 @@ help: ## Print help for each target book: ## Generate an mdBook version @./createBookFromReadme.sh +github_pages: ## Generate an mdBook version + @./createGithubPagesFromReadme.sh + snippets: clean ## Create snippets @type md2src >/dev/null 2>&1 || (echo "Run 'cargo install md2src' first." >&2 ; exit 1) @mkdir -p $(SNIPPETS) diff --git a/README.md b/README.md index 8da5712..9876690 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,9 @@ -Update 28 November 2020: [Now also available in simplified Chinese](https://github.com/kumakichi/easy_rust_chs) thanks to [kumakichi](https://github.com/kumakichi)! +## Update +![example workflow name](https://github.com/FongYoong/easy_rust/workflows/github%20pages/badge.svg) + +22 December: mdBook can be found [here](https://fongyoong.github.io/easy_rust/). + +28 November 2020: [Now also available in simplified Chinese](https://github.com/kumakichi/easy_rust_chs) thanks to [kumakichi](https://github.com/kumakichi)! ## Introduction diff --git a/createGithubPagesFromReadme.sh b/createGithubPagesFromReadme.sh new file mode 100644 index 0000000..481550d --- /dev/null +++ b/createGithubPagesFromReadme.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash + +# A modified version of createBookFromReadme.sh for Github Pages +# Usage: ./createGithunPagesFromReadme.sh + +# -------------------- Utility Methods -------------------- +# Check for binaries +function checkEnvironment(){ + type gcsplit >/dev/null 2>&1 || { echo "Install 'gcsplit' first (e.g. via 'brew install coreutils')." >&2 && exit 1 ; } +} + +# 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_ | sort -V); 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 .. +} + +# -------------------- Steps to generate separate Markdown chapters -------------------- +checkEnvironment +cleanupBeforeStarting +splitIntoChapters +moveChaptersToSrcDir +createSummary \ No newline at end of file