|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +set +o errexit +o nounset |
| 4 | + |
| 5 | +get_asset_version() { |
| 6 | + [ $# -ne 1 ] && return 1 |
| 7 | + asset_version="$(grep -o -m 1 -P '(?<=assets_version = ")[^"]*' "$1/book.toml")" |
| 8 | +} |
| 9 | + |
| 10 | +populate_cache() { |
| 11 | + if ! type mdbook-admonish > /dev/null 2>&1; then |
| 12 | + echo "\ |
| 13 | +mdbook-admonish must be installed! See |
| 14 | +https://github.com/tommilligan/mdbook-admonish" 1>&2 |
| 15 | + exit 1 |
| 16 | + fi |
| 17 | + |
| 18 | + [ $# -ne 1 ] && return 1 |
| 19 | + |
| 20 | + dir="$1" |
| 21 | + |
| 22 | + mkdir -p "$dir" |
| 23 | + |
| 24 | + # Create a fake mdbook config file so that mdbook-admonish can install its files |
| 25 | + # to it. |
| 26 | + touch "${dir}/book.toml" |
| 27 | + |
| 28 | + mdbook-admonish install "$dir" |
| 29 | + |
| 30 | + if ! [ -f "${dir}/mdbook-admonish.css" ]; then |
| 31 | + echo "Couldn't generate ${dir}/mdbook-admonish.css!" 1>&2 |
| 32 | + exit 1 |
| 33 | + fi |
| 34 | +} |
| 35 | + |
| 36 | +exec_mdbook() { |
| 37 | + [ $# -lt 2 ] && return 1 |
| 38 | + asset_version="$1" |
| 39 | + finaldir="$2" |
| 40 | + shift 2 |
| 41 | + exec env MDBOOK_PREPROCESSOR__ADMONISH="{\"assets_version\": \"$asset_version\"}"\ |
| 42 | + MDBOOK_OUTPUT__HTML__ADDITIONAL_CSS="[\"${finaldir}/mdbook-admonish.css\"]"\ |
| 43 | + mdbook "$@" |
| 44 | +} |
| 45 | + |
| 46 | +usage="\ |
| 47 | +A wrapper for mdBook which handles mdbook-admonish. |
| 48 | +
|
| 49 | +$0 needs to generate some files. It puts them to <the directory the script |
| 50 | +resides in>/cache/ by default. |
| 51 | +
|
| 52 | +You can change the base directory with -b <directory> (directory the script |
| 53 | +resides in by default) and the cache subdirectory (cache/ by default) with -c |
| 54 | +<directory relative to base directory>. The files are put into <base |
| 55 | +directory>/<subdirectory> |
| 56 | +
|
| 57 | +Directories are created if they do not exist. |
| 58 | +
|
| 59 | +Usage: |
| 60 | + $0 [-b <directory>] [-c <directory>] mdbook commandline... |
| 61 | + $0 -h |
| 62 | +
|
| 63 | +Options: |
| 64 | + -h Show this help message. |
| 65 | + -b=<directory> Set base directory. |
| 66 | + -c=<directory> Set cache directory. |
| 67 | +
|
| 68 | +Example usage: |
| 69 | + Build the book (calls \`mdbook build\` internally): |
| 70 | + $0 build |
| 71 | + Serve the book (calls \`mdbook serve\` internally): |
| 72 | + $0 serve |
| 73 | +" |
| 74 | + |
| 75 | +# I assume $0 is set and it's valid. |
| 76 | +basedir="$(dirname $0)" |
| 77 | +cachedir="cache/" |
| 78 | + |
| 79 | +while getopts hb:c: f; do |
| 80 | + case $f in |
| 81 | + h) |
| 82 | + printf %s "$usage" |
| 83 | + exit 0 |
| 84 | + ;; |
| 85 | + b) |
| 86 | + basedir="$OPTARG" |
| 87 | + ;; |
| 88 | + c) |
| 89 | + cachedir="$OPTARG" |
| 90 | + ;; |
| 91 | + *) |
| 92 | + printf %s "$usage" |
| 93 | + exit 1 |
| 94 | + ;; |
| 95 | + esac |
| 96 | +done |
| 97 | + |
| 98 | +shift $((OPTIND - 1)) |
| 99 | + |
| 100 | +finaldir="${basedir}/${cachedir}" |
| 101 | + |
| 102 | +if [ -f "${finaldir}/mdbook-admonish.css" ]; then |
| 103 | + get_asset_version "$finaldir" |
| 104 | + exec_mdbook "$asset_version" "$finaldir" "$@" |
| 105 | +else |
| 106 | + populate_cache "$finaldir" |
| 107 | + get_asset_version "$finaldir" |
| 108 | + exec_mdbook "$asset_version" "$finaldir" "$@" |
| 109 | +fi |
0 commit comments