make (4247B)
1 #!/bin/sh -e 2 # 3 # Simple static site builder. 4 5 indir=source 6 resdir=resource 7 outdir=public 8 template=template.html 9 10 txt2html() { 11 # Transform plain-text input into HTML and insert it into the template. 12 # Right now this only does some URL transformations. 13 14 # Convert markdown links to html 15 # TODO: think of & or ^ is better 16 # sed -E 's/\[([^]]+)\]\(([^)]+)\)/\&\/<a href="\2">\1<\/a>/g' | 17 # Convert markdown code blocks into span 18 # possibly later insert language? 19 # sed -E 's|^```\[(.*?)$|<pre class=code>|g' | 20 # sed -E 's|^\]```$|</pre>|g' | 21 # sed -E 's|`([^`]+)`|<span class=inlcode> \1 </span>|g' | 22 23 # Convert all plain-text links to HTML links (<a href="X">X</a>). 24 sed -E "s|([^\"\'\>=])(http[s]?://[^[:space:]]*)|\1<a href=\2>\2</a>|g" | 25 sed -E "s|^(http[s]?://[^[:space:]]*)|<a href=\1>\1</a>|g" | 26 27 # Convert @/words to relative HTML links. 28 # Convert %/words to absolute wiki links. 29 # Convert #/words to absolute HTML links. 30 # Convert $/words to GitHub URLs. 31 # Convert ~/words to sourcehut git URLs. 32 sed -E "s|(@/)([^ \)]*)|\1<a href=$1/\2>\2</a>|g" | 33 sed -E "s|(%/)([^ \)]*)|\1<a href=/w/\2>\2</a>|g" | 34 sed -E "s|(\\#/)([^ \)]*)|\1<a href=/\2>\2</a>|g" | 35 sed -E "s|(\\$/)([^ \)]*)|\1<a href=https://github.com/\2>\2</a>|g" | 36 sed -E "s|(\\~/)([^ \)]*)|\1<a href=https://git.sr.ht/~\2>\2</a>|g" | 37 38 # Convert markdown links to html 39 sed -E 's/\[([^]]+)\]\(([^)]+)\)/<a href="\2">\1<\/a>/g' | 40 41 # Convert [0-9] into HTML links. 42 sed -E "s|^([ -]*)\[([0-9\.]*)\]|\1<span id=\2>[\2]</span>|g" | 43 sed -E "s|([^\"#])\[([0-9\.]*)\]|\1[<a class=t href=#\2>\2</a>]|g" | 44 45 # Insert the page into the template. 46 sed -E '/%%CONTENT%%/r /dev/stdin' $template | 47 sed -E '/%%CONTENT%%/d' | 48 49 # Insert the page path into the source URL. 50 sed -E "s|%%TITLE%%|${2:-home}|g" | 51 sed -E "s|%%FILE%%|$1/${2:-index}|g" | 52 sed -E "s|%%DIR%%|${1:-/}|g" 53 } 54 55 page() { 56 pp=${1%/*} 57 title=${1##*/} 58 title=${title%%.wht} 59 title=${title##index} 60 61 mkdir -p "$outdir/$pp" 62 63 # Generate HTML from wht files. 64 case $1 in *.wht) 65 txt2html "${pp#.}" "$title" < "$indir/$1" > "$outdir/${1%%.wht}.html" 66 esac 67 68 # cp -Lf "$indir/$1" "$outdir/$1" 69 } 70 71 pkg() { 72 rm -rf $indir/wiki/pkg 73 mkdir -p $indir/wiki/pkg 74 75 # Get git log of all packages. 76 git -C "$KISS_REPO" log \ 77 --date=short \ 78 --pretty="format:%cd <a href=\"$url/commit/%H\">%h</a> %s" \ 79 > .log 80 81 for pkg in "$KISS_REPO"/*/*/; do 82 pkg=${pkg%%/} 83 rep=${pkg%/*} 84 rep=${rep##*/} 85 86 while IFS= read -r line || [ "$line" ]; do case $line in 87 # Insert package version. 88 "${pkg##*/}") 89 read -r ver _ < "$pkg/version" 90 printf "%s%$((80 - ${#line}))s\\n" "$line" "version $ver" | 91 sed "s $ver <a href=$url/tree/master/$rep/$line>$ver</a> " 92 ;; 93 94 # Insert new entries into index. 95 "* References"*) 96 _i=${line##*\[0} 97 _i=${_i#0} 98 _i=${_i%%']'} 99 printf '* History ................................................................ [%03d]\n' "$_i" 100 printf '%s[%03d]\n' "${line%'['*}" "$((_i + 1))" 101 ;; 102 103 # Insert new entries into page. 104 "["???"] References"*) 105 printf '[%03d] History\n' "$_i" 106 printf '________________________________________________________________________________\n\n' 107 printf '<span style="display:block;max-height:177px;overflow:hidden scroll">' 108 grep -F "${pkg##*/}: " .log 109 printf '</span>\n\n[%03d] References\n' "$((_i + 1))" 110 ;; 111 112 *) 113 printf '%s\n' "$line" 114 ;; 115 esac done < "$pkg/README" > "$indir/wiki/pkg/${pkg##*/}.wht" || : 116 117 done 118 119 rm -f .log 120 } 121 122 main() { 123 rm -rf $outdir && mkdir -p $outdir 124 url=https://github.com/kisslinux/repo 125 126 # [ -z "$KISS_REPO" ] || pkg 127 128 (cd $indir && find . ! -type d) | 129 130 while read -r page; do 131 printf '%s\n' "CC $page" 132 page "$page" 133 done 134 135 cp $resdir/* "$outdir" 136 } 137 138 main "$@"