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