build.sh (2003B)
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 *) 33 cp "$file" "$dest/$(basename "$file")" 34 esac 35 } 36 37 markdowntitle() { 38 grep '^# ' "$1" | head -n 1 | sed 's/^# //' 39 } 40 41 htmltitle() { 42 grep '<!--TITLE: ' "$1" | sed 's/^<!--TITLE: //' | sed 's/-->$//' 43 } 44 45 makeblogindexandfeed() { 46 mkdir -p http/blog 47 bf=http/blog/index.html 48 ff=http/blog/feed.xml 49 50 sed "s/TITLE/Blog/" < top.html > "$bf" 51 { echo '<h1 id="blog">Blog</h1>'; 52 echo '<table id="blog">'; 53 echo '<a href="../series">Blog series</a> - '; 54 echo '<a href="feed.xml">RSS feed</a>'; } >> "$bf" 55 56 cp feed-top.xml "$ff" 57 58 for i in $(ls src/blog | sort -r); do 59 [ -d "src/blog/$i" ] || continue 60 61 f="src/blog/$i/*.md" 62 d="$(echo "$i" | grep -oE '^[0-9]{4}-[0-9]{2}-[0-9]{2}')" 63 t="$(head -n 1 $f | sed 's/# //')" 64 65 thisyear="$(echo "$d" | sed 's/-.*//')" 66 if [ "$thisyear" != "$lastyear" ]; then 67 echo "<tr><td><h2>$thisyear</h2></td></tr>" >> "$bf" 68 lastyear=$thisyear 69 fi 70 71 { echo "<tr><td>$d</td>"; 72 echo "<td><a href=\"$i\">$t</a></td></tr>"; } >> "$bf" 73 74 { echo "<item>"; 75 echo "<title>$t</title>"; 76 echo "<link>https://sebastiano.tronto.net/blog/$i</link>"; 77 echo "<description>$t</description>"; 78 echo "<pubDate>$d</pubDate>"; 79 echo "</item>"; 80 echo ""; } >> $ff 81 done 82 83 echo '</table>' >> "$bf" 84 cat bottom.html >> "$bf" 85 86 { echo ""; echo "</channel>"; echo "</rss>"; } >> "$ff" 87 } 88 89 makeblogindexandfeed 90 recursivebuild src