-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
executable file
·147 lines (118 loc) · 3.97 KB
/
build
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
145
146
147
#!/usr/bin/env bash
# How the script works?
# - Substitutes the brand_color setting in the variables.less file
# of the theme being customized. That is the only customization.
# - Compiles and minimizes the css using the build.less to create
# bootstrap.min.css
# - Copies files css, js and html template files from sphinx_bootstrap_theme
# into the right paths in the ./theme directory
# - At the end of the process the ./theme directory can be copied to
# as sphinx directory and used as a local theme as documented here
# http://www.sphinx-doc.org/en/stable/theming.html#using-a-theme
# e.g. with these settings
# html_theme = "theme"
# html_theme_path = ["."]
# What can go wrong?
# - wrong input (i.e not a valid color)
# - boostrap or bootwatch version changes
# - layout.html changes where it finds the css and js files
tcolor=$(tput setaf 7) # trace color
ocolor=$(tput setaf 4) # options color
reset=$(tput sgr0) # reset color codes
themes=(cerulean cosmo cyborg darkly flatly journal lumen
paper readable sandstone simplex slate solar spacelab
superhero united yeti)
usage() {
cat <<EOF
Usage: build [--theme theme] [--brand-primary color] [...]
theme: Theme is to be customized. This is is optional
(default is united), should be one of the
following:$ocolor
--brand-primary : color (in quotes e.g '#FF12AD')
[...] - other variables specified in the variables.less
file, without the '@' symbol. Any hex colors must be
quoted so that '#' is not parse as a comment delimeter.
eg. build --theme flatly --brand-color '#F1F1F1' --font-size-base 15px
${themes[*]} $reset
EOF
}
# default values
base_theme='united'
primary=''
success=''
info=''
warning=''
danger=''
declare -A variables
while [[ "$#" > 1 ]]; do
var=$1
if [[ $var =~ ^-- ]]; then
name=${var:2} # strip the --
variables[$name]="$2"
fi
# Shift past the argname and value (2 tokens)
shift; shift
done
# the theme is not part of the less variables
if [[ -n ${variables[theme]} ]]; then
base_theme=${variables[theme]}
unset variables[theme]
fi
if [[ ! "${themes[@]}" =~ "${base_theme}" ]]; then
usage
exit 1
fi
# Debugging friends
export PS4='$tcolor+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }$reset'
# set -o xtrace
base_theme_dir="./bootswatch/$base_theme/"
bootstrap_dir="sphinx-bootstrap-theme/sphinx_bootstrap_theme/bootstrap"
bootstrap_version="bootstrap-3.3.7"
variables_file="theme/variables.less"
# Destination directories
theme_dir=theme
bootstrap_css_dir=theme/static/$bootstrap_version/css
bootstrap_js_dir=theme/static/$bootstrap_version/js
jquery_dir=theme/static/js
# Create theme directory structure
rm -rf $theme_dir
mkdir -p $bootstrap_css_dir
mkdir -p $bootstrap_js_dir
mkdir -p $jquery_dir
# Copy less files
cp -f {$base_theme_dir,$theme_dir}/variables.less
cp -f {$base_theme_dir,$theme_dir}/bootswatch.less
# Function to customize the colors
# How to use it?
# $cat variables.less | substitute 'brand-primary' '#ff0011'
substitute() {
variable=$1
value=$2
if [[ -n "$value" ]]; then
cat | sed -e "s/\(@$variable:\s\+\)\(.\+\);/\1$value;/g"
else
cat
fi
}
# Do customization
contents=$(cat $variables_file)
for name in "${!variables[@]}"; do
value=${variables[$name]}
contents=$(echo "$contents" | substitute $name $value)
done
echo "$contents" > $variables_file
# Compile and minify (into the final destination)
lessc build.less > theme/bootstrap.css
cleancss -o $bootstrap_css_dir/bootstrap.min.css theme/bootstrap.css code_pygments.css
# Cleanup
rm -rf theme/*.less
rm -rf theme/bootstrap.css
# Collect the css
cp {$bootstrap_dir,$theme_dir}/static/bootstrap-sphinx.css_t
# Collect the js
cp $bootstrap_dir/static/js/jquery*.js $jquery_dir
cp {$bootstrap_dir,$theme_dir}/static/$bootstrap_version/js/bootstrap.min.js
cp {$bootstrap_dir,$theme_dir}/static/bootstrap-sphinx.js_t
# Copy the sphinx conf file and html templates
cp {$bootstrap_dir,$theme_dir}/theme.conf
cp $bootstrap_dir/*.html $theme_dir/