build (3264B)
1 #!/bin/sh 2 3 subtitle=" | Cem's Website" 4 copyright="2019-$(date +%Y) Cem Keylan" 5 6 hr() { 7 # Shell function to create 80 dash characters on a file without actually 8 # having to do it manually, and without seq. Unless your shell doesn't 9 # have printf as a built-in (highly-unlikely), this is the pure shell 10 # method. 11 i=0 char=${1:--} hr= 12 while ! [ "$i" -eq 80 ]; do 13 hr="$hr$char"; i=$(( i + 1 )) 14 done 15 printf '\n%s\n\n' "$hr" 16 } 17 18 gettitle() { 19 unset title 20 case "$(sed -n 2p "$1")" in =*=|-*-) title="$(sed -n 1p "$1")"; esac 21 file=${1##*/} file=${file%.*} 22 printf '%s\n' "${title:-$file}${2:+$subtitle}" 23 } 24 25 genpages() ( 26 cd src || return 1 27 find . | while read -r file; do 28 29 [ -d "$file" ] && { 30 mkdir -p "../docs/${file#./}" 31 continue 32 } 33 34 case "$file" in 35 *.md|*.txt) 36 tohtml "$file" > "../docs/${file%.*}.html" 37 cp "$file" "../docs/${file%.*}.txt" 38 ;; 39 *) 40 cp "$file" "../docs/$file" 41 esac 42 done 43 ) 44 45 getcopyright() { printf 'Copyright \\© %s\n' "$copyright" ;} 46 47 tohtml() { 48 srcfile=${1#.} srcfile=${srcfile%.*}.txt 49 markdown -f +fencedcode "$1" | 50 sed '/{{ CONTENT }}/r /dev/stdin' "$ROOT/template.html" | 51 sed '/{{ CONTENT }}/d' | 52 sed "s#{{ TITLE }}#$(gettitle "$1" html)#" | 53 sed "s#{{ COPYRIGHT }}#$(getcopyright)#" | 54 sed "s|{{ SRC }}|$srcfile|" 55 } 56 57 site_index() { 58 cp index.md src/index.md 59 # Okay. 60 # shellcheck disable=2030 61 find src/blog -type f | sort -r | while read -r post; do 62 hr \* >> src/index.md 63 postdate="${post##*/}" postdate="${postdate%%-*}" 64 postloc="${post#src}" postloc="${postloc%.*}.html" 65 sed -e "2a[Permalink]($postloc)" -e 2a -e "2aDate: $(date --date="$postdate" "+%b %d %Y")" -e "2a" "$post" >> src/index.md 66 done 67 } 68 69 blog_index() { 70 hr >/dev/null 71 printf '%s\n%s\n\n' "Blog Index" "$hr" > src/blog.md 72 find src/blog -type f | sort -r | while read -r post; do 73 # Okay. 74 # shellcheck disable=2030,2031 75 postdate="${post##*/}" posthtml="${postdate%.*}" postdate="${postdate%%-*}" 76 printf '* %s - [%s](%s)\n' \ 77 "$(date --date="$postdate" "+%b %d %Y")" \ 78 "$(gettitle "$post")" \ 79 "/blog/${posthtml}.html" 80 done >> src/blog.md 81 } 82 83 genrss() { 84 find src/blog -type f | sort -r | while read -r post; do 85 # OKAY! 86 # shellcheck disable=2031 87 { postdate="${post##*/}" postdate="${postdate%%-*}" 88 postloc="${post##src/blog/}" postloc=${postloc%.*}.html ;} 89 cat <<EOF 90 <item> 91 <title>$(gettitle "$post")</title> 92 <pubDate>$(date --date="$postdate" +%a,\ %d\ %b\ %Y)</pubDate> 93 <dc:creator>Cem Keylan</dc:creator> 94 <link>https://cemkeylan.com/blog/$postloc</link> 95 <description>$(markdown -f cdata -f fencedcode "$post")</description> 96 </item> 97 EOF 98 done | 99 sed '/{{ CONTENT }}/r /dev/stdin' "$ROOT/template.xml" | 100 sed '/{{ CONTENT }}/d' | 101 sed "s|{{ DATE }}|$(date -u "+%a %b %d %Y %H:00")|" 102 } 103 104 main() { 105 ROOT=$PWD 106 107 find docs -type f -exec rm -f {} + 108 109 site_index; blog_index 110 genrss > src/rss.xml 111 112 genpages 113 } 114 115 main "$@"