build-incremental.sh (5144B)
1 #!/bin/sh 2 3 basedir="$(pwd)" 4 5 if [ "$(uname)" = "Linux" ]; then 6 date_last_modified_cmd="stat -c %Y" 7 else 8 date_last_modified_cmd="stat -f %m" 9 fi 10 11 age_top="$($date_last_modified_cmd top.html)" 12 age_top_nobar="$($date_last_modified_cmd top-nobar.html)" 13 age_bottom="$($date_last_modified_cmd bottom.html)" 14 15 recursivebuild() { 16 local destdir=$(echo "$1" | sed 's|^src|http|') 17 mkdir -p "$destdir" 18 for file in "$1"/*; do 19 if [ -d "$file" ]; then 20 mkdir -p "$destdir/$(basename "$file")" 21 recursivebuild "$file" 22 else 23 copyfile "$file" "$destdir" 24 fi 25 done 26 } 27 28 # This function is currently useless, but we keep it in case we need to 29 # preprocess some m4 files. 30 mdpreprocess() { 31 file="$1" 32 if [ "$(echo "$file" | sed 's/.*\.//')" = "m4" ]; then 33 printf 'm4_include(macros.m4)m4_dnl\n%s\n' "$(cat "$file")" | \ 34 m4 -P -I "$basedir/utils" 35 else 36 cat "$file" 37 fi 38 } 39 40 copyfile() { 41 file=$1 42 dest=$2 43 ind=$dest/index.html 44 extension=$(echo "$file" | sed 's/.*\.//') 45 age_src="$($date_last_modified_cmd "$file")" 46 47 case "$extension" in 48 # m4 preprocess is currently disabled 49 #m4) 50 # noext=$(echo "$file" | sed 's/\..*//') 51 # mdpreprocess "$file" > $noext.md 52 # ;; 53 md) 54 if [ \( -f "$ind" \) ]; then 55 age_dst="$($date_last_modified_cmd "$ind")" 56 if [ \( "$age_dst" -ge "$age_src" \) -a \ 57 \( "$age_dst" -ge "$age_top" \) -a \ 58 \( "$age_dst" -ge "$age_bottom" \) ]; then 59 echo "Skipping "$file" -> "$ind"" 60 return 61 fi 62 fi 63 64 echo "Building "$file" -> "$ind"" 65 66 t="$(markdowntitle "$file")" 67 sed "s/TITLE/$t/" < top.html > "$ind" 68 mdpreprocess "$file" | \ 69 lowdown --html-no-skiphtml --html-no-escapehtml \ 70 >> "$ind" 71 cat bottom.html >> "$ind" 72 ;; 73 noheadermd) 74 if [ \( -f "$ind" \) ]; then 75 age_dst="$($date_last_modified_cmd "$ind")" 76 if [ \( "$age_dst" -ge "$age_src" \) -a \ 77 \( "$age_dst" -ge "$age_top_nobar" \) -a \ 78 \( "$age_dst" -ge "$age_bottom" \) ]; then 79 echo "Skipping "$file" -> "$ind"" 80 return 81 fi 82 fi 83 84 echo "Building "$file" -> "$ind"" 85 86 t="$(markdowntitle "$file")" 87 sed "s/TITLE/$t/" < top-nobar.html > "$ind" 88 mdpreprocess "$file" | \ 89 lowdown --html-no-skiphtml --html-no-escapehtml \ 90 >> "$ind" 91 ;; 92 html) 93 if [ \( -f "$ind" \) ]; then 94 age_dst="$($date_last_modified_cmd "$ind")" 95 if [ \( "$age_dst" -ge "$age_src" \) -a \ 96 \( "$age_dst" -ge "$age_top" \) -a \ 97 \( "$age_dst" -ge "$age_bottom" \) ]; then 98 echo "Skipping "$file" -> "$ind"" 99 return 100 fi 101 fi 102 103 echo "Copying "$file" -> "$ind"" 104 105 t="$(htmltitle "$file")" 106 cat top.html "$file" bottom.html | sed "s/TITLE/$t/" > "$ind" 107 ;; 108 raw) 109 namenoraw="$(basename "$file" | sed 's/\.raw$//')" 110 if [ \( -f "$dest/$namenoraw" \) ]; then 111 age_dst="$($date_last_modified_cmd "$dest/$namenoraw")" 112 if [ "$age_dst" -ge "$age_src" ]; then 113 echo "Skipping "$file" -> "$dest/$namenoraw"" 114 return 115 fi 116 fi 117 118 echo "Copying "$file" -> "$dest/$namenoraw"" 119 120 cp "$file" "$dest/$namenoraw" 121 ;; 122 *) 123 dest_name="$(basename "$file")" 124 if [ \( -f "$dest/$dest_name" \) ]; then 125 age_dst="$($date_last_modified_cmd "$dest/$dest_name")" 126 if [ "$age_dst" -ge "$age_src" ]; then 127 echo "Skipping "$file" -> "$dest/$dest_name"" 128 return 129 fi 130 fi 131 132 echo "Copying "$file" -> "$dest/$dest_name"" 133 134 cp "$file" "$dest/$dest_name" 135 esac 136 } 137 138 markdowntitle() { 139 grep '^# ' "$1" | head -n 1 | sed 's/^# //' 140 } 141 142 htmltitle() { 143 grep '<!--TITLE: ' "$1" | sed 's/^<!--TITLE: //' | sed 's/-->$//' 144 } 145 146 makeblogindexandfeed() { 147 mkdir -p http/blog 148 bf=http/blog/index.html 149 ff=http/blog/feed.xml 150 151 sed "s/TITLE/Blog/" < top.html > "$bf" 152 { echo '<h1 id="blog">Blog</h1>'; 153 echo '<table id="blog">'; 154 echo '<a href="../series">Blog series</a> - '; 155 echo '<a href="feed.xml">RSS feed</a>'; } >> "$bf" 156 157 cp feed-top.xml "$ff" 158 159 for i in $(ls src/blog | sort -r); do 160 [ -d "src/blog/$i" ] || continue 161 162 f="src/blog/$i/*.md" 163 d="$(echo "$i" | grep -oE '^[0-9]{4}-[0-9]{2}-[0-9]{2}')" 164 t="$(markdowntitle $f)" 165 link="https://sebastiano.tronto.net/blog/$i" 166 167 thisyear="$(echo "$d" | sed 's/-.*//')" 168 if [ "$thisyear" != "$lastyear" ]; then 169 echo "<tr><td><h2>$thisyear</h2></td></tr>" >> "$bf" 170 lastyear=$thisyear 171 fi 172 173 { echo "<tr><td>$d</td>"; 174 echo "<td><a href=\"$i\">$t</a></td></tr>"; } >> "$bf" 175 176 dd="$(echo "$d" | sed 's/.*-//')" 177 mm="$(echo "$d" | sed 's/....-//' | sed 's/-.*//')" 178 mon="$(month "$mm")" 179 { echo "<item>"; 180 echo "<title>$t</title>"; 181 echo "<link>$link</link>"; 182 echo "<guid isPermaLink=\"true\">$link</guid>"; 183 echo "<description>$t</description>"; 184 echo "<pubDate>$dd $mon $thisyear 00:00:00 GMT</pubDate>"; 185 echo "</item>"; 186 echo ""; } >> $ff 187 done 188 189 echo '</table>' >> "$bf" 190 cat bottom.html >> "$bf" 191 192 { echo ""; echo "</channel>"; echo "</rss>"; } >> "$ff" 193 } 194 195 month() { 196 case "$1" in 197 01) 198 echo "Jan" 199 ;; 200 02) 201 echo "Feb" 202 ;; 203 03) 204 echo "Mar" 205 ;; 206 04) 207 echo "Apr" 208 ;; 209 05) 210 echo "May" 211 ;; 212 06) 213 echo "Jun" 214 ;; 215 07) 216 echo "Jul" 217 ;; 218 08) 219 echo "Aug" 220 ;; 221 09) 222 echo "Sep" 223 ;; 224 10) 225 echo "Oct" 226 ;; 227 11) 228 echo "Nov" 229 ;; 230 12) 231 echo "Dec" 232 ;; 233 esac 234 } 235 236 makeblogindexandfeed 237 recursivebuild src