1c9d023eb75b3af7375afee5aa3e3fa049937dca
[src/app-framework-main.git] / doc / updt.sh
1 #!/bin/bash
2
3 # the HTML template
4 main='<html>
5 <head>
6   <link rel="stylesheet" type="text/css" href="doc.css">
7   <meta charset="UTF-8">
8 </head>
9 <body>
10 GENERATED-MARKDOWN-HERE
11 </body>
12 </html>'
13
14 # substitute the pattern $1 by the content of the file $2
15 subst() {
16   awk -v pat="$1" -v rep="$(sed 's:\\:\\\\:g' $2)" '{gsub(pat,rep);gsub(pat,"\\&");print}'
17 }
18
19 # update the date field of file $1
20 updadate() {
21   local x=$1
22   local t=$(git log -n 1 --format=%ct $x)
23   [[ -n "$t" ]] || t=$(stat -c %Y $x)
24   local d=$(LANG= date -d @$t +"%d %B %Y")
25   sed -i "s/^\(    Date: *\).*/\1$d/" $x
26 }
27
28 # make the html file for $1
29 mkhtml() {
30   local x=$1
31   local h=${x%%.md}.html
32   expand -i $x | sed 's:^        :    :' > $h.pre
33   markdown -f toc,autolink $h.pre > $h.toc.no
34   markdown -Tf toc,autolink $h.pre > $h.toc.yes
35   head --bytes=-$(stat -c %s $h.toc.no) $h.toc.yes > $h.toc
36   echo "$main" |
37   subst GENERATED-MARKDOWN-HERE $h.toc.no |
38   subst TABLE-OF-CONTENT-HERE $h.toc > $h
39   rm $h.*
40 }
41
42 # apply
43 for x in *.md; do
44   updadate $x
45   mkhtml $x
46 done
47