-
Notifications
You must be signed in to change notification settings - Fork 23
/
build-docs.sh
executable file
·144 lines (120 loc) · 3.48 KB
/
build-docs.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/bin/bash
usage_string="usage: ./$(basename "$0") [OPTIONS]
Script for building docs from source code
Options:
--theme THEME Specify theme: alabaster, rtd or python (default: rtd)
--open BROWSER Open docs after build with the provided browser
-h, --help Display help message and exit
Examples:
"
usage() { echo -n "$usage_string" 1>&2; }
parse_project_version() {
source_code=$1
init_file="./${source_code}/__init__.py"
parsed=$(sed -n 's/^__version__ = \(.*\)/\1/p' < ${init_file})
version=$(echo $parsed | tr -d "\'")
echo $version
}
CONTENT="docs" # Content that is not auto-generated from docs
TARGET="docs/target" # Will contain auto-generated files (unstaged)
CONFIG="$TARGET/source/conf.py" # Sphinx configuration file
ALABASTER_THEME="alabaster" # Default sphinx theme
RTD_THEME="sphinx_rtd_theme" # Read-the-docs theme
PYTHON_THEME="python_docs_theme" # Python docs theme
LANG="en"
BROWSER=
PROJECT="pymerkle"
SOURCE_CODE="pymerkle"
VERSION=$(parse_project_version "$SOURCE_CODE")
AUTHOR="fmerg"
THEME="$RTD_THEME"
while [[ $# -gt 0 ]]
do
arg="$1"
case $arg in
--open)
BROWSER="$2"
shift
shift
;;
--theme)
case $2 in
alabaster)
THEME="$ALABASTER_THEME"
;;
rtd)
THEME="$RTD_THEME"
;;
python)
THEME="$PYTHON_THEME"
;;
*)
echo "[-] Unsupported theme: $arg"
usage
exit 1
;;
esac
shift
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "[-] Invalid argument: $arg"
usage
exit 1
;;
esac
done
set -e
export LC_ALL=C
# Generate sphinx source
rm -rf "$TARGET"
sphinx-quickstart "$TARGET" \
--project "$PROJECT" \
--release "$VERSION" \
--author "$AUTHOR" \
--language "$LANG" \
--ext-autodoc \
--ext-intersphinx \
--ext-coverage \
--ext-viewcode \
--sep
# Adjust sphinx configuration
sed -ie "/html_theme/s/$ALABASTER_THEME/$THEME/" $CONFIG
echo >> $CONFIG
echo "master_doc = 'index'" >> $CONFIG
echo "pygments_style = 'sphinx'" >> $CONFIG
# Make sphinx configuration see the source code
line_1="import os"
line_2="import sys"
line_3="sys.path.insert(0, os.path.abspath('.'))"
sed -ie "/$line_1/s/^# //" $CONFIG
sed -ie "/$line_2/s/^# //" $CONFIG
sed -ie "/$line_3/s/^# //" $CONFIG
sed -ie "/$line_3/s/('.')/('..\/..\/..')/" $CONFIG
# echo "autodoc_default_options = {'private-members': True}" >> $CONFIG
echo "extensions += ['sphinx.ext.autosectionlabel']" >> $CONFIG
if [ $THEME == $PYTHON_THEME ]; then
echo "html_sidebars = {
'**': ['globaltoc.html', 'sourcelink.html', 'searchbox.html'],
'using/windows': ['windowssidebar.html', 'searchbox.html']
}" >> $CONFIG
fi
# Content auto-generated from source code
sphinx-apidoc \
--force \
--module-first \
-o "$TARGET/source" $SOURCE_CODE
# Transfer non auto-generated content to sphinx source
for f in "$CONTENT"/*.rst; do
cp "$f" "$TARGET/source/$(basename "$f")"
done
# Build html artifacts from sphinx source
sphinx-build -b html "$TARGET/source/" "$TARGET/build/html"
# Navigate locally with browser
if [ -n "$BROWSER" ]; then
$BROWSER "$TARGET/build/html/index.html"
fi