git.mcksp
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
#!/bin/bash
domain="mcksp.com"
tmpl=$(cat template.html)

compile() {
    webpage="${tmpl//<!--include:title-->/$2}"
    echo "${webpage//<!--include:main-->/$1}"
}

item() {
    echo "<div class='item'><span class='date'>$2</span><a href='/$1'>$3</a></div>"
}

rssdate() {
    LC_TIME=en_US date -d "$1 12:00" '+%a, %d %b %Y %H:%M:%S %z'
}

rssitem() {
    echo "<!--$1--><item><guid>https://$domain/$3</guid><title>$2</title><link>https://$domain/$3</link><description>$4</description><pubDate>$(rssdate "$1")</pubDate></item>"
}

htmlencode() {
    html=${1//&/&amp;}; html=${html//</&lt;}; html=${html//>/&gt;}; html=${html//'"'/&quot;}; html=${html//$'\n'/&\#10;}; echo "$html"
}

if [ $# -ne 0 ]; then
    variables=$(echo -e "<!--title:$2-->\n<!--date:$(date +%Y/%m/%d)-->")
    echo "${tmpl//<!--include:main-->/$variables}" > "$1/$2.html"
fi

rsslist=()
for dir in blog tech .; do
    list=()
    for page in "$dir"/*.html; do
        [[ -f "$page" && "$page" != "./template.html" ]] || continue
        content=$(sed '1,/<\/header>/d;/<footer>/,$d' "$page")
        timestamp="$(grep -m1 -oP "date:\K.*?(?=-->)" <<< "$content")"
        title="$(grep -m1 -oP "title:\K.*?(?=-->)" <<< "$content")"
        compile "$content" "$title | $domain" > "$page"
        [[ -z "$timestamp" ]] || list+=("$(item "$page" "$timestamp" "$title")")
        [[ -z "$timestamp" ]] || rsslist+=("$(rssitem "$timestamp" "$title" "$page" "$(htmlencode "$content")")")
    done
    [[ "$dir" == "." ]] || compile "$(printf "%s\n" "${list[@]}" | sort -r)" "$dir | $domain" > "${dir}/index.html"
done
echo "<rss version=\"2.0\"><channel><title>$domain</title><link>https://$domain</link><description>$domain</description>
$(printf "%s\n" "${rsslist[@]}" | sort -r | head -n10)
</channel></rss>" > "rss.xml"