pub / stagit-scripts

Building src.jayvii.de with stagit
git clone https://src.jayvii.de/pub/stagit-scripts.git
Home | Log | Files | Exports | Refs | README | RSS

git_update_repo_html.sh (6941B)


      1 #!/usr/bin/env sh
      2 # SPDX-License-Identifier: AGPL-3.0-or-later
      3 # SPDX-FileCopyrightText: 2021-2024 JayVii <jayvii[AT]posteo[DOT]de>
      4 
      5 # $1 - HTML Root
      6 # $2 - prv/pub
      7 # $3 - repo name
      8 
      9 # Error out if not all options are given
     10 if [ -z "$3" ]; then
     11     echo "Usage:
     12 git_update_repo_html.sh \\
     13     \"/var/www/git.example.git\" \\
     14     \"pub\" \\
     15     \"stagit-scripts\"
     16     "
     17     exit 1;
     18 fi
     19 
     20 # HTML Viewer
     21 cd "${1}/${2}/${3}" || exit 1;
     22 
     23 # Cleanup directory
     24 find "${1}/${2}/${3}" \
     25     -type f \
     26     -name "*.html" \
     27     -not -path "${1}/${2}/${3}/exports/*" \
     28     -exec rm {} \;
     29 
     30 # build HTML
     31 stagit "${1}/${2}/${3}.git"
     32 
     33 # Modules ----------------------------------------------------------------------
     34 
     35 # Activity module
     36 if [ -f "${1}/bin/activity.sh" ]; then
     37     ACTIVITY_HTML=`${1}/bin/activity.sh "${1}/${2}/${3}.git"`
     38 fi
     39 
     40 # Summary Page -----------------------------------------------------------------
     41 
     42 # remove pre-existing index
     43 if [ -f "${1}/${2}/${3}/index.html" ]; then
     44     rm "${1}/${2}/${3}/index.html"
     45 fi
     46 
     47 ## fetch repo description, URL and menu-line
     48 RDESC=$(
     49     cat "${1}/${2}/${3}.git/description" 2>/dev/null | \
     50     sed -e 's/\//\\\//g'
     51 )
     52 RURL=$(cat "${1}/${2}/${3}.git/url" 2>/dev/null)
     53 RURL_ESC=$(echo "$RURL" | sed -e 's/\//\\\//g')
     54 RMENU=$(
     55     grep -E "^.*<a\ href=\"log.html\">.*$" \
     56     --only-matching "${1}/${2}/${3}/log.html" | \
     57     sed -e 's/\//\\\//g'
     58 )
     59 
     60 ## read template sections
     61 THEAD=$(
     62     cat "${1}/templates/head.html" | \
     63     sed \
     64         -e "s/<\!--TITLE-->/${3}/g" \
     65         -e "s/<\!--DESC-->/${RDESC}/g" \
     66         -e "s/<\!--DIR-->/\./g" \
     67         -e "s/<\!--REPO-->/${RURL_ESC}/g"
     68 )
     69 TMENU=$(
     70     cat "${1}/templates/menu.html" | \
     71     sed \
     72         -e "s/<\!--TITLE-->/${3}/g" \
     73         -e "s/<\!--DESC-->/${RDESC}/g" \
     74         -e "s/<\!--REPO-->/${RURL_ESC}/g" \
     75         -e "s/<\!--MENU-->/${RMENU}/g"
     76 )
     77 
     78 ## re-format README to summary description
     79 # find readme
     80 READMEFILE=$(
     81     find "${1}/${2}/${3}/file" -type f -name "*README*" | \
     82     head -n 1
     83 )
     84 # if readme exists, re-format to summary description
     85 if [ ! -z "$READMEFILE" ]; then
     86     RSUMD=$(
     87         grep -E "<a\ href=\"#l[0-9]+\"\ class=\"line\"\ id=\"l[0-9]+\">" \
     88         "$READMEFILE" | \
     89         sed -E \
     90             -e 's/^<a\ .*?<\/a>\s*//g' \
     91             -e 's/&nbsp;/ /g' \
     92             -e ' s/&amp;/\&/g' \
     93             -e 's/&lt;/\</g' \
     94             -e 's/&gt;/\>/g' \
     95             -e 's/&quot;/\"/g' \
     96             -e 's/&#39;/\'"'"'/g' \
     97             -e 's/&ldquo;/\"/g' \
     98             -e 's/&rdquo;/\"/g' | \
     99         pandoc --to=html
    100     )
    101 fi
    102 
    103 ## Write template
    104 echo "<!DOCTYPE html>
    105 <html>
    106 ${THEAD}
    107 <body>
    108 ${TMENU}
    109 <div id=\"content\">
    110 ${ACTIVITY_HTML}
    111 <div id=\"description\">
    112 ${RSUMD}
    113 </div>
    114 <hr>
    115 <span>Clone this repository via:</span><br>
    116 <span>git clone <a href=\"$RURL\">$RURL</a></span>
    117 </a>
    118 </div>
    119 </body>
    120 </html>
    121 " > "${1}/${2}/${3}/index.html"
    122 
    123 # Exports Page -----------------------------------------------------------------
    124 
    125 ## fetch repo description, URL and menu-line
    126 RDESC=$(
    127     cat "${1}/${2}/${3}.git/description" 2>/dev/null | \
    128     sed -e 's/\//\\\//g'
    129 )
    130 RURL=$(
    131     cat "${1}/${2}/${3}.git/url" 2>/dev/null | \
    132     sed -e 's/\//\\\//g')
    133 RMENU=$(
    134     grep -E "^.*<a\ href=\"log.html\">.*$" \
    135     --only-matching "${1}/${2}/${3}/log.html" | \
    136     sed -e 's/\//\\\//g'
    137 )
    138 
    139 # read template sections
    140 THEAD=$(
    141     cat "${1}/templates/head.html" | \
    142     sed \
    143         -e "s/<\!--TITLE-->/Exports\ -\ ${3}/g" \
    144         -e "s/<\!--DESC-->/${RDESC}/g" \
    145         -e "s/<\!--DIR-->/\./g" \
    146         -e "s/<\!--REPO-->/${RURL}/g"
    147 )
    148 TMENU=$(
    149     cat "${1}/templates/menu.html" | \
    150     sed \
    151         -e "s/<\!--TITLE-->/${3}/g" \
    152         -e "s/<\!--DESC-->/${RDESC}/g" \
    153         -e "s/<\!--REPO-->/${RURL}/g" \
    154         -e "s/<\!--MENU-->/${RMENU}/g"
    155 )
    156 
    157 # list exports
    158 EXPTS=$(
    159     ls "${1}/${2}/${3}/exports/" -lhcr \
    160         --time-style="+%Y-%m-%d %H:%m" | \
    161     awk '{ print "<tr><td><a href=\"exports/" $8 "\">" $8 "</a></td><td>" $6 " " $7 "</td><td>" $5 "</td><tr>" }'
    162 )
    163 
    164 ## Write template
    165 echo "<!DOCTYPE html>
    166 <html>
    167 ${THEAD}
    168 <body>
    169 ${TMENU}
    170 <div id=\"content\">
    171 <table id=\"exports\">
    172 <thead>
    173 <tr><td><b>Name</b></td><td><b>Date</b></td><td class=\"num\" align=\"right\"><b>Size</b></td></tr>
    174 </thead>
    175 <tbody>
    176 ${EXPTS}
    177 </tbody>
    178 </table>
    179 </div>
    180 </body>
    181 </html>
    182 " > "${1}/${2}/${3}/exports.html"
    183 
    184 # Replacements -----------------------------------------------------------------
    185 
    186 # Protect Mail-Adresses in HTML
    187 MAILEXP='s/([a-zA-Z0-9\.]+)\@([a-zA-Z0-9\-]+)\.([a-z]+)/\1\[AT\]\2\[DOT\]\3/g'
    188 find "${1}/${2}/${3}/" -type f -name "*.html" -exec sed -E -e ${MAILEXP} -i {} \;
    189 
    190 # Add Exports to menu (behind Files)
    191 INJEXP="s/(>Files<\/a>\ \|)/\1 <a href=\"\/${2}\/${3}\/exports.html\">Exports<\/a> \|/"
    192 find "${1}/${2}/${3}/" -type f -name "*.html" -exec sed -E "${INJEXP}" -i {} \;
    193 
    194 # Add Home to menu (first item)
    195 INJEXP="s/(^.*log.html\">.*$)/<a href=\"\/${2}\/${3}\/index.html\">Home<\/a>\ \|\ \1/"
    196 find "${1}/${2}/${3}/" -type f -name "*.html" -exec sed -E "${INJEXP}" -i {} \;
    197 
    198 # Add RSS Feed to menu (last item)
    199 INJEXP="s/(^.*log.html\">.*)(<\/td>)/\1\ \|\ <a href=\"\/${2}\/${3}\/atom.xml\">RSS<\/a>\2/"
    200 find "${1}/${2}/${3}/" -type f -name "*.html" -exec sed -E "${INJEXP}" -i {} \;
    201 
    202 # Add Back-Button and Summary page (within title)
    203 INJEXP="s/(<h1>)(${3})(<\/h1>)/\1<a\ href=\"\/${2}\/\">${2}<\/a>\ \/\ <a\ href=\"\/${2}\/${3}\/index.html\">\2<\/a>\3/"
    204 find "${1}/${2}/${3}/" -type f -name "*.html" -exec sed -E "${INJEXP}" -i {} \;
    205 
    206 # Replace "git clone" href
    207 INJEXP="s/(git\ clone)\ <a href=\"([^\"]+)\">.*?<\/a>/<code>\1 \2<\/code>/g"
    208 find "${1}/${2}/${3}/" -type f -name "*.html" -exec sed -E "${INJEXP}" -i {} \;
    209 
    210 # Inject copy-codeblock for file previews (needs to be run inside SHELL)
    211 find "${1}/${2}/${3}/file" -type f -name "*.html" -exec sh -c 'sed -E "s/^<p>\s*($(basename {} | sed -e s/\.html$//)\s*\([^\)]*\))\s*<\/p>/<p><a title=\"Copy to Clipboard\" class=\"copycodeblock\" href=\"javascript:copy_to_clipboard(codeblock());\">\1<\/a><\/p>/" -i {}' \;
    212 
    213 # Mark menu entries as active if page is opened (needs to be run inside SHELL)
    214 find "${1}/${2}/${3}/" -type f -name "*.html" -exec sh -c 'sed -E "s/(href=\"[^\"]*$(basename {})\")/\1\ class=\"active\"/g" -i {}' \;
    215 find "${1}/${2}/${3}/commit/" -type f -name "*.html" -exec sed -E 's/(href=\"[^\"]*log.html\")/\1\ class=\"active\"/g' -i {} \;
    216 find "${1}/${2}/${3}/file/" -type f -name "*.html" -exec sed -E 's/(href=\"[^\"]*files.html\")/\1\ class=\"active\"/g' -i {} \;
    217 
    218 # Inject javascript reference in HTML
    219 INJEXP="s/(<\/html>)/<script\ async\ src=\"\/assets\/js\/code_to_clipboard.js\"><\/script>\1/"
    220 find "${1}/${2}/${3}/" -type f -name "*.html" -exec sed -E "${INJEXP}" -i {} \;
    221 
    222 # Linking Assets ---------------------------------------------------------------
    223 ln -sf "${1}/assets/style.min.css" "${1}/${2}/${3}/style.css"
    224 ln -sf "${1}/assets/favicon.png" "${1}/${2}/${3}/favicon.png"
    225 ln -sf "${1}/assets/backicon.png" "${1}/${2}/${3}/logo.png"
    226