star-ty

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

sty.sh (2136B)


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