fusch.online

git / lists / casts

git.fusch.online/planner

Generate static pages from a list of links
git clone git://git.fusch.online/planner
Readme / Log / Files / Issues

 planner (4846B)

    1 #!/usr/bin/env bash
    2 # SPDX-License-Identifier: MIT
    3 
    4 tmp=$(mktemp)
    5 site=planner_html
    6 offline=false
    7 dateformat="--rfc-3339=seconds"
    8 builddate=$(date "$dateformat")
    9 sitetitle="planner"
   10 db=./planner.db
   11 
   12 config=./config
   13 
   14 xml_escape() {
   15     sed -e 's/&/\&amp\;/g; s/</\&lt\;/g; s/>/\&gt\;/g;' <<< "$1"
   16 }
   17 
   18 print_html_head() {
   19     cat <<EOF
   20 <!DOCTYPE html>
   21 <html lang="en">
   22   <head>
   23     <meta charset="utf-8">
   24     <title>$sitetitle/$1</title>
   25     <link rel="stylesheet" href="./style.css">
   26   </head>
   27   <body>
   28   <div class="planner">
   29 EOF
   30 
   31     sed -e "s/{{{ tag }}}/$1/g" \
   32         -e "s/{{{ head }}}/planner/g" ./header.html  
   33 }
   34 
   35 print_html_tail() {
   36     sed -e "s_{{{ rss }}}_$PLANNER_URL/atom.xml_" ./footer.html | grep -v "{{{ .* }}}"
   37 
   38     cat << EOF
   39   </div>
   40   </body>
   41 </html>
   42 EOF
   43 }
   44 
   45 print_html_link_head() {
   46     cat << EOF
   47     <div class="entry">
   48       <a class="$3" href="$2">$1</a><br>
   49       <a class="link" href="$2">$2</a><br>
   50 EOF
   51 }
   52 
   53 print_html_link_tail() {
   54     printf "    </div>\n"
   55 }
   56 
   57 print_html_link_tag() {
   58     printf "        <a class=\"tag\" href=\"$PLANNER_URL/%s\">#%s</a>\n" "./$1" "$1"
   59 }
   60 
   61 print_atom_head() {
   62     cat << EOF
   63 <?xml version="1.0" encoding="UTF-8" ?>
   64 <feed xmlns="http://www.w3.org/2005/Atom">
   65 <link rel="self" href="$PLANNER_URL/atom.xml"></link>
   66 <title>$sitetitle</title>
   67 <id>$PLANNER_URL</id>
   68 <updated>$builddate</updated>
   69 EOF
   70 }
   71 
   72 print_atom_tail() {
   73     cat << EOF
   74     </feed>
   75 EOF
   76 }
   77 
   78 print_atom_link() {
   79     cat << EOF
   80 <entry>
   81     <title type="text">$(xml_escape "$1")</title>
   82     <link href="$2"></link>
   83     <id>$2</id>
   84     <author>
   85         <name>$PLANNER_NAME</name>
   86         <email>$PLANNER_EMAIL</email>
   87     </author>
   88     <published>$3</published>
   89     <updated>$3</updated>
   90 </entry>
   91 EOF
   92 }
   93 
   94 print_atom_tail() {
   95     printf "</feed>\n"
   96 }
   97 
   98 
   99 # main
  100 [[ -z $1 ]] && exit
  101 
  102 mkdir -p $site
  103 touch $db
  104 rm -f $site/*.html $site/atom.xml
  105 
  106 [[ -f $config ]] && source "$config"
  107 
  108 [[ $1 == -n ]] && {
  109     offline=true
  110     shift
  111 }
  112 
  113 print_html_head "" > $site/index.html
  114 print_atom_head > $site/atom.xml
  115 
  116 echo "<h1>${PLANNER_URL##https://}</h1>" >> $site/index.html
  117 
  118 [[ -f ./index.md ]] && {
  119     echo "<div class=\"planner-desc\">"
  120     markdown ./index.md
  121     echo "</div>"
  122 } >> $site/index.html
  123 
  124 true > $site/bookmarks.txt
  125 
  126 sort -u -r $1 | while read -r line; do
  127     link=$(cut -d, -f2 <<< "$line")
  128     tags=$(cut -s -d, -f3 <<< "$line")
  129     title=$(cut -s -d, -f4- <<< "$line")
  130     class="linktitle read"
  131 
  132     date=$(grep "$link" $db | cut -d, -f1)
  133     [[ -z $date ]] && date=$builddate
  134 
  135     if [[ -z $title ]]; then
  136         title=$(grep "$link" $db | cut -d, -f4-)
  137 
  138         [[ -z $title && $offline == false ]] && {
  139             printf "getting title for %s...\n" "$link" >&2
  140 
  141             title=$(curl -L -s "$link" \
  142                 | grep "<title>" \
  143                 | head -1 \
  144                 | sed -e 's/^.*<title>//' -e 's/<\/title>.*$//')
  145 
  146             printf ",%s\n" "$title" >&2
  147         }
  148 
  149         [[ -z $title ]] && {
  150             title="$(sed -e 's/http.:\/\///' -e 's/\/.*$//' <<< "$link")"
  151             title="⛷ $title"
  152             printf ",%s\n" "$title" >&2
  153         }
  154     fi
  155 
  156     printf "%s,%s,%s,%s\n" "$date" "$link" "$tags" "$title" >> "$tmp"
  157 
  158     grep -q "read" <<< "$tags" && {
  159         title="⛟  $title"
  160         class="linktitle unread"
  161     }
  162 
  163     grep -q "$link" $db || {
  164         echo "$title"
  165         class="new $class"
  166     }
  167 
  168     true > $site/link_build.html
  169 
  170     print_html_link_head "$title" "$link" "$class" >> $site/link_build.html
  171 
  172     for tag in $tags; do
  173         [[ $tag == read ]] && continue
  174 
  175         print_html_link_tag "$tag" >> $site/link_build.html
  176         print_html_link_tag "$tag" >> $site/tags_build.html
  177     done
  178 
  179     print_html_link_tail >> $site/link_build.html
  180 
  181     for tag in $tags; do
  182         cat $site/link_build.html >> $site/"${tag}_build.html"
  183     done
  184 
  185     cat $site/link_build.html >> $site/index.html
  186 
  187     print_atom_link "$title" "$link" "$date" >> $site/atom.xml
  188 
  189     echo "$link" >> $site/bookmarks.txt
  190 done
  191 
  192 print_atom_tail >> $site/atom.xml
  193 
  194 {
  195     printf "    <div class=\"tags\">"
  196     sort -u $site/tags_build.html
  197     printf "    </div>"
  198 } > $site/tags_build.html.tmp
  199 
  200 mv $site/tags_build.html.tmp $site/tags_build.html
  201 cat $site/tags_build.html >> $site/index.html
  202 
  203 print_html_tail >> $site/index.html
  204 
  205 mv "$tmp" $db
  206 
  207 for file in $site/*; do
  208     [[ $file != *_build.html ]] && continue
  209     tag=$(sed -e "s/$site\///" -e 's/_build.html//' <<< "$file")
  210     mkdir -p $site/$tag
  211     print_html_head "$tag" > $site/$tag/index.html
  212     cat <<EOF >> $site/$tag/index.html
  213     <h1>
  214       <a href="$PLANNER_URL">${PLANNER_URL##https://}</a>#$tag
  215     </h1>
  216 EOF
  217     cat "$file" >> $site/$tag/index.html
  218     cat $site/tags_build.html >> $site/$tag/index.html
  219     print_html_tail >> $site/$tag/index.html
  220     cp ./style.css $site/$tag/
  221 done
  222 
  223 rm $site/*_build.html
  224 cp ./style.css $site/