star-ty

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

sty.sh (2084B)


      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 # Delete irrelevant data
     19 strip_dummy() # stdin
     20 {
     21     # - Remove comments
     22     # - Remove empty lines
     23     # - Remove heading an trailing spaces
     24      sed 's/#.\{0,\}$//g' \
     25    | sed '/^[[:space:]]\{0,\}$/d' \
     26    | sed -e 's/^[[:space:]]\{1,\}//g' -e 's/[[:space:]]\{1,\}$//g'
     27 }
     28 
     29 # List the sections of the menu
     30 sections()
     31 {
     32   # - Remove irrelevant data from the menu file
     33   # - Get the section field
     34   # - Protect spaces in the section name
     35     strip_dummy < menu.tsv \
     36   | cut -d'	' -f2 \
     37   | sed -e 's/[[:space:]]/\\ /g' \
     38   | xargs -I{} sh -c \
     39     "if [ -f \"\$1/index.tsv\" ]; then \
     40        printf '%s\n' \"\$1\"; \
     41      fi" -- {}
     42 }
     43 
     44 # Print the absolute dir of the input file
     45 absdir() # file
     46 {
     47   if [ -d "$1" ]; then
     48     cd -- "$1" || exit 1
     49   else
     50     cd -- "$(dirname "$1")" || exit 1
     51   fi
     52   echo "${PWD}"
     53   cd "${OLDPWD}" || exit 1
     54 }
     55 
     56 # Print relative path from input file to a dir
     57 relpath_to_dir() # file, dir
     58 (
     59   # Build directory from worktree to path
     60   _dir0="$(absdir "$1")"
     61   _dir1="$(absdir "$2")"
     62 
     63   _dir0="$(printf '%s\n' "${_dir0}" | sed "s;^${_dir1}[/]\{0,\};;g")"
     64 
     65   # Ensure that the directory is a subpath of the worktree
     66   if ! [ -d "${_dir1}/${_dir0}" ]; then
     67     >&2 printf '%s: %s is not a subdirectory of %s\n' \
     68       "$0" "${_dir0}" "${_dir1}"
     69     return 1
     70   fi
     71 
     72   echo "${_dir0}" | sed 's/\//\n/g' | while read -r _i; do
     73     printf "../"
     74   done
     75 )