1
0
Fork 0

rewrote to use POSIX shell features

This commit is contained in:
Emma Tebibyte 2024-01-16 15:05:12 -07:00
parent aba50c2843
commit 49a3d524d7
Signed by: emma
GPG Key ID: 06FA419A1698C270
2 changed files with 75 additions and 27 deletions

View File

@ -1,5 +1,10 @@
# Changelog
## 2024-12-16
- Rewrote build-index.sh to be POSIX-compliant
- build-index.sh now takes two arguments. The first is the root directory of the
JD system, and the second is the output directory.
## 2023-12-17
Repository created

View File

@ -1,40 +1,83 @@
#!/bin/sh
# define the target dir
parent="$(dirname "${BASH_SOURCE[0]}")"
cd "$parent"; cd ../../../
directory="$(pwd -P)"
# MIT License
#
# Copyright (c) 2023 Cordelya Sharpe
# Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
today="$(date "+%Y-%m-%d")"
set -e
printf "Date: %s\n" "$today"
test -n "$DEBUG" && set -x
for dirs in "$directory"/*; do
string="$(printf "---\n")"
area="$(basename "${dirs}")"
if [ "$area" != "System Volume Information" ]; then
if test -z "$2"; then
if test -z "$1"; then
# output to and read system from current dir if $1 and $2 are not set
out="$(pwd -P)"
root="$out"
else
# if $1 is set, that is the system root
root="$1"
fi
else
# if both $1 and $2 are set, they are the root and output dir
root="$1"
out="$2"
fi
string="$(printf "%stitle: %s\n" "$string" "$area")"
string="$(printf "%sdate: %s\n" "$string" "$today")"
string="$(printf "%s---\n\n" "$string")"
date="$(date "+%Y-%m-%d")"
for cats in "$dirs"/*; do
category="$(basename "$cats")"
string="$(printf "%s# %s\n\n" "$string" "$category")"
# for every area dir in the root, get the basename of the area
for dir in "$root"/*; do
area="$(basename "$dir")"
for ids in "$cats"/*; do
ident="$(basename "$ids")"
string="$(printf "%s* %s\n" "$string" "$ident")"
# only include jd files
if test -n "$(printf '%s\n' "$area" | sed -n '/^[0-9][0-9]-.*/p')"
then
# set the filename to the area number for the filename of the index
fullpath="$out/$(printf '%s\n' "$area" | dd bs=5 count=1 2>/dev/null).md"
# if the file already exists, back it up
if test -f "$fullpath"; then
mv "$fullpath" "$fullpath.old"
fi
printf '%s: %s: Saving index as %s.\n' "$0" "$area" "$fullpath" 1>&2
# add front matter to markdown file
printf -- "---\ntitle: %s\ndate: %s\n---\n" "$area" "$date" >>"$fullpath"
# iterate through catergories
for c in "$dir"/*; do
category="$(basename "$c")"
# add category as a header to the output file
printf "# %s\n\n" "$category" >>"$fullpath"
# iterate through contents of categories
for i in "$c"/*; do
id="$(basename "$i")"
# add each content item to the output file
printf "* %s\n" "$id" >>"$fullpath"
done
string="$(printf "%s\n" "$string")"
printf '\n' >>"$fullpath"
done
savedir="$directory/00-09 Index/00 Index/00.01 Index/"
fname="${area:0:5}.md"
fullpath="$savedir$fname"
printf '%s\n' "$fullpath"
printf '%s' "$string" > "$fullpath"
fi
done