3232# into a book-like collection. It should perhaps be viewed as a collection of essays,
3333# since each README.md file is written independently.
3434
35+
36+ # # fail fast in the event of a failure of any command in this script
37+ set -e
38+
3539# # This script assumes it is running at $METRON_SOURCE/site-book/bin/
3640METRON_SOURCE=` cd $( dirname $0 ) ; cd ../..; pwd`
3741
@@ -49,7 +53,7 @@ EXCLUSION_LIST=(
4953
5054# # This is a list of resources (eg .png files) needed to render the markdown files.
5155# # Each entry is a file path, relative to $METRON_SOURCE.
52- # # Note: any images in site-book/src/site/resources/image-archive / will also be included.
56+ # # Note: any images in site-book/src/site/src- resources/images / will also be included.
5357RESOURCE_LIST=(
5458 metron-platform/metron-parsers/parser_arch.png
5559 metron-platform/metron-indexing/indexing_arch.png
@@ -67,6 +71,8 @@ HREF_REWRITE_LIST=(
6771 metron-analytics/metron-maas-service/README.md ' s#(maas_arch.png)#(../../images/maas_arch.png)#g'
6872)
6973
74+ TEMPLATES_DIR=" $METRON_SOURCE /site-book/src/site/src-resources/templates"
75+
7076
7177# #####################
7278# #####################
@@ -77,18 +83,21 @@ HREF_REWRITE_LIST=(
7783TRACE_ENABLE=0
7884function trace () {
7985 if (( $TRACE_ENABLE == 1 )) ; then
80- echo " $* "
86+ echo " $* "
8187 fi # else do nothing
8288}
8389TREE_TRACE_ENABLE=0
8490function tree_trace () {
8591 if (( $TREE_TRACE_ENABLE == 1 )) ; then
86- echo " $* "
92+ echo " $* "
8793 fi # else do nothing
8894}
8995
96+ # file used for storing error messages during re-write routine
97+ SCRATCH_ERR_FILE_NAME=" $METRON_SOURCE /site-book/src/site/errout.dat"
98+
9099# input: cumulative directory_path, indent_level
91- # output: items to site.tmp , as lines of text
100+ # output: items to site.xml , as lines of text
92101# This function is called recursively as we descend the directory tree
93102# The cum_dir_path must not have a terminal "/".
94103function descend () {
@@ -100,38 +109,38 @@ function descend () {
100109 indent=$2
101110
102111 if [ -e " ${cum_dir_path} " /index.md ] ; then
103- dir_name=` basename " $cum_dir_path " `
104- dir_name=" ${dir_name# metron-} " # remove the "metron-" prefix if present
105- dir_name=` get_prettyname " $dir_name " ` # capitalize the remainder
106- # Is it a leaf node?
107- num_peers=` ls -d " ${cum_dir_path} " /* | wc -l`
108- if (( $num_peers == 1 )) ; then # yes, it's a leaf node, do a closed item
109- echo " ${INDENTS[$indent]} <item name='${dir_name} ' href='${cum_dir_path} /index.html'/>" >> ../site.tmp
110- tree_trace " exit descend due to leaf node"
111- return # nothing else to process in this directory path
112- fi # otherwise carry on with open item and child items at deeper indent
113- echo " ${INDENTS[$indent]} <item name='${dir_name} ' href='${cum_dir_path} /index.html' collapse='true'>" >> ../site.tmp
114- open_item_exists=1
115- indent=$(( indent + 1 ))
112+ dir_name=` basename " $cum_dir_path " `
113+ dir_name=" ${dir_name# metron-} " # remove the "metron-" prefix if present
114+ dir_name=` get_prettyname " $dir_name " ` # capitalize the remainder
115+ # Is it a leaf node?
116+ num_peers=` ls -d " ${cum_dir_path} " /* | wc -l`
117+ if (( $num_peers == 1 )) ; then # yes, it's a leaf node, do a closed item
118+ echo " ${INDENTS[$indent]} <item name='${dir_name} ' href='${cum_dir_path} /index.html'/>" >> ../site.xml
119+ tree_trace " exit descend due to leaf node"
120+ return # nothing else to process in this directory path
121+ fi # otherwise carry on with open item and child items at deeper indent
122+ echo " ${INDENTS[$indent]} <item name='${dir_name} ' href='${cum_dir_path} /index.html' collapse='true'>" >> ../site.xml
123+ open_item_exists=1
124+ indent=$(( indent + 1 ))
116125 else
117- open_item_exists=0
126+ open_item_exists=0
118127 fi
119128 for md in " ${cum_dir_path} " /* .md ; do
120- if [ ! -e " $md " ] ; then continue ; fi # globbing sometimes gives spurious results
121- item_name=` basename -s " .md" " $md " ` # strip the suffix
122- if [ " $item_name " != " index" ] ; then
123- echo " ${INDENTS[$indent]} <item name='${item_name} ' href='${cum_dir_path} /${item_name} .html'/>" >> ../site.tmp
124- fi
129+ if [ ! -e " $md " ] ; then continue ; fi # globbing sometimes gives spurious results
130+ item_name=` basename -s " .md" " $md " ` # strip the suffix
131+ if [ " $item_name " != " index" ] ; then
132+ echo " ${INDENTS[$indent]} <item name='${item_name} ' href='${cum_dir_path} /${item_name} .html'/>" >> ../site.xml
133+ fi
125134 done
126135 for dir in " ${cum_dir_path} " /* ; do
127- if [ ! -e " $dir " ] ; then continue ; fi # globbing sometimes gives spurious results
128- if [ -d " $dir " ] ; then
129- descend " $dir " $indent
130- fi
136+ if [ ! -e " $dir " ] ; then continue ; fi # globbing sometimes gives spurious results
137+ if [ -d " $dir " ] ; then
138+ descend " $dir " $indent
139+ fi
131140 done
132141 if (( open_item_exists == 1 )) ; then
133- indent=$(( indent - 1 )) # close the item
134- echo " ${INDENTS[$indent]} </item>" >> ../site.tmp
142+ indent=$(( indent - 1 )) # close the item
143+ echo " ${INDENTS[$indent]} </item>" >> ../site.xml
135144 fi
136145 tree_trace " exit descend with indent = $indent "
137146}
@@ -144,12 +153,29 @@ function get_prettyname () {
144153 echo " $( tr ' [:lower:]' ' [:upper:]' <<< ${1: 0: 1} ) ${1: 1} "
145154}
146155
156+ # This function, with the following traps, cleans up before exiting, if interrupted during the re-write routine
157+ function quit () {
158+ exitCode=${1:- 0}
159+ rm -f " $SCRATCH_ERR_FILE_NAME "
160+ exit $exitCode
161+ }
162+ trap ' quit 129' SIGHUP
163+ trap ' quit 130' SIGINT
164+ trap ' quit 143' SIGTERM
165+ trap ' quit $?' ERR
166+
147167
148168# #####################
149169# # Proceed
150170
151171cd " $METRON_SOURCE "
152172
173+ # Validate that the src/site directory is writable for generated content
174+ if [ ! -w " site-book/src/site" ]; then
175+ echo " ERROR: 'site-book/src/site' is not writable" > /dev/stderr
176+ exit 126
177+ fi
178+
153179# Clean up generated directories
154180if [ -d " $METRON_SOURCE " /site-book/src/site/markdown ] ; then
155181 rm -rf " $METRON_SOURCE " /site-book/src/site/markdown ; fi
@@ -178,7 +204,7 @@ tar cvf - "${MD_FILE_LIST[@]}" | ( cd "$METRON_SOURCE"/site-book/src/site/markdo
178204# Grab the other resources needed
179205echo " "
180206echo Collecting additional resource files:
181- for r in " ${RESOURCE_LIST[@]} " site-book/src/site/resources/image-archive /* ; do
207+ for r in " ${RESOURCE_LIST[@]} " site-book/src/site/src- resources/images /* ; do
182208 if [ ! -e " $r " ] ; then continue ; fi # globbing sometimes gives spurious results
183209 echo ./" $r "
184210 cp " $r " " $METRON_SOURCE " /site-book/src/site/resources/images/
@@ -191,18 +217,18 @@ cd site-book/src/site/markdown
191217for (( i= 0 ; i< ${# HREF_REWRITE_LIST[@]} ; i+= 2 )) ; do
192218 echo rewriting href in " ${HREF_REWRITE_LIST[$i]} " : " ${HREF_REWRITE_LIST[ $(( i + 1 )) ]} "
193219 case " ${OSTYPE} " in
194- linux* )
195- # Linux sed correctly parses lack of argument after -i option
220+ linux* )
221+ # Linux sed correctly parses lack of argument after -i option
196222 sed -i -e " ${HREF_REWRITE_LIST[ $(( i + 1 )) ]} " " ${HREF_REWRITE_LIST[$i]} "
197- ;;
198- darwin* )
223+ ;;
224+ darwin* )
199225 # MacOS sed needs an empty-string argument after -i option to get the same result
200226 sed -i ' ' -e " ${HREF_REWRITE_LIST[ $(( i + 1 )) ]} " " ${HREF_REWRITE_LIST[$i]} "
201- ;;
202- * )
203- echo " ERROR: Unable to determine 'sed' argument list for OS ${OSTYPE} " > /dev/stderr
204- exit -1
205- ;;
227+ ;;
228+ * )
229+ echo " ERROR: Unable to determine 'sed' argument list for OS ${OSTYPE} " > /dev/stderr
230+ exit 126
231+ ;;
206232 esac
207233done
208234echo " "
@@ -213,15 +239,15 @@ echo " "
213239echo Renaming \" README\" files to \" index\" files.
214240if (( `ls - R | grep - c 'index.md'` > 0 )) ; then
215241 echo " ERROR: index.md file exists in tree already, we currently don't handle that"
216- exit - 1
242+ exit 1
217243fi
218244find . -name README.md -execdir mv README.md index.md \;
219245echo " "
220246
221- # Insert the tree of generated html files in the LHS menu of the site.xml
247+ # Insert the tree of generated html files in the LHS nav menu of the site.xml
222248# The problem is that we want a depth-first listing, with files before subdirectories, and "index" always first.
223- # So the following logic is a little complex, but we avoid having to hardwire the tree structure -- which we
224- # may go back to in the long run .
249+ # And we synthesize the page labels in the nav tree from the directory paths.
250+ # So the following logic is a little complex, but we avoid having to hardwire the tree structure .
225251
226252BEGIN_TAG=" BEGIN_MENU_TREE"
227253END_TAG=" END_MENU_TREE"
@@ -231,12 +257,15 @@ echo "Generating menu tree from directory tree structure"
231257echo " "
232258
233259# Copy the first part of the file, up to where the menu tree goes.
234- sed -n -e " 1,/${BEGIN_TAG} / p" ../site.xml > ../site.tmp
260+ if [ -e ../site.xml ]; then
261+ mv ../site.xml ../site.xml.bak
262+ fi
263+ sed -n -e " 1,/${BEGIN_TAG} / p" " $TEMPLATES_DIR " /site.xml.template > ../site.xml
235264
236265# Now start inserting menu tree items
237266# top level of markdown tree is special
238267if [ -e index.md ] ; then
239- echo " <item name='Metron' href='index.html' title='Apache Metron - Incubating' collapse='false'>" >> ../site.tmp
268+ echo " <item name='Metron' href='index.html' title='Apache Metron - Incubating' collapse='false'>" >> ../site.xml
240269 item0_exists=1
241270else
242271 item0_exists=0
@@ -245,45 +274,38 @@ indent_level=1
245274for md in * .md ; do
246275 if [ ! -e " $md " ] ; then continue ; fi # globbing sometimes gives spurious results
247276 if [ " $md " != " index.md" ] ; then
248- item_name=" ${md% .* } " # strip the suffix
249- echo " ${INDENTS[$indent_level]} <item name='${item_name} ' href='${item_name} .html' />" >> ../site.tmp
277+ item_name=" ${md% .* } " # strip the suffix
278+ echo " ${INDENTS[$indent_level]} <item name='${item_name} ' href='${item_name} .html' />" >> ../site.xml
250279 fi
251280done
252281for dir in * ; do
253282 if [ ! -e " $dir " ] ; then continue ; fi # globbing sometimes gives spurious results
254283 if [ -d " $dir " ] ; then
255- descend " $dir " $indent_level
284+ descend " $dir " $indent_level
256285 fi
257286done
258287if (( item0 _exists == 1 )) ; then
259- echo " </item>" >> ../site.tmp
288+ echo " </item>" >> ../site.xml
260289fi
261290
262291# Copy the last part of the file, from the end of the menu tree.
263- sed -n -e " /${END_TAG} /," ' $ p' ../site.xml >> ../site.tmp
264-
265- mv ../site.xml ../site.xml.bak
266- mv ../site.tmp ../site.xml
292+ sed -n -e " /${END_TAG} /," ' $ p' " $TEMPLATES_DIR " /site.xml.template >> ../site.xml
267293
268294echo " Done."
269295echo " "
270296
271297echo " Fixing up markdown dialect problems between Github-MD and doxia-markdown:"
272- # Detecting errors from a `find -exec` command is difficult, have to use an intermediary file
273- errfile=" ../errout.dat"
274- if [ -e $errfile ]; then
275- rm -f $errfile
276- fi
277- touch $errfile
278- find . -name ' *.md' -print -exec python " $METRON_SOURCE " /site-book/bin/fix-md-dialect.py ' {}' \; 2>> $errfile
279- errlines=` cat $errfile | wc -l`
280- if (( $errlines > 0 )) ; then
281- echo " ERROR DETECTED:"
282- cat $errfile
283- rm -f $errfile
284- exit 127
298+ # Detecting errors from a `find -exec` command is difficult. We do it using an intermediary file.
299+ rm -f " $SCRATCH_ERR_FILE_NAME "
300+ find . -name ' *.md' -print -exec python " $METRON_SOURCE " /site-book/bin/fix-md-dialect.py ' {}' \; 2> " $SCRATCH_ERR_FILE_NAME "
301+ errlines=` wc -l " $SCRATCH_ERR_FILE_NAME " `
302+ if (( ${errlines% * } > 0 )) ; then
303+ echo " ERROR OR ERRORS DETECTED:"
304+ cat " $SCRATCH_ERR_FILE_NAME "
305+ rm -f " $SCRATCH_ERR_FILE_NAME "
306+ exit 1
285307else
286- rm -f $errfile
308+ rm -f " $SCRATCH_ERR_FILE_NAME "
287309 echo " Done."
288310 echo " "
289311 exit 0
0 commit comments