star-ty

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

sty-hooks (6064B)


      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 if [ ! -e "menu.tsv" ]; then
     25   >&2 printf \
     26     '%s: not a star-typesetting directory (the menu.tsv file is missing)\n' \
     27     "${0##*/}"
     28   exit 1
     29 fi
     30 
     31 # Print on standard output the Makefile targets used to automate hook
     32 # management
     33 
     34 shtml="$(sty-list shtml)"
     35 hook="$(sty-list hook)"
     36 
     37 sections | while read -r i; do
     38   shtml_section="$(printf '%s\n' "${shtml}" \
     39     | sed -n "/^${i}\//{s/\.sh/\.md/g;p;}" | tr '\n' ' ')"
     40   hook_section="$(printf '%s\n' "${hook}" \
     41     | sed -n "/^${i}\//p" | tr '\n' ' ' \
     42     | sed -e 's/\.sh[[:space:]]/.hook /g')"
     43 
     44   # Define Makefile target that makes section hooks prerequisites for
     45   # the HTML content of the section.
     46   #
     47   # It is the files generated by each of the hooks that are
     48   # prerequisites for the HTML files, not the scripts  of the hooks
     49   # themselves, because even if the scripts have not changed, their
     50   # output may have been updated, which can impact the HTML content
     51   # generated at build time.
     52   #
     53   # Furthermore, it is not the HTML file itself that depends on the
     54   # hook's output, but rather the intermediate Markdown dynamically
     55   # generated by the script, whose output may depend on the execution of
     56   # hooks.
     57   if [ -n "${shtml_section}" ] \
     58   && [ -n "${hook_section}" ]; then
     59     printf '%s: %s\n' "${shtml_section}" "${hook_section}"
     60   fi
     61 done
     62 
     63 printf '%s\n' "${hook}" | while read -r i; do
     64   # Discard empty line
     65   if [ -z "${i}" ]; then continue; fi
     66 
     67   # Divide the path into two parts:
     68   #   - the top-level directory, i.e. the section
     69   #   - the shell script to be executed from the section directory
     70   section="${i%%/*}"
     71   hook="${i##"${section}"/}"
     72 
     73   # The hook is the first one in the current section: it has the highest
     74   # priority and therefore has no prerequisites.
     75   if [ "${section}" != "${prev_section}" ]; then
     76     dep=""
     77     tgt_list=""
     78     prev_priority=""
     79   fi
     80 
     81   # Retrieve the hook priority
     82   priority="$(basename "${i}")"
     83   priority="${priority%%-*}"
     84 
     85   # Define a name for the hook used as a prefix for Makefile targets in
     86   # order to automate its execution and the management of the file it
     87   # generates. To ensure that this name is unique, and thus avoid
     88   # conflicts between different hooks, it is  constructed from the names
     89   # of the section and file corresponding to the hook.
     90   file="$(basename "${hook}")"
     91   prefix="${section}-${file%%.*}"
     92 
     93   # Set the file in which the names of the files generated by the hook
     94   # are stored as resources for the website, i.e., the files that must
     95   # be deployed with the website.
     96   tgt="${i%%.*}.hook"
     97 
     98   if [ "${priority}" = "${prev_priority}" ]; then
     99     # The hook's priority is the same as the previous one. Add its
    100     # target to the list of prerequisites for hooks with lower priority
    101     tgt_list="${tgt_list} ${tgt}"
    102   else
    103     # The priority of the current hook is lower than those processed
    104     # previously. Define as a prerequisite the execution of hooks whose
    105     # priority precedes the priority of the current hook.
    106     dep="${tgt_list}"
    107 
    108     # The execution of the current hook becomes a prerequisite for hooks
    109     # with lower priority.
    110     tgt_list="${tgt}"
    111   fi
    112 
    113   # Define the Makefile target that automate the hook executation. Its
    114   # pre-requisites are the hook script and the output of the # previous
    115   # hook in the section, i.e. with an higher priority. So that
    116   # execution priority is ensured.
    117   #
    118   # Finally, make this target a prerequisite for building the website
    119   # in order to enforce its execution during the build call.
    120   printf '%s: %s %s\n' "${tgt}" "${i}" "${dep}"
    121   printf '	@cd -- %s; ' "${section}"
    122   printf "\$(SHELL) %s > \$\${OLDPWD}/\$@ " "${hook}"
    123   printf "|| { rm -f \$\${OLDPWD}/\$@; exit 1; } \n"
    124 
    125   # Set the target for cleaning files generated by executing the hook.
    126   # Run it on "distclean" and not on "clean", as their generation can be
    127   # costly while effectively being files generated to "distribute" the
    128   # website.
    129   printf '%s-distclean:\n' "${prefix}"
    130   printf '	if [ -f %s ]; then cat %s | xargs rm -f; fi\n' \
    131     "${tgt}" "${tgt}"
    132   printf '	rm -f %s\n' "${tgt}"
    133   printf 'distclean__: %s-distclean\n' "${prefix}"
    134 
    135   # Set the target to "lightly" clean up the hook, i.e., force it to
    136   # re-execute without deleting the files it would generate. This way,
    137   # the hooks could query these files in order to regenerate them
    138   # depending on whether they deem it necessary or not.
    139   #
    140   # This aims to speed up the re-execution of hooks, as there is no way
    141   # to track their dependencies, meaning that their (conditional)
    142   # re-execution is often the only way to ensure that content is up to
    143   # date with the latest updates.
    144   printf '%s-clean:\n' "${prefix}"
    145   printf '	rm -f %s\n' "${tgt}"
    146   printf 'clean__: %s-clean\n' "${prefix}"
    147 
    148   # Set the target that installs website resources generated by the hook
    149   printf '%s-install: %s\n' "${prefix}" "${tgt}"
    150   printf '	@rsync -avzrR --delete-after --progress \\\n'
    151   printf '	--chmod=Dg+s,g+w,Fg+w --chown=:'"\$(GROUP)"' --omit-dir-times \\\n'
    152   printf '	--omit-link-times --files-from=%s ./ '"\$(PREFIX)"'\n' "${tgt}"
    153   printf 'install__: %s-install\n' "${prefix}"
    154 
    155   # Save the current section and priority of the hook that has just been
    156   # processed
    157   prev_section="${section}"
    158   prev_priority="${priority}"
    159 done