No, actually. . . paste lib.sh into bread

This commit is contained in:
Case Duckworth 2021-11-22 21:05:39 -06:00
parent 61fd3a6f6b
commit 454804f334
1 changed files with 127 additions and 2 deletions

View File

@ -5,8 +5,133 @@
: "${BREADNEWS:=/bread/news}"
: "${BREADDOCS:=/bread/docs}"
#shellcheck disable=1091
#. /bread/site/lib.sh
### Here's the bit that /was/ in lib.sh
#shellcheck disable=2034
DISCOUNT="markdown -f links,image,pants,html,autolink,fencedcode"
alias p=echo
alias l='p >&2'
N(){ f="${1:-$F}";n="${f#$UNKI/}";p "${n%.*}";}
NN(){ f="${1:-$F}";n="${f##*/}";p "${n%%.*}";}
N_(){ f="${1:-$F}";n="$(N "$f")";p "${n#_}";}
M(){ sed '/^$/q' "${2:-$F}"|grep "^$1"|cut -f2-;}
C(){ sed '1,/^$/d' "${1:-$F}";}
X(){
Xd=$((Xd+1));
eval "$(p "set -e";p "cat<<$UNKZ$Xd";cat "${1:--}"|sed 's/`/\\`/g';p;p "$UNKZ$Xd")";
}
modmsg()
{ # message as to when a page was modified
date -d "$1" \
+'this page was updated on <time datetime="%F">'"${2:-%e %B %Y}"'</time>'
}
series()
{
in="$(cat)"
s="$*"
if [ -n "$in" ]; then
s="$s $in"
fi
if [ -z "$s" ]; then
return
fi
tx "part of the $s series"
}
wrapstr()
{ # wrap a non-empty string
sed -e "s_^._<$*>&_" -e "s_.\$_&</$1>_"
}
addstr()
{ # add a string after, but not after an empty string
sed "s/^.\+\$/&$*/"
}
defstr()
{ # replace an empty string with a default
sed "s/^\$/$*/"
}
slugify()
{ # turn a string into a slug
if [ "$#" -gt 0 ]; then
echo "$*"
else
cat
fi |
tr '[:upper:]' '[:lower:]' |
sed \
-e 's/[[:punct:]]//g' \
-e 's/[^[:alnum:]_]/-/g' \
-e 's/-+/-/g' \
-e 's/^-//' -e 's/-$//'
}
recent_files()
{ # list files
# WARNING: find -printf is NOT posix :(
dir="$1"
shift
find "$dir" -maxdepth 1 \
-not -name '.*' "$@" \
-printf '%T@\t%p\n' \
| sort -nr \
| cut -f2
}
maybe_expand()
{ # expand a file based on its metadata (or lack thereof)
F="${1:-$F}"
expand="$(M expand "$F")"
if [ -z "$expand" ] || "$expand"; then
C "$F" | ./lht | X
else
C "$F"
fi
}
#shellcheck disable=2120
build()
{ # X on a file
F="${1:-$F}"
[ -f "$F" ] || return 1
l "$F"
dir="$UNKO/$(N)"
rm -rf "$dir"
mkdir -p "$dir"
X "$UNKL" > "$dir/index.html"
}
build_dir()
{ # build a directory
for F in "$UNKI/$1"/*; do
build || l "$F: not a file"
done
}
listing()
{ # print a listing
p "<ul class='listing'>"
for F in $(recent_files "$UNKI/$1" -type f); do
build && {
p "<li><a href='/$(N)/'>$(M title)</a>"
if [ -n "$(M series)" ]; then
p "<span class='series desc'>$(series "$(M series)")</span>"
fi
p "</li>"
}
done
p "</ul>"
}
###
groupck()
{