star-ty

Generate content for online publication
git clone git://git.meso-star.fr/star-ty.git
Log | Files | Refs | README | LICENSE

sty-list (3636B)


      1 #!/bin/sh
      2 
      3 # Copyright (C) 2017-2025 |Méso|Star> (contact@meso-star.com)
      4 #
      5 # This program is free software: you can redistribute it and/or modify
      6 # it under the terms of the GNU General Public License as published by
      7 # the Free Software Foundation, either version 3 of the License, or
      8 # (at your option) any later version.
      9 #
     10 # This program is distributed in the hope that it will be useful,
     11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     13 # GNU General Public License for more details.
     14 #
     15 # You should have received a copy of the GNU General Public License
     16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
     17 
     18 . "sty.sh"
     19 
     20 set -e
     21 
     22 ########################################################################
     23 # Helper functions
     24 ########################################################################
     25 usage()
     26 {
     27   >&2 printf 'usage: %s resource\n' "${0##*/}"
     28 }
     29 
     30 # List of HTML files to generate from Markdown files or shell scripts
     31 # stored in each section
     32 html()
     33 {
     34   _find_args="$(\
     35       printf '%s\n' "${sections}" \
     36     | sed -e 's/^/! -path /' \
     37     | tr '\n' ' ')"
     38 
     39   eval "exec find ${search_dirs} ${_find_args} -type d -prune -o \
     40     \( \
     41           -name \"*.md\" \
     42        -o -name \"*.sh\" \
     43     \) -exec sh -c 'printf \"%s.html\n\" \"\${1%.*}\"' -- {} \; \
     44     | sort"
     45 }
     46 
     47 # List of shell scripts that dynamically generate HTML content
     48 shtml()
     49 {
     50   _find_args="$(\
     51       printf '%s\n' "${sections}" \
     52     | sed -e 's/^/! -path /' \
     53     | tr '\n' ' ')"
     54 
     55   eval "exec find ${search_dirs} ${_find_args} -type d -prune -o \
     56       -name \"*.sh\" -print \
     57     | sort"
     58 }
     59 
     60 # List of hooks, i.e. shell scripts to be run by section
     61 hook()
     62 {
     63   _find_args="$(\
     64       printf '%s\n' "${sections}" \
     65     | sed -e 's/^/-path "/;s/$/\/hooks\/*" -o/' \
     66     | tr '\n' ' ' \
     67     | sed 's/ -o $//')"
     68 
     69   eval "find ${search_dirs} \
     70     \( \( ${_find_args} \) -type f -name \"[0-9][0-9]-*.sh\"  \) \
     71     | sort"
     72 }
     73 
     74 # List the shell scripts relevant to the compilation system, i.e. the
     75 # shell script at the root of the working tree and those in each section
     76 # used to generate HTML pages.
     77 shell()
     78 {
     79   _find_args="$(\
     80       printf '%s\n' "${sections}" \
     81     | sed -e 's/^\(.\{0,\}\)$/! -path \1 ! -path \1\/hooks/' \
     82     | tr '\n' ' ')"
     83 
     84   _search_dirs="${search_dirs}"
     85   if [ -d "scripts" ]; then
     86     _search_dirs="scripts ${_search_dirs}"
     87     _find_args="! -path scripts ${_find_args}"
     88   fi
     89 
     90   eval "exec find ${_search_dirs} \
     91     ${_find_args} -type d -prune -o -name \"*.sh\" -print \
     92     | sort"
     93 }
     94 
     95 # List the relevent section subdirectories which are:
     96 #   - downloads
     97 #   - images
     98 #   - thumbs
     99 subdir()
    100 {
    101   downloads="-path \1/downloads"
    102   images="-path \1/images -o -path \1/thumbs"
    103 
    104   _find_args="$(\
    105     printf '%s\n' "${sections}" \
    106   | sed "s;^\(.\{0,\}\)$;${downloads} -o ${images} -o;" \
    107   | tr '\n' ' ' \
    108   | sed 's/ -o $//')"
    109 
    110   eval "exec find ${search_dirs} ${_find_args} -type d | sort"
    111 }
    112 
    113 ########################################################################
    114 # Script
    115 ########################################################################
    116 if [ "$#" -lt 1 ]; then
    117   usage
    118   exit 1
    119 fi
    120 
    121 if [ ! -e menu.tsv ]; then
    122   >&2 printf \
    123     '%s: not a star-typesetting directory (the menu.tsv file is missing)\n' \
    124     "${0##*/}"
    125   exit 1
    126 fi
    127 
    128 # List of sections to be dealt with
    129 sections="$(sections)"
    130 [ -z "${sections}" ] && exit 0 # No section
    131 
    132 search_dirs="$(printf '%s\n' "${sections}" | tr '\n' ' ')"
    133 
    134 case "$1" in
    135   "html") html ;;
    136   "hook") hook ;;
    137   "shell") shell ;;
    138   "shtml") shtml ;;
    139   "subdir") subdir ;;
    140   *) usage; exit 1 ;;
    141 esac