2021-07-30 16:35:00 -07:00
|
|
|
#!/usr/bin/env dash
|
|
|
|
# NO_ZSH
|
|
|
|
# NO_BASH
|
|
|
|
# YES_DASH
|
|
|
|
# okay, so this probably does run with zsh and bash, but why bother?
|
|
|
|
|
|
|
|
# NOTE: a lot of boilerplate code is pulled from the pure sh bible:
|
|
|
|
# https://github.com/dylanaraps/pure-sh-bible
|
|
|
|
|
|
|
|
document2() {
|
2021-07-30 19:42:53 -07:00
|
|
|
local f="$1" # file
|
|
|
|
local c=0 # (line) count
|
|
|
|
local e="${f#sh/}" # expecting (function name matches filename)
|
2021-07-30 16:35:00 -07:00
|
|
|
[ "$f" != "$e" ] || e=""
|
2021-07-30 19:42:53 -07:00
|
|
|
[ "$e" != document ] || return 0 # doesn't play nicely yet, so skip it
|
2021-07-30 16:35:00 -07:00
|
|
|
while IFS= read -r line; do
|
|
|
|
: $((c+=1))
|
2021-07-30 19:42:53 -07:00
|
|
|
# we only care about lines with our docstring marker on them:
|
2021-07-30 16:35:00 -07:00
|
|
|
case "$line" in *'###'*) :;; *) continue;; esac
|
|
|
|
|
2021-07-30 19:42:53 -07:00
|
|
|
# split by the marker:
|
2021-07-30 16:35:00 -07:00
|
|
|
local code="${line%%###*}" docs="${line#*###}"
|
|
|
|
code="${code#${code%%[![:space:]]*}}" # ltrim
|
|
|
|
docs="${docs#${docs%%[![:space:]]*}}" # ltrim
|
|
|
|
|
2021-07-30 19:42:53 -07:00
|
|
|
local s=' ' n='' # using a space to signify unset
|
2021-07-30 16:35:00 -07:00
|
|
|
|
|
|
|
case "$docs" in
|
|
|
|
@-*)
|
|
|
|
s="$code"
|
2021-07-30 19:42:53 -07:00
|
|
|
n="${s%%[!a-zA-Z0-9_-]*}" # substr first word (might not be one)
|
2021-07-30 16:35:00 -07:00
|
|
|
while [ -z "$n" -o "$n" = function -o "$n" = alias ]; do
|
|
|
|
[ -n "$s" ] || break
|
2021-07-30 19:42:53 -07:00
|
|
|
s="${s#${s%%[!a-zA-Z0-9_-]*}}" # lstrip to end of word
|
|
|
|
s="${s#*[!a-zA-Z0-9_-]}" # lstrip to next word
|
|
|
|
n="${s%%[!a-zA-Z0-9_-]*}" # substr that word
|
2021-07-30 16:35:00 -07:00
|
|
|
done
|
2021-07-30 19:42:53 -07:00
|
|
|
s="${docs#@-}" # substr the stuff after the hyphen
|
2021-07-30 16:35:00 -07:00
|
|
|
s="${s#${s%%[![:space:]]*}}" # ltrim
|
|
|
|
;;
|
|
|
|
|
|
|
|
@*-*)
|
2021-07-30 19:42:53 -07:00
|
|
|
# split by the hyphen:
|
2021-07-30 16:35:00 -07:00
|
|
|
n="${docs%%-*}"
|
|
|
|
s="${docs#*-}"
|
|
|
|
|
2021-07-30 19:42:53 -07:00
|
|
|
n="${n#@}" # substr the stuff after the at
|
2021-07-30 16:35:00 -07:00
|
|
|
n="${n#${n%%[![:space:]]*}}" # ltrim
|
|
|
|
n="${n%${n##*[![:space:]]}}" # rtrim
|
|
|
|
|
|
|
|
s="${s#${s%%[![:space:]]*}}" # ltrim
|
|
|
|
;;
|
|
|
|
|
|
|
|
@*)
|
|
|
|
n="${docs#@}"
|
|
|
|
n="${n#${n%%[![:space:]]*}}" # ltrim
|
|
|
|
;;
|
|
|
|
|
|
|
|
*)
|
|
|
|
s="${docs#${docs%%[![:space:]]*}}" # ltrim
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
if [ -n "$n" ]; then
|
|
|
|
local url
|
2021-07-30 19:42:53 -07:00
|
|
|
# different subdirs need to be handled differently:
|
2021-07-30 18:08:59 -07:00
|
|
|
[ "$f" != "${f#sh/}" ] && url="/sh/${f#sh/}#L$c" || url="/home/${f#.}#L$c"
|
2021-07-30 16:35:00 -07:00
|
|
|
if [ "$n" = "$e" ]; then
|
2021-07-30 19:42:53 -07:00
|
|
|
# function name matches the filename.
|
2021-07-30 16:35:00 -07:00
|
|
|
printf '\n### [%s](%s)\n\n' "$n" "$url" >> rc/README.md~ || return 5
|
|
|
|
else
|
2021-07-30 19:42:53 -07:00
|
|
|
# this file contains some other function, so include the filename.
|
2021-07-30 17:57:08 -07:00
|
|
|
printf '\n### [%s](%s)\n\n' "$n (${f#.})" "$url" >> rc/README.md~ || return 5
|
2021-07-30 16:35:00 -07:00
|
|
|
#printf '\n### %s\n\n* defined in [%s](%s)\n\n' "$n" "$f" "$url" >> rc/README.md~ || return 5
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2021-07-30 19:42:53 -07:00
|
|
|
if [ "$s" != ' ' ]; then # don't bother unless it was set to something
|
|
|
|
if [ -z "$n" -o -n "$s" ]; then # might only be a name, check that
|
|
|
|
# just pass the remaining comment through:
|
2021-07-30 17:57:08 -07:00
|
|
|
printf '%s\n' "$s" >> rc/README.md~ || return 5
|
|
|
|
fi
|
2021-07-30 16:35:00 -07:00
|
|
|
fi
|
|
|
|
done < "$f" || return 4
|
|
|
|
}
|
|
|
|
|
|
|
|
document1() {
|
|
|
|
# NOTE: in the future, it'd be nice to support arbitary files through arguments.
|
|
|
|
[ $# -le 0 ] || return 1
|
|
|
|
|
2021-07-30 19:42:53 -07:00
|
|
|
# sanity check:
|
2021-07-30 16:35:00 -07:00
|
|
|
cd || return 2
|
|
|
|
[ -d rc ] && [ -d sh ] && [ -f .zshrc ] && [ -f .-shrc ] && [ -f .bashrc ] || return 3
|
|
|
|
|
2021-07-30 19:42:53 -07:00
|
|
|
# create new output file (with a tilde as not to overwrite just yet):
|
2021-07-30 16:35:00 -07:00
|
|
|
: > rc/README.md~ || return 4
|
|
|
|
|
|
|
|
local line
|
|
|
|
|
|
|
|
if [ -f rc/README.md ]; then
|
2021-07-30 19:42:53 -07:00
|
|
|
# copy existing lines up to (and including) the "DOCUMENT" marker:
|
2021-07-30 16:35:00 -07:00
|
|
|
while IFS= read -r line; do
|
|
|
|
printf '%s\n' "$line" >> rc/README.md~ || return 5
|
|
|
|
case "$line" in *DOCUMENT*) break;; esac
|
|
|
|
done < rc/README.md || return 4
|
|
|
|
fi
|
|
|
|
|
2021-07-30 19:42:53 -07:00
|
|
|
# first section:
|
2021-07-30 16:35:00 -07:00
|
|
|
printf '\n## %s\n' 'shell functions' >> rc/README.md~ || return 5
|
|
|
|
for f in sh/*; do
|
2021-07-30 19:42:53 -07:00
|
|
|
[ -e "$f" ] || continue # make sure glob went through
|
2021-07-30 16:35:00 -07:00
|
|
|
document2 "$f" || return $?
|
|
|
|
done
|
|
|
|
|
2021-07-30 19:42:53 -07:00
|
|
|
# second section:
|
2021-07-30 16:35:00 -07:00
|
|
|
printf '\n## %s\n' 'miscellaneous' >> rc/README.md~ || return 5
|
|
|
|
document2 .zshrc || return $?
|
|
|
|
document2 .bashrc || return $?
|
|
|
|
document2 .-shrc || return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
document() { ### @ document
|
|
|
|
### generate a markdown file out of docstrings in shell scripts.
|
|
|
|
### **TODO:** describe. i have a rough outline written in my scrap file.
|
|
|
|
|
|
|
|
document1 "$@"
|
|
|
|
local ret=$?
|
|
|
|
|
|
|
|
case $ret in
|
|
|
|
0)
|
|
|
|
return 0;;
|
|
|
|
1)
|
|
|
|
printf '%s\n' "$0: too many arguments" >&2
|
|
|
|
return 1;;
|
|
|
|
2)
|
|
|
|
printf '%s\n' "$0: failed to change directory" >&2
|
|
|
|
return 1;;
|
|
|
|
3)
|
|
|
|
printf '%s\n' "$0: essential files are missing" >&2
|
|
|
|
return 1;;
|
|
|
|
4)
|
|
|
|
printf '%s\n' "$0: failed to open file" >&2
|
|
|
|
return 1;;
|
|
|
|
5)
|
|
|
|
printf '%s\n' "$0: failed to read line" >&2
|
|
|
|
return 1;;
|
|
|
|
*)
|
|
|
|
printf '%s\n' "$0: unknown error occurred" >&2
|
|
|
|
return 1;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
[ "${SOURCING:-0}" -gt 0 ] || document "$@"
|