treesite
path: root/build.sh

326 lines, 11270 bytes

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
#!/usr/bin/env bash
OUTDIR=__site
OUTTMP=${OUTDIR}_tmp
IDENTITY=("css/*" "js/*" "lyrics/*" "argentina/**" "static/**" "robots.txt" "favicon.ico")
POSTS=posts/*
INDEX="index.html"
TEMPLATES=templates/*
POSTLIST="templates/post-list.html"
# Read-only source view served at /git/. Only these paths (files, or
# directories searched recursively) are published; everything else in the repo
# (cv/, server/, static/, ...) is not.
GIT_ALLOW=("about.markdown" "build.sh" "index.html" "Makefile" "README.md"
           "robots.txt" ".gitignore" "tidy.conf" "diff.sh"
           "css" "js" "posts" "templates" "lyrics" "argentina")

TEMPLATE="templates/default.html"
POST="templates/post.html"

P=pandoc
PM="pandoc --mathjax"

postprocess_html() {
  sed -i -e 's/href="\/\(.*\)"/href=".\/\1"/g' $1
  sed -i -e 's/script async/script async="async"/g' $1
  for ((i=1; i<=10; i++))
  do
    sed -i -e "s/title=\"$i\"/data-line-number=\"$i\"/g" $1
  done
}

no_diff_hacks() {
  sed -i -e 's/^\(.*\)post_link\(.*\)<br>\(.*\)$/\1post_link\2<!-- br-->\3/g' \
    $OUTDIR/index.html
  sed -i -e 's/<span class="ot">=<\/span>/<span class="fu">=<\/span>/g' \
    $OUTDIR/propositions-as-types.html
  sed -i -e 's/ i\.e\.[^,]/ i.e. /' $OUTDIR/propositions-as-types.html
  sed -i -e 's/<span class="fu">id<\/span>/id/' \
    $OUTDIR/propositions-as-types.html
  sed -i -e 's/href="\//href=".\//g' $OUTDIR/about.html
  # twice to resolve all appearances.
  sed -i -e 's/^\(.*\)sourceLine\(.*\)\\\\\(.*\)$/\1sourceLine\2\\\3/' \
    $OUTDIR/propositions-as-types.html
  sed -i -e 's/^\(.*\)sourceLine\(.*\)\\\\\(.*\)$/\1sourceLine\2\\\3/' \
    $OUTDIR/propositions-as-types.html
}

remove_yaml_header() {
  sed -i '0,/^---$/d' $1
  sed -i '0,/^---$/d' $1
}

get_post_date() {
  date_str=$(basename $1 .markdown | cut -c-10)
  date -d "$date_str" '+%B %e, %Y' 2>/dev/null || date -j -f "%Y-%m-%d" "$date_str" '+%B %e, %Y'
}
# xargs echo -n trims whitespace.
get_post_title() {
  head $1 | grep "title:" | sed -e "s/title://" | xargs echo -n
}
get_post_url() {
  head $1 | grep "url:" | sed -e "s/url://" | xargs echo -n
}

html_escape() {
  sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g'
}

# Writes $OUTDIR/git/ in the style of cgit: tree pages for every directory
# (/git/, /git/posts/, ...) and blob pages with line numbers (/git/<path>.html)
# for the allowlisted files. Binary files are skipped.
git_page_head() {  # $1 title, $2 path ("" for root), $3 "file" if $2 is a file
  cat <<HTML
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>$1</title><link rel="stylesheet" href="/git/git.css"></head><body>
<table class="top"><tr><td class="logo"><a href="/git/">gciruelos.com</a></td></tr></table>
<table class="tabs"><tr><td><a class="active" href="/git/">tree</a><a href="/">site</a></td></tr></table>
<div class="content">
HTML
  local path="" part acc="" crumbs='<a href="/git/">root</a>'
  if [ -n "$2" ]; then
    IFS=/ read -ra parts <<< "$2"
    for part in "${parts[@]}"; do
      acc+="/$part"
      crumbs+="/<a href=\"/git${acc}/\">$part</a>"
    done
    [ "$3" = file ] && crumbs=${crumbs%\<a href=*}$part
  fi
  echo "<div class=\"path\">path: $crumbs</div>"
}

git_write_css() {
  cat > $1 <<'CSS'
body{font:10pt/1.4 Verdana,sans-serif;color:#333;background:#fff;margin:0;padding:4px}
a{color:#00c;text-decoration:none}a:hover{text-decoration:underline}
table{border-collapse:collapse}
.top{width:100%;padding:0}
.top td{padding:4px 0}
.logo{width:1%;white-space:nowrap;font-size:140%;font-weight:bold}
.logo a{color:#000}
.desc{padding-left:1.5em!important;color:#777;vertical-align:bottom}
.tabs{width:100%;border-bottom:3px solid #ccc;margin-top:6px}
.tabs td{padding:0 0 0 1em}
.tabs a{display:inline-block;padding:.1em .75em;color:#777}
.tabs a.active{color:#000;background:#ccc}
.tabs a:hover{background:#eee;text-decoration:none}
.content{padding:1em 0;border-bottom:3px solid #ccc;min-height:12em}
.path{margin:0 0 .8em;color:#777;font-size:90%}
.path a{color:#00c}
table.list{width:100%}
table.list th{text-align:left;background:#eee;border-bottom:1px solid #ccc;padding:2px 1em 2px 0;font-weight:normal;color:#333}
table.list td{padding:1px 1em 1px 0;border-bottom:1px solid #f2f2f2}
table.list tr:hover td{background:#f8f8f8}
.mode{font-family:monospace;color:#777}
.size{text-align:right}
table.list th.size{text-align:right}
.blob{width:100%;margin-top:.3em}
.blob td{vertical-align:top;padding:0}
.blob pre{margin:0;font:10pt/1.35 monospace;tab-size:4}
.nums{width:1%;text-align:right;padding-right:1em!important;color:#777;user-select:none}
.nums a{color:#777;display:block}
.nums a:hover{color:#000;text-decoration:none}
.code pre{overflow-x:auto}
.meta{color:#777;font-size:90%;margin:0 0 .5em}
.foot{padding-top:.5em;text-align:right;color:#777;font-size:80%}
CSS
}

run_git_view() {
  echo "Resolving /git/..."
  local out=$OUTDIR/git files=() dirs=() f d a n size mode
  mkdir -p $out
  git_write_css $out/git.css
  for a in "${GIT_ALLOW[@]}"
  do
    if [ -d "$a" ]; then
      while IFS= read -r f; do files+=("$f"); done < <(find "$a" -type f | sort)
    elif [ -f "$a" ]; then
      files+=("$a")
    fi
  done
  # Keep only text files.
  local text=()
  for f in "${files[@]}"; do grep -Iq . "$f" && text+=("$f"); done
  files=()
  while IFS= read -r f; do files+=("$f"); done < <(printf '%s\n' "${text[@]}" | LC_ALL=C sort)

  # Blob pages.
  for f in "${files[@]}"
  do
    n=$(wc -l < "$f"); size=$(wc -c < "$f")
    mkdir -p "$out/$(dirname "$f")"
    {
      git_page_head "$f" "$f" file
      echo "<p class=\"meta\">$n lines, $size bytes</p>"
      echo '<table class="blob"><tr><td class="nums"><pre>'
      seq 1 "$(( n > 0 ? n : 1 ))" | sed 's|.*|<a id="L&" href="#L&">&</a>|' | tr -d '\n'
      echo
      echo '</pre></td><td class="code"><pre>'
      html_escape < "$f"
      echo '</pre></td></tr></table></div><div class="foot">gciruelos.com</div></body></html>'
    } > "$out/$f.html"
  done

  # Tree pages: the root plus every ancestor directory of an allowed file.
  dirs=("")
  while IFS= read -r d; do dirs+=("$d"); done < <(
    for f in "${files[@]}"; do d=$(dirname "$f"); while [ "$d" != "." ]; do echo "$d"; d=$(dirname "$d"); done; done | sort -u)
  for d in "${dirs[@]}"
  do
    mkdir -p "$out/$d"
    {
      git_page_head "${d:-root}" "$d"
      echo '<table class="list"><tr><th>Mode</th><th>Name</th><th class="size">Size</th></tr>'
      # Immediate subdirectories first, then files.
      for sub in "${dirs[@]}"; do
        [ -z "$sub" ] && continue
        [ "$(dirname "$sub")" = "${d:-.}" ] || continue
        echo "<tr><td class=\"mode\">drwxr-xr-x</td><td><a href=\"/git/$sub/\">$(basename "$sub")/</a></td><td class=\"size\"></td></tr>"
      done
      for f in "${files[@]}"; do
        [ "$(dirname "$f")" = "${d:-.}" ] || continue
        mode=$(stat -c %A "$f" 2>/dev/null || stat -f %Sp "$f")
        size=$(wc -c < "$f")
        echo "<tr><td class=\"mode\">$mode</td><td><a href=\"/git/$f.html\">$(basename "$f")</a></td><td class=\"size\">$size</td></tr>"
      done
      echo '</table></div><div class="foot">gciruelos.com</div></body></html>'
    } > "$out/${d:+$d/}index.html"
  done
}

run_build() {
  rm -r $OUTDIR
  rm -r $OUTTMP
  mkdir -p $OUTDIR
  mkdir -p $OUTTMP

  # Resolve files that are copied verbatim.
  echo "Resolving verbatim files..."
  for pattern in "${IDENTITY[@]}"
  do
    for f in $pattern
    do
      mkdir -p "$OUTDIR/$(dirname "$f")" && cp -R "$f" "$OUTDIR/$f";
    done
  done

  # Resolve about page.
  echo "Resolving about..."
  $P --template=$TEMPLATE -o $OUTDIR/about.html ./about.markdown
  postprocess_html $OUTDIR/about.html

  run_git_view

  # Resolve posts.
  echo "Resolving posts..."
  for f in $POSTS
  do
    echo "Resolving $f..."
    post_url=$(get_post_url $f)
    post_date=$(get_post_date $f)
    outtmp=$OUTTMP/$(basename $f .markdown).1.markdown
    echo "---" > $outtmp
    echo "title: $(get_post_title $f)" >> $outtmp
    echo "url: $post_url" >> $outtmp
    echo "date: $post_date" >> $outtmp
    echo "---" >> $outtmp
    $PM --metadata date="$post_date" --template=$POST -o- $f >> $outtmp
    sed -i 's/\\/\\\\/g' $outtmp
    sed -i 's/}_{/}\\_\{/g' $outtmp
    sed -e 's/.*\$body\$.*/$body$/' $TEMPLATE > $OUTTMP/default.4.html
    $P --template=$OUTTMP/default.4.html -o $OUTDIR/$post_url $outtmp
    postprocess_html $OUTDIR/$post_url
  done

  # Resolve index.
  echo "Resolving index..."
  index_tmp=$OUTTMP/index.1.html
  sed -e 's/\$partial("\(.*\)")\$/$\1()$/' $INDEX > $index_tmp
  remove_yaml_header $index_tmp  # unneeded
  cp templates/post-list.html $OUTTMP/post-list.1.html
  for field in url title date teaser
  do
    sed -i "s/\\\$$field\\$/\$posts.$field\$/"  $OUTTMP/post-list.1.html
  done
  index_title=$(get_post_title $INDEX)
  post_list=$(basename $POSTLIST .html);
  post_list_yaml=$OUTTMP/$post_list.2.yaml
  edited_default=$OUTTMP/default.1.html
  cp $TEMPLATE $edited_default
  sed -i -e "/\\\$body\\$/{r $OUTTMP/post-list.1.html" -e 'd}' $edited_default
  echo "title: $index_title" >> $post_list_yaml
  echo "posts:" >> $post_list_yaml
  REVPOSTS=()
  for f in $POSTS
  do
    echo "Resolving index.html teaser for $f..."
    post_url=$(head $f | grep "url:" | sed -e "s/url://" | xargs echo -n)
    post_date=$(get_post_date $f)
    teasermd=$OUTTMP/post_url.2.markdown
    teaseryaml=$OUTTMP/$post_url.1.yaml
    cp $f $teasermd
    remove_yaml_header $teasermd
    sed -i '/<!--more-->/q' $teasermd
    sed -i 's/<!--more-->//' $teasermd
    sed -i '1d' $teasermd
    sed -i -e 's/^/    /'  $teasermd
    echo "- title: $(get_post_title $f)" >> $teaseryaml
    echo "  url: $post_url" >> $teaseryaml
    echo "  date: $post_date" >> $teaseryaml
    date_str=$(basename $f .markdown | cut -c-10)
    date822=$(date -d "$date_str" -u +"%a, %d %b %Y %H:%M:%S GMT" 2>/dev/null || date -j -u -f "%Y-%m-%d" "$date_str" +"%a, %d %b %Y %H:%M:%S GMT")
    echo "  date822: $date822" >> $teaseryaml
    echo "  teaser: |" >> $teaseryaml
    sed -e 's/_/\&#95;/g' $teasermd >> $teaseryaml
    echo "      " >> $teaseryaml
    REVPOSTS[${#REVPOSTS[@]}]=$teaseryaml
  done
  for ((i=${#REVPOSTS[@]}-1; i>=0; i--))
  do
    cat ${REVPOSTS[i]} >> $post_list_yaml
  done
  $PM --metadata-file=$post_list_yaml --template=$edited_default -o- /dev/null \
    | sed -e 's/<\/ br>/<br>/' > $OUTDIR/$INDEX
  postprocess_html $OUTDIR/$INDEX

  # Resolve feed.xml.
  echo "Resolving feed.xml"
  cp templates/feed.xml $OUTTMP/feed.1.xml
  for field in url title date822 teaser
  do
    sed -i "s/\\\$$field\\$/\$posts.$field\$/"  $OUTTMP/feed.1.xml
  done
  $PM --metadata-file=$post_list_yaml --template=$OUTTMP/feed.1.xml -o- /dev/null > $OUTDIR/feed.xml

  no_diff_hacks
  echo "Done!"
}

run_tidy() {
  tidy -config tidy.conf $1 | sed -e "s/^[ \t]*//"
}

run_diff () {
  echo "Diffing..."
  for new_f in $OUTDIR/**.html
  do
    echo "Diffing $new_f..."
    diff -u <(run_tidy ${new_f:1}) <(run_tidy $new_f)
  done
}

if [ $1 == "build" ];
then
  run_build
elif [ $1 == "watch" ];
then
  run_build
  python -m http.server 9999 --directory $OUTDIR
elif [ $1 == "diff" ];
then
  run_build
  run_diff
else
  echo "Nothing to do"
fi
gciruelos.com