1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-05-17 01:33:22 -07:00
rc/sh/document

179 lines
5.6 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env dash
2021-10-29 06:03:02 -07:00
# FAKE_COMPAT
# NOTE: a lot of boilerplate code is pulled from the pure sh bible:
# https://github.com/dylanaraps/pure-sh-bible
document2() {
2021-07-31 16:50:21 -07:00
local c=0 # line count
local d="#""#""#" # marker/delimiter, written this way to avoid self-matching
# NOTE: since the marker corresponds to a <h3> in markdown, it's used there too.
local e="${f#sh/}" # function name to expect
2021-07-30 19:42:53 -07:00
local f="$1" # file
local o=1 # when one, previous line was a one-liner (@- token).
2021-07-31 16:50:21 -07:00
[ "$f" != "$e" ] || e="" # only expect function names for stuff in sh/
while IFS= read -r line; do
: $((c+=1)) $((o+=1))
# we only care about lines with our docstring marker in them:
2021-07-31 16:50:21 -07:00
case "$line" in *"$d"*) :;; *) continue;; esac
2021-07-30 19:42:53 -07:00
# split by the marker:
2021-07-31 16:50:21 -07:00
local code="${line%%$d*}" docs="${line#*$d}"
code="${code#${code%%[! ]*}}" # ltrim
docs="${docs# }" # ltrim a single space
2021-07-30 19:42:53 -07:00
local s=' ' n='' # using a space to signify unset
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)
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
done
2021-07-30 19:42:53 -07:00
s="${docs#@-}" # substr the stuff after the hyphen
s="${s#${s%%[! ]*}}" # ltrim
[ -z "$s" ] || o=0
;;
@*-*)
2021-07-30 19:42:53 -07:00
# split by the hyphen:
n="${docs%%-*}"
s="${docs#*-}"
2021-07-30 19:42:53 -07:00
n="${n#@}" # substr the stuff after the at
n="${n#${n%%[! ]*}}" # ltrim
n="${n%${n##*[! ]}}" # rtrim
s="${s#${s%%[! ]*}}" # ltrim
[ -z "$s" ] || o=0
;;
@*)
n="${docs#@}" # substr the stuff after the at
n="${n#${n%%[! ]*}}" # ltrim
;;
*)
s="$docs" # leave whitespace intact
;;
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"
if [ "$n" = "$e" ]; then
2021-07-30 19:42:53 -07:00
# function name matches the filename.
2021-08-01 00:35:56 -07:00
printf '\n%s [%s](%s)\n\n' "$d" "$n" "$url" || return 5
else
2021-07-30 19:42:53 -07:00
# this file contains some other function, so include the filename.
2021-08-01 00:35:56 -07:00
printf '\n%s [%s](%s)\n\n' "$d" "$n (${f#.})" "$url" || return 5
#printf '\n%s %s\n\n* defined in [%s](%s)\n\n' "%d" "$n" "$f" "$url" || 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:
if [ $o = 1 ]; then
# ensure one-liners are their in their own paragraph.
printf '\n' || return 5
fi
2021-08-01 00:35:56 -07:00
printf '%s\n' "$s" || return 5
2021-07-30 17:57:08 -07:00
fi
fi
done < "$f" || return 4
}
document1() {
# NOTE: in the future, it'd be nice to support arbitary files through arguments.
2021-08-01 00:35:56 -07:00
local in_='rc/README.md'
local out='rc/README.md~'
if [ $# -eq 0 ]; then
cd || return 2
[ -d rc ] || return 3 # sanity check
elif [ $# -eq 1 ]; then
in_='README.md'
out='README.md~'
cd "$1" || return 2
else
return 1
fi
2021-07-30 19:42:53 -07:00
# sanity check:
[ -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-08-01 00:35:56 -07:00
: > "$out" || return 4
local line=
2021-08-01 00:35:56 -07:00
if [ -f "$in_" ]; then
2021-07-30 19:42:53 -07:00
# copy existing lines up to (and including) the "DOCUMENT" marker:
while IFS= read -r line; do
2021-08-01 00:35:56 -07:00
printf '%s\n' "$line" >> "$out" || return 5
case "$line" in *DOCUMENT*) break;; esac
2021-08-01 00:35:56 -07:00
done < "$in_" || return 4
fi
2021-07-30 19:42:53 -07:00
# first section:
2021-08-01 00:35:56 -07:00
printf '\n## %s\n' 'shell functions' >> "$out" || return 5
for f in sh/*; do
2021-07-30 19:42:53 -07:00
[ -e "$f" ] || continue # make sure glob went through
local fn="${f##*/}"
# ignore some stuff:
[ "$fn" = "${fn#_}" ] || continue # completion files
[ "$fn" = "${fn%.bak}" ] || continue # backup files
2021-10-05 10:59:05 -07:00
document2 "$f" >> "$out" || return
done
2021-07-30 19:42:53 -07:00
# second section:
2021-08-01 00:35:56 -07:00
printf '\n## %s\n' 'miscellaneous' >> "$out" || return 5
2021-10-05 10:59:05 -07:00
document2 .zshrc >> "$out" || return
document2 .bashrc >> "$out" || return
document2 .shrc >> "$out" || return
}
2021-07-30 19:47:43 -07:00
document() { ### @-
### generate a markdown file out of docstrings in shell scripts.
2021-07-31 16:50:21 -07:00
###
### **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
}
[ -n "${preload+-}" ] || document "$@"