1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-06-28 18:17:11 -07:00

write more comments for document

This commit is contained in:
Connor Olding 2021-07-30 19:42:53 -07:00
parent 0439a5a4e8
commit f73c003e13

View File

@ -8,40 +8,43 @@
# https://github.com/dylanaraps/pure-sh-bible # https://github.com/dylanaraps/pure-sh-bible
document2() { document2() {
local f="$1" local f="$1" # file
local c=0 local c=0 # (line) count
local e="${f#sh/}" local e="${f#sh/}" # expecting (function name matches filename)
[ "$f" != "$e" ] || e="" [ "$f" != "$e" ] || e=""
[ "$e" != document ] || return 0 [ "$e" != document ] || return 0 # doesn't play nicely yet, so skip it
while IFS= read -r line; do while IFS= read -r line; do
: $((c+=1)) : $((c+=1))
# we only care about lines with our docstring marker on them:
case "$line" in *'###'*) :;; *) continue;; esac case "$line" in *'###'*) :;; *) continue;; esac
# split by the marker:
local code="${line%%###*}" docs="${line#*###}" local code="${line%%###*}" docs="${line#*###}"
code="${code#${code%%[![:space:]]*}}" # ltrim code="${code#${code%%[![:space:]]*}}" # ltrim
docs="${docs#${docs%%[![:space:]]*}}" # ltrim docs="${docs#${docs%%[![:space:]]*}}" # ltrim
local s=' ' n='' local s=' ' n='' # using a space to signify unset
case "$docs" in case "$docs" in
@-*) @-*)
s="$code" s="$code"
n="${s%%[!a-zA-Z0-9_-]*}" n="${s%%[!a-zA-Z0-9_-]*}" # substr first word (might not be one)
while [ -z "$n" -o "$n" = function -o "$n" = alias ]; do while [ -z "$n" -o "$n" = function -o "$n" = alias ]; do
[ -n "$s" ] || break [ -n "$s" ] || break
s="${s#${s%%[!a-zA-Z0-9_-]*}}" s="${s#${s%%[!a-zA-Z0-9_-]*}}" # lstrip to end of word
s="${s#*[!a-zA-Z0-9_-]}" s="${s#*[!a-zA-Z0-9_-]}" # lstrip to next word
n="${s%%[!a-zA-Z0-9_-]*}" n="${s%%[!a-zA-Z0-9_-]*}" # substr that word
done done
s="${docs#@-}" s="${docs#@-}" # substr the stuff after the hyphen
s="${s#${s%%[![:space:]]*}}" # ltrim s="${s#${s%%[![:space:]]*}}" # ltrim
;; ;;
@*-*) @*-*)
# split by the hyphen:
n="${docs%%-*}" n="${docs%%-*}"
s="${docs#*-}" s="${docs#*-}"
n="${n#@}" n="${n#@}" # substr the stuff after the at
n="${n#${n%%[![:space:]]*}}" # ltrim n="${n#${n%%[![:space:]]*}}" # ltrim
n="${n%${n##*[![:space:]]}}" # rtrim n="${n%${n##*[![:space:]]}}" # rtrim
@ -60,17 +63,21 @@ document2() {
if [ -n "$n" ]; then if [ -n "$n" ]; then
local url local url
# different subdirs need to be handled differently:
[ "$f" != "${f#sh/}" ] && url="/sh/${f#sh/}#L$c" || url="/home/${f#.}#L$c" [ "$f" != "${f#sh/}" ] && url="/sh/${f#sh/}#L$c" || url="/home/${f#.}#L$c"
if [ "$n" = "$e" ]; then if [ "$n" = "$e" ]; then
# function name matches the filename.
printf '\n### [%s](%s)\n\n' "$n" "$url" >> rc/README.md~ || return 5 printf '\n### [%s](%s)\n\n' "$n" "$url" >> rc/README.md~ || return 5
else else
# this file contains some other function, so include the filename.
printf '\n### [%s](%s)\n\n' "$n (${f#.})" "$url" >> rc/README.md~ || return 5 printf '\n### [%s](%s)\n\n' "$n (${f#.})" "$url" >> rc/README.md~ || return 5
#printf '\n### %s\n\n* defined in [%s](%s)\n\n' "$n" "$f" "$url" >> rc/README.md~ || return 5 #printf '\n### %s\n\n* defined in [%s](%s)\n\n' "$n" "$f" "$url" >> rc/README.md~ || return 5
fi fi
fi fi
if [ "$s" != ' ' ]; then if [ "$s" != ' ' ]; then # don't bother unless it was set to something
if [ -z "$n" -o -n "$s" ]; then if [ -z "$n" -o -n "$s" ]; then # might only be a name, check that
# just pass the remaining comment through:
printf '%s\n' "$s" >> rc/README.md~ || return 5 printf '%s\n' "$s" >> rc/README.md~ || return 5
fi fi
fi fi
@ -81,26 +88,31 @@ document1() {
# NOTE: in the future, it'd be nice to support arbitary files through arguments. # NOTE: in the future, it'd be nice to support arbitary files through arguments.
[ $# -le 0 ] || return 1 [ $# -le 0 ] || return 1
# sanity check:
cd || return 2 cd || return 2
[ -d rc ] && [ -d sh ] && [ -f .zshrc ] && [ -f .-shrc ] && [ -f .bashrc ] || return 3 [ -d rc ] && [ -d sh ] && [ -f .zshrc ] && [ -f .-shrc ] && [ -f .bashrc ] || return 3
# create new output file (with a tilde as not to overwrite just yet):
: > rc/README.md~ || return 4 : > rc/README.md~ || return 4
local line local line
if [ -f rc/README.md ]; then if [ -f rc/README.md ]; then
# copy existing lines up to (and including) the "DOCUMENT" marker:
while IFS= read -r line; do while IFS= read -r line; do
printf '%s\n' "$line" >> rc/README.md~ || return 5 printf '%s\n' "$line" >> rc/README.md~ || return 5
case "$line" in *DOCUMENT*) break;; esac case "$line" in *DOCUMENT*) break;; esac
done < rc/README.md || return 4 done < rc/README.md || return 4
fi fi
# first section:
printf '\n## %s\n' 'shell functions' >> rc/README.md~ || return 5 printf '\n## %s\n' 'shell functions' >> rc/README.md~ || return 5
for f in sh/*; do for f in sh/*; do
[ -e "$f" ] || continue [ -e "$f" ] || continue # make sure glob went through
document2 "$f" || return $? document2 "$f" || return $?
done done
# second section:
printf '\n## %s\n' 'miscellaneous' >> rc/README.md~ || return 5 printf '\n## %s\n' 'miscellaneous' >> rc/README.md~ || return 5
document2 .zshrc || return $? document2 .zshrc || return $?
document2 .bashrc || return $? document2 .bashrc || return $?