forked from whyjz/GMT-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorate.bash
More file actions
executable file
·184 lines (163 loc) · 8.06 KB
/
decorate.bash
File metadata and controls
executable file
·184 lines (163 loc) · 8.06 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
# insert the following in output html files generated by sphinx, which are in _build/html/:
# 1. a justfont javascript after the </head> tag
# 2. a disqus javascript before the <div role="contentinfo"> tag
# 3. a github ribbon image, replacing "View page source" (at the upper-right corner)
# 4. a favicon before the </head> tag (only in index.html)
# by Whyjay Zheng, first edition on 2016/1/12, last edited on 2017/10/22 (added google fonts for english)
html_files=$(ls _build/html/*.html _build/html/en/*.html)
google_fonts=$(cat _static/google_fonts.txt)
jf_string=$(cat _static/jf_code.txt)
disqus_string=$(cat _static/disqus_code.txt)
github_ribbon=$(cat _static/github_ribbon.txt)
favicon_string=$(cat _static/favicon.txt)
localemenu=$(cat _static/localemenu.txt)
# refer to the escape characters...
# http://unix.stackexchange.com/questions/32907/what-characters-do-i-need-to-escape-when-using-sed-in-a-sh-script
google_fonts=${google_fonts//\\/\\\\} # changes all "\" to "\\"
google_fonts=${google_fonts//\[/\\\[} # changes all "[" to "\["
google_fonts=${google_fonts//\]/\\\]} # changes all "]" to "\]"
google_fonts=${google_fonts//\$/\\\$} # changes all "$" to "\$"
google_fonts=${google_fonts//\./\\\.} # changes all "." to "\."
google_fonts=${google_fonts//\*/\\\*} # changes all "*" to "\*"
google_fonts=${google_fonts//\^/\\\^} # changes all "^" to "\^"
google_fonts=${google_fonts//\//\\\/} # changes all "/" to "\/"
google_fonts=${google_fonts//\"/\\\"} # changes all '"' to '\"'
jf_string=${jf_string//\\/\\\\} # changes all "\" to "\\"
jf_string=${jf_string//\[/\\\[} # changes all "[" to "\["
jf_string=${jf_string//\]/\\\]} # changes all "]" to "\]"
jf_string=${jf_string//\$/\\\$} # changes all "$" to "\$"
jf_string=${jf_string//\./\\\.} # changes all "." to "\."
jf_string=${jf_string//\*/\\\*} # changes all "*" to "\*"
jf_string=${jf_string//\^/\\\^} # changes all "^" to "\^"
jf_string=${jf_string//\//\\\/} # changes all "/" to "\/"
jf_string=${jf_string//\"/\\\"} # changes all '"' to '\"'
disqus_string=${disqus_string//\\/\\\\} # changes all "\" to "\\"
disqus_string=${disqus_string//\[/\\\[} # changes all "[" to "\["
disqus_string=${disqus_string//\]/\\\]} # changes all "]" to "\]"
disqus_string=${disqus_string//\$/\\\$} # changes all "$" to "\$"
disqus_string=${disqus_string//\./\\\.} # changes all "." to "\."
disqus_string=${disqus_string//\*/\\\*} # changes all "*" to "\*"
disqus_string=${disqus_string//\^/\\\^} # changes all "^" to "\^"
disqus_string=${disqus_string//\//\\\/} # changes all "/" to "\/"
disqus_string=${disqus_string//\"/\\\"} # changes all '"' to '\"'
github_ribbon=${github_ribbon//\\/\\\\} # changes all "\" to "\\"
github_ribbon=${github_ribbon//\[/\\\[} # changes all "[" to "\["
github_ribbon=${github_ribbon//\]/\\\]} # changes all "]" to "\]"
github_ribbon=${github_ribbon//\$/\\\$} # changes all "$" to "\$"
github_ribbon=${github_ribbon//\./\\\.} # changes all "." to "\."
github_ribbon=${github_ribbon//\*/\\\*} # changes all "*" to "\*"
github_ribbon=${github_ribbon//\^/\\\^} # changes all "^" to "\^"
github_ribbon=${github_ribbon//\//\\\/} # changes all "/" to "\/"
github_ribbon=${github_ribbon//\"/\\\"} # changes all '"' to '\"'
favicon_string=${favicon_string//\\/\\\\} # changes all "\" to "\\"
favicon_string=${favicon_string//\[/\\\[} # changes all "[" to "\["
favicon_string=${favicon_string//\]/\\\]} # changes all "]" to "\]"
favicon_string=${favicon_string//\$/\\\$} # changes all "$" to "\$"
favicon_string=${favicon_string//\./\\\.} # changes all "." to "\."
favicon_string=${favicon_string//\*/\\\*} # changes all "*" to "\*"
favicon_string=${favicon_string//\^/\\\^} # changes all "^" to "\^"
favicon_string=${favicon_string//\//\\\/} # changes all "/" to "\/"
favicon_string=${favicon_string//\"/\\\"} # changes all '"' to '\"'
localemenu=${localemenu//\\/\\\\} # changes all "\" to "\\"
localemenu=${localemenu//\[/\\\[} # changes all "[" to "\["
localemenu=${localemenu//\]/\\\]} # changes all "]" to "\]"
localemenu=${localemenu//\$/\\\$} # changes all "$" to "\$"
localemenu=${localemenu//\./\\\.} # changes all "." to "\."
localemenu=${localemenu//\*/\\\*} # changes all "*" to "\*"
localemenu=${localemenu//\^/\\\^} # changes all "^" to "\^"
localemenu=${localemenu//\//\\\/} # changes all "/" to "\/"
localemenu=${localemenu//\"/\\\"} # changes all '"' to '\"'
# start to insert
for html_f in $html_files
do
# ==== Attaching google fonts code ====
if grep -q 'googleapis' $html_f; then
echo skip ${html_f##*/} - already attached the google fonts code
else
echo ----- Attaching google fonts code to ${html_f##*/} ...
# insert a new line after theme.css line
sed -i "/theme.css/a\ $google_fonts" $html_f
fi
# ==== Attaching jf code ====
if grep -q 'jf.push' $html_f; then
echo skip ${html_f##*/} - already attached the jf code
else
echo ----- Attaching jf code to ${html_f##*/} ...
# insert a new line after </head> tag
sed -i "/<\/head>/a\ $jf_string" $html_f
fi
# ==== Attaching disqus code ====
if grep -q 'disqus_thread' $html_f; then
echo skip ${html_f##*/} - already attached the disqus code
else
echo ----- Attaching disqus code to ${html_f##*/} ...
dyn_id=${html_f##*/}
dyn_id=${dyn_id%%.*}
unique_disqus_string=${disqus_string//DYNAMIC_ID/$dyn_id}
sed -i "/<div\ role=\"contentinfo\">/i\ $unique_disqus_string" $html_f
fi
# ==== Replacing "View source file" with a github ribbon ====
if grep -q 'forkme_right_green' $html_f; then
echo skip ${html_f##*/} - already attached the github ribbon
else
if grep -q 'View page source' $html_f; then
echo ----- Attaching a github ribbon to ${html_f##*/} ...
sed -i "/View page source/a\ $github_ribbon" $html_f
sed -i "/View page source/d" $html_f
else
echo skip ${html_f##*/} - No "View page source"
fi
fi
done
# or $ find . -type f -exec sed -e 's/cpu/memory/ig' '{}' \;
# see http://blog.miniasp.com/post/2010/12/24/Useful-tool-Find-and-Replace-with-sed-command.aspx
# ==== Add favicon in index.html ====
indpage="_build/html/index.html"
if grep -q 'shortcut icon' $indpage; then
echo skip favicon - already replaced
else
echo ----- Replacing favicon to ${indpage##*/} ...
# insert a new line before </head> tag
sed -i "/<\/head>/i\ $favicon_string" $indpage
fi
# ==== Add locate menu ====
for html_f in $html_files
do
if grep -q 'clearfix' $html_f; then
echo skip locale - already added
else
html_name=${html_f##*/}
echo ----- Adding locale to $html_name ...
if ( echo $html_f | grep -q '/en/' ); then
localemenu_each=${localemenu/LinkToChinese/\\.\\.\\\/${html_name/./\\.}}
localemenu_each=${localemenu_each/LinkToEnglish/${html_name/./\\.}}
else
localemenu_each=${localemenu/LinkToChinese/${html_name/./\\.}}
localemenu_each=${localemenu_each/LinkToEnglish/en\\\/${html_name/./\\.}}
fi
# delete 2 lines before class "wy-menu wy-menu-vertical"
vi -e - $html_f <<COMMANDEND
g/wy-menu wy-menu-vertical/-2,-1d
wq
COMMANDEND
# add locale menu body
sed -i "/wy-menu wy-menu-vertical/i\ $localemenu_each" $html_f
# add a </div> back, which was deleted by vi
sed -i "/wy-menu wy-menu-vertical/i\ <\/div>" $html_f
fi
done
# ==== Replace project name in Chinese with English ====
en_html_files=$(ls _build/html/en/*.html)
for html_f in $en_html_files
do
echo ----- Finalizing English Locale of ${html_f##*/} ...
sed -i -e 's/GMT 教學手冊/GMT Tutorials /g' $html_f
sed -i -e 's/zzz_replace_token/English/g' $html_f
done
zhtw_html_files=$(ls _build/html/*.html)
for html_f in $zhtw_html_files
do
echo ----- Finalizing Zhtw Locale of ${html_f##*/} ...
sed -i -e 's/zzz_replace_token/中文 (台灣)/g' $html_f
done