shell dot on steroids https://pagure.io/shellfu

sfdoc 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #!/bin/bash
  2. #shellcheck disable=SC1090
  3. . "$(sfpath)" || exit 3
  4. shellfu import pretty
  5. shellfu import sfdoc
  6. usage() {
  7. mkusage "$@" \
  8. "[options] MODULE" \
  9. "[options] --ls [MODULE]" \
  10. "[options] --ls{var|fun} MODULE" \
  11. "[options] --which MODULE" \
  12. "[options] --lsmod" \
  13. "[options] -s|--src MODULE" \
  14. "[options] --export FMT MODULE" \
  15. -c \
  16. "-l, --ls [MODULE] list contents of MODULE if specified," \
  17. " otherwise of all modules" \
  18. "-L, --lsmod show list of modules" \
  19. "--lsfun MODULE show list of functions in MODULE" \
  20. "--lsvar MODULE show list of variables in MODULE" \
  21. "--which MODULE show path to MODULE file" \
  22. "-s, --src MODULE show MODULE source code" \
  23. "-e, --export FMT MODULE export MODULE documentation in" \
  24. " format FMT: 'markdown', 'manpage' and" \
  25. " 'pod' are supported" \
  26. -o \
  27. "-O, --object Treat MODULE as function or variable" \
  28. " name and find respective module first." \
  29. "-d, --debug Turn on debugging" \
  30. "-a, --all Don't ignore hidden (starting with" \
  31. " underscore) modules or objects" \
  32. "--encoding ENC Override encoding of the source text" \
  33. " (default: utf8)" \
  34. "--name NAME Override module name (useful when" \
  35. " exporting from a file and the filename" \
  36. " is not helpful)" \
  37. "-I PTH, --include PTH Include path PTH when searching" \
  38. " for modules; can be specified multiple" \
  39. " times." \
  40. "-o PTH, --only-from PTH Search only under path PTH." \
  41. -- \
  42. "Without command specified, will try to display documentation" \
  43. "in man(1) pager."
  44. }
  45. select_mfile() {
  46. #
  47. # Find and/or verify file that holds module $1
  48. #
  49. local module=$1
  50. local mfile
  51. case $module in
  52. */*) mfile="$module" ;;
  53. *) mfile=$(shellfu _select_mfile "$module") ;;
  54. esac
  55. debug -v mfile
  56. test -n "$mfile" || { warn "no such module found: $module"; return 3; }
  57. test -f "$mfile" || { warn "no such file found: $mfile"; return 3; }
  58. echo "$mfile"
  59. }
  60. find_lesspipe() {
  61. #
  62. # Output correct path to src-hilite-lesspipe.sh
  63. #
  64. find \
  65. /usr/bin/src-hilite-lesspipe.sh \
  66. /usr/share/source-highlight/src-hilite-lesspipe.sh \
  67. 2>/dev/null
  68. }
  69. find_by() {
  70. #
  71. # Find module by object type $1 and name $2
  72. #
  73. local name=$1
  74. sfdoc -a -l \
  75. | grep ":$name$" \
  76. | cut -d: -f1 \
  77. | grep .
  78. }
  79. main() {
  80. local action # what to do
  81. local format # export format
  82. local module # module name or path/to/module.sh
  83. local mspec # module/function name or path/to/module.sh
  84. local m # module helper var
  85. local RealModuleName # name to override eg. if accessing via filename
  86. local encoding # encoding
  87. local mpath # path to module file
  88. local jump # jump to first occurence of this
  89. action=man
  90. format=markdown
  91. encoding=utf8
  92. spectype=mod
  93. #shellcheck disable=SC2034
  94. while true; do case "$1" in
  95. -O|--object) spectype=obj; shift ;;
  96. -d|--debug) PRETTY_DEBUG=true; shift ;;
  97. -a|--all) SFDOC_SHOW_HIDDEN=true; shift ;;
  98. -I|--include) SHELLFU_PATH="$2:$SHELLFU_PATH"; shift 2 || usage -w "no PTH?" ;;
  99. -s|--src) action=src; shift; break ;;
  100. -l|--ls) action=lsx; shift; break ;;
  101. -L|--lsmod) action=lsm; shift; break ;;
  102. --which) action=wch; shift; break ;;
  103. -o|--only-from) SHELLFU_PATH="$2"; SHELLFU_INCLUDE=""; shift 2 || usage -w "no PTH?" ;;
  104. --lsvar) action=lsv; shift; break ;;
  105. --lsfun) action=lsf; shift; break ;;
  106. -e|--export) action=exp; format="$2"; shift 2 || usage -w "no FMT?"; break ;;
  107. --encoding) encoding="$2"; shift 2 || usage -w "no ENC?" ;;
  108. --name) RealModuleName="$2"; shift 2 || usage -w "no NAME?" ;;
  109. -*) usage -w "unknown argument: $1" ;;
  110. *) break ;;
  111. esac done
  112. mspec="$1"; shift
  113. debug -v SHELLFU_INCLUDE SHELLFU_PATH SFDOC_SHOW_HIDDEN
  114. debug -v action format module RealModuleName
  115. case $action:$mspec in
  116. lsx:*|lsm:*) true ;;
  117. *:) usage -w "no MODULE?" ;;
  118. esac
  119. case $spectype in
  120. obj) module=$(find_by "$mspec") \
  121. || die "no module found with: $mspec"
  122. jump="$mspec"
  123. ;;
  124. *) module=$mspec ;;
  125. esac
  126. case $action in
  127. lsm) : ;;
  128. *) mpath=$(select_mfile "$module") || die ;;
  129. esac
  130. case $action in
  131. exp) # --export
  132. grep -qwe "$format" <<<manpage,markdown,pod \
  133. || die "unknown format: $format"
  134. sfdoc__export "$format" "${RealModuleName:-$module}" "$mpath" "$encoding"
  135. ;;
  136. lsm) # --lsmod
  137. sfdoc__ls_m | LC_ALL=C sort
  138. ;;
  139. lsv) # --lsvar MODULE
  140. sfdoc__ls v "$mpath" | LC_ALL=C sort
  141. ;;
  142. lsf) # --lsfun MODULE
  143. sfdoc__ls f "$mpath" | LC_ALL=C sort
  144. ;;
  145. lsx) # --ls [MODULE]
  146. case $module in
  147. "") shellfu _list_mfiles ;;
  148. *) echo "$mpath" ;;
  149. esac \
  150. | while read -r m;
  151. do
  152. sfdoc__ls v "$m"
  153. sfdoc__ls f "$m"
  154. done \
  155. | LC_ALL=C sort
  156. ;;
  157. man)
  158. which pod2man &>/dev/null \
  159. || die "pod2man is missing; cannot use man page mode"
  160. manfile=$(mktemp -t "sfdoc.manfile.XXXXXXXX")
  161. sfdoc__export manpage "${RealModuleName:-$module}" "$mpath" "$encoding" >"$manfile"
  162. manless="$LESS"
  163. test -n "$jump" && manless+=" +/$jump"
  164. LESS="$manless" man "$manfile"
  165. rm "$manfile"
  166. ;;
  167. src)
  168. local lesspipe
  169. lesspipe=$(find_lesspipe)
  170. if test -n "$lesspipe";
  171. then
  172. LESS="$LESS -R " \
  173. LESSOPEN="| $lesspipe %s" \
  174. less "$mpath"
  175. else
  176. less "$mpath"
  177. fi
  178. ;;
  179. wch)
  180. echo "$mpath"
  181. ;;
  182. esac
  183. }
  184. main "$@"