build.sh (3123B)
1 #!/bin/sh 2 3 basedir="$(pwd)" 4 5 recursivebuild() { 6 local destdir=$(echo "$1" | sed 's|^src|http|') 7 mkdir -p "$destdir" 8 for file in "$1"/*; do 9 if [ -d "$file" ]; then 10 mkdir -p "$destdir/$(basename "$file")" 11 recursivebuild "$file" 12 else 13 copyfile "$file" "$destdir" 14 fi 15 done 16 } 17 18 # This function is currently unused, but we keep it in case we need to 19 # preprocess some m4 files. 20 mdpreprocess() { 21 file="$1" 22 if [ "$(echo "$file" | sed 's/.*\.//')" = "m4" ]; then 23 printf 'm4_include(macros.m4)m4_dnl\n%s\n' "$(cat "$file")" | \ 24 m4 -P -I "$basedir/utils" 25 else 26 cat "$file" 27 fi 28 } 29 30 copyfile() { 31 file=$1 32 dest=$2 33 ind=$dest/index.html 34 extension=$(echo "$file" | sed 's/.*\.//') 35 case "$extension" in 36 # m4 preprocess is currently disabled 37 #m4) 38 # noext=$(echo "$file" | sed 's/\..*//') 39 # mdpreprocess "$file" > $noext.md 40 # ;; 41 md) 42 t="$(markdowntitle "$file")" 43 sed "s/TITLE/$t/" < top.html > "$ind" 44 mdpreprocess "$file" | \ 45 lowdown --html-no-skiphtml --html-no-escapehtml \ 46 >> "$ind" 47 cat bottom.html >> "$ind" 48 ;; 49 html) 50 t="$(htmltitle "$file")" 51 cat top.html "$file" bottom.html | sed "s/TITLE/$t/" > "$ind" 52 ;; 53 raw) 54 namenoraw="$(basename "$file" | sed 's/\.raw$//')" 55 cp "$file" "$dest/$namenoraw" 56 ;; 57 *) 58 cp "$file" "$dest/$(basename "$file")" 59 esac 60 } 61 62 markdowntitle() { 63 grep '^# ' "$1" | head -n 1 | sed 's/^# //' 64 } 65 66 htmltitle() { 67 grep '<!--TITLE: ' "$1" | sed 's/^<!--TITLE: //' | sed 's/-->$//' 68 } 69 70 makeblogindexandfeed() { 71 mkdir -p http/blog 72 bf=http/blog/index.html 73 ff=http/blog/feed.xml 74 75 sed "s/TITLE/Blog/" < top.html > "$bf" 76 { echo '<h1 id="blog">Blog</h1>'; 77 echo '<table id="blog">'; 78 echo '<a href="../series">Blog series</a> - '; 79 echo '<a href="feed.xml">RSS feed</a>'; } >> "$bf" 80 81 cp feed-top.xml "$ff" 82 83 for i in $(ls src/blog | sort -r); do 84 [ -d "src/blog/$i" ] || continue 85 86 f="src/blog/$i/*.md" 87 d="$(echo "$i" | grep -oE '^[0-9]{4}-[0-9]{2}-[0-9]{2}')" 88 t="$(markdowntitle $f)" 89 link="https://sebastiano.tronto.net/blog/$i" 90 91 thisyear="$(echo "$d" | sed 's/-.*//')" 92 if [ "$thisyear" != "$lastyear" ]; then 93 echo "<tr><td><h2>$thisyear</h2></td></tr>" >> "$bf" 94 lastyear=$thisyear 95 fi 96 97 { echo "<tr><td>$d</td>"; 98 echo "<td><a href=\"$i\">$t</a></td></tr>"; } >> "$bf" 99 100 dd="$(echo "$d" | sed 's/.*-//')" 101 mm="$(echo "$d" | sed 's/....-//' | sed 's/-.*//')" 102 mon="$(month "$mm")" 103 { echo "<item>"; 104 echo "<title>$t</title>"; 105 echo "<link>$link</link>"; 106 echo "<guid isPermaLink=\"true\">$link</guid>"; 107 echo "<description>$t</description>"; 108 echo "<pubDate>$dd $mon $thisyear 00:00:00 GMT</pubDate>"; 109 echo "</item>"; 110 echo ""; } >> $ff 111 done 112 113 echo '</table>' >> "$bf" 114 cat bottom.html >> "$bf" 115 116 { echo ""; echo "</channel>"; echo "</rss>"; } >> "$ff" 117 } 118 119 month() { 120 case "$1" in 121 01) 122 echo "Jan" 123 ;; 124 02) 125 echo "Feb" 126 ;; 127 03) 128 echo "Mar" 129 ;; 130 04) 131 echo "Apr" 132 ;; 133 05) 134 echo "May" 135 ;; 136 06) 137 echo "Jun" 138 ;; 139 07) 140 echo "Jul" 141 ;; 142 08) 143 echo "Aug" 144 ;; 145 09) 146 echo "Sep" 147 ;; 148 10) 149 echo "Oct" 150 ;; 151 11) 152 echo "Nov" 153 ;; 154 12) 155 echo "Dec" 156 ;; 157 esac 158 } 159 160 makeblogindexandfeed 161 recursivebuild src