star-ty

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

sty-list (3688B)


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