1 #!/bin/sh 2 3 # The most bloated, non-mantainable, non-readable static site generator 4 # by: Martin Kloeckner - https://kloeckner.com.ar 5 # Dependencies: sed, grep, lowdown. 6 7 # Template: the converted html text is placed on a copy of the template 8 # containing $body$ on it, $body$ then gets deleted. 9 # Also the template should contain an $article-title$, $article-date$ and 10 # $pagetitle$ 11 12 # The article title is considered to be the first h1 header found 13 # in the markdown file 14 15 div_article_title_w_logo() { 16 logo_src=$(echo $1 | grep -zoP '(?<=src=\")(.*?)(?=\")' | tr -d '\0') 17 logo_alt=$(echo $1 | grep -zoP '(?<=alt=\")(.*?)(?=\")' | tr -d '\0') 18 logo_title=$(echo $1 | grep -zoP '(?<=title=\")(.*?)(?=\")' | tr -d '\0') 19 h1_title=$(echo $2 | grep -zoP '(?<=<h1 id=article-title>)(.*?)(?=</h1>)' |\ 20 tr -d '\0') 21 } 22 23 usage() { 24 echo "usage: $0 -i <input_file> -t <template_file>" 25 } 26 27 missing_operand() { 28 echo "$0: missing operands" 29 usage 30 } 31 32 check_opt() { 33 local opt OPTIND 34 while getopts ":i:t:d:" opt; do 35 case $opt in 36 i) input="$OPTARG";; 37 t) templ="$OPTARG";; 38 d) dest_dir="$OPTARG";; 39 \?) printf "%s\n\n" "error: flag not found" && usage && exit 1;; 40 esac 41 done 42 43 [ $OPTIND -eq 1 ] && missing_operand && exit 2 44 shift "$((OPTIND-1))" 45 46 [ -z "$input" ] || [ ! -e "$input" ] &&\ 47 echo "error: no input file" && exit 1 48 [ -z "$templ" ] || [ ! -e "$templ" ] &&\ 49 echo "error: no template fshellile" && exit 1 50 [ -z "$dest_dir" ] && dest_dir="." 51 } 52 53 check_opt $@ 54 55 # deprecated format 56 # title=$(cat $input | head -n 3 | grep -oP '(?<=% title: \")(.*?)(?=\")') 57 # date=$(cat $input | head -n 3 | grep -oP '(?<=% date: \")(.*?)(?=\")') 58 59 title=$(cat $input | sed '/^%%/,/^%%/!d' | grep -oP '(?<=title: \")(.*?)(?=\")') 60 date=$(cat $input | sed '/^%%/,/^%%/!d' | grep -oP '(?<=date: \")(.*?)(?=\")') 61 pagetitle="Martin Klöckner's Webpage" 62 lang="en" 63 generator="Shell script" 64 template="$templ" 65 filename="$(basename $input | sed 's/\.[^.]*$//')" 66 last_update="$(date -r $input '+%d-%b-%Y')" 67 68 mkdir -p $dest_dir &> /dev/null 69 70 echo "title: $title" 71 echo "date: $date" 72 73 [ -z "$title" ] || [ -z "$date" ] \ 74 && echo "error: no metadata found on file $input" && exit 1 75 76 # generate body (skips lines starting with `%`, they're considered metadata) 77 # sed '/^% /d' $input | \ 78 # lowdown --html-no-head-ids --html-no-escapehtml --html-no-owasp > body.html 79 # sed '/^%%/,/^%%/' 80 sed '/^%%/,/^%%/d' $input |\ 81 lowdown --html-no-head-ids --html-no-escapehtml --html-no-owasp > body.html 82 83 # puts id to <h1> tag and adds paragraph next to it with the article-date 84 sed -i -e 's/<h1>/<h1 id=article-title>/g' \ 85 -e "s/<\/h1>/<\/h1><p class=\"article-date\">$date (last update $last_update)<\/p>/"\ 86 body.html 87 88 sed -e "s/\$article-title\\$/$title/" -e "s/\$article-date\\$/$date/" \ 89 -e "s/\$pagetitle\\$/$pagetitle/" -e '/\$body\$/r./body.html' \ 90 -e "s/\$lang\\$/$lang/" -e "s/\$generator\\$/$generator/" \ 91 -e '/\$body\$/d' $template > "$dest_dir"/"$filename".html 92 93 logo_line=$(grep -zoP '<img.*?("article-icon").*?>' "$dest_dir"/"$filename".html | tr -d '\0') 94 title_line=$(grep -P '<h1.*?(article-title).*?>' "$dest_dir"/"$filename".html | tr -d '\0') 95 96 insert_div_article_title_w_logo() { 97 logo_line="$1" 98 title_line="$2" 99 100 div_article_title_w_logo "$logo_line" "$title_line" 101 102 sed -i -e "s^$title_line^\ 103 \ <div id=\"article-title-with-icon\">\ 104 \ <div id=\"article-icon\">\ 105 \ <img src=\"$logo_src\" title=\"$logo_title\" alt=\"$logo_alt\">\ 106 \ <\/div>\ 107 \ <h1 id=\"article-title\">$h1_title<\/h1>\ 108 \ <\/div>\ 109 \ <p class=\"article-date\">$date (last update $last_update)</p>^"\ 110 "$dest_dir"/"$filename".html 111 112 sed -i -E '/(^<p>).*?(article-icon).*?<\/p>/d' "$dest_dir"/"$filename".html 113 } 114 115 [ ! -z "$logo_line" ] && \ 116 insert_div_article_title_w_logo "$logo_line" "$title_line" 117 118 rm body.html &> /dev/null 119 120 ./scripts/syntax-highlight "$dest_dir"/"$filename".html > tmp.html 121 122 mv tmp.html "$dest_dir"/"$filename".html 123 124 echo "==> "$filename".html generated succesfully" 125 echo ""