OILS / build / dev-guide.sh View on Github | oils.pub

93 lines, 40 significant
1#!/usr/bin/env bash
2#
3# Usage:
4# build/dev-guide.sh build-dev-guide
5#
6# This will clone the oils-for-unix/oils wiki, and build all of the markdown
7# files using doc tools. The docs are saved to _release/VERSION/doc/wiki.
8#
9# This can be used for the dev guide which is located at
10# _release/VERSION/doc/wiki/Dev-Guide.html.
11
12set -e
13
14readonly HTML_BASE_DIR=_release/VERSION
15readonly WIKI_DIR=_tmp/wiki
16
17has-wiki() {
18 test -d "$WIKI_DIR"
19}
20
21clone-wiki() {
22 if has-wiki; then
23 echo "Wiki already cloned to $WIKI_DIR, pulling latest"
24
25 pushd "$WIKI_DIR"
26 git pull
27 popd
28 return
29 fi
30
31 echo "Cloning wiki to $WIKI_DIR"
32 mkdir -p "$WIKI_DIR"
33 git clone https://github.com/oils-for-unix/oils.wiki.git "$WIKI_DIR"
34}
35
36pre-render-wikilinks() {
37 ## GitHub wikis have a unique [[link syntax]] which references topic within
38 ## the wiki.
39 ##
40 ## This function converts that syntax to the traditional
41 ## [link syntax](./link-syntax.html) which will render correctly once fed to
42 ## doctools.
43 local script=$(cat <<'EOF'
44import sys
45import re
46
47def slugify(text: str) -> str:
48 """
49 The links are given in "human-form" but we need to turn then into links or
50 "slugs" which correspond to the rendered file name.
51
52 Note: Some titles have commas in them. These are not present in the slug.
53 """
54 return text.replace(" ", "-").replace(",", "")
55
56link_pattern = re.compile(r"\[\[(.*?)\]\]")
57
58def replacer(match):
59 text = match.group(1).strip()
60 return f"[{text}](./{slugify(text)}.html)"
61
62input_text = sys.stdin.read()
63output_text = link_pattern.sub(replacer, input_text)
64sys.stdout.write(output_text)
65EOF
66)
67
68 python3 -c "$script"
69}
70
71build-one() {
72 local path=$1
73 local name=$(basename "$path")
74 local name=${name%.md} # Remove .md extension
75 local name=${name/,//} # Remove commas in names (breaks doctools)
76 local title=$(echo "$name" | sed 's/ /-/g')
77
78 mkdir -p "$HTML_BASE_DIR/doc/wiki/"
79
80 local web_url="../../web"
81 build/doc.sh render-only \
82 <(pre-render-wikilinks <"$path") \
83 "$HTML_BASE_DIR/doc/wiki/$name.html" \
84 "$web_url/base.css $web_url/manual.css $web_url/toc.css $web_url/language.css $web_url/code.css" \
85 "$title"
86}
87
88build-dev-guide() {
89 clone-wiki
90 find "$WIKI_DIR" -name '*.md' -print0 | xargs -I {} -0 -- $0 build-one {}
91}
92
93"$@"