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

sfdoc 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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" \
  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 Override encoding of the source text" \
  33. " (default: utf8)" \
  34. "--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. action=man
  89. format=markdown
  90. encoding=utf8
  91. spectype=mod
  92. #shellcheck disable=SC2034
  93. while true; do case "$1" in
  94. -O|--object) spectype=obj; shift ;;
  95. -d|--debug) PRETTY_DEBUG=true; shift ;;
  96. -a|--all) SFDOC_SHOW_HIDDEN=true; shift ;;
  97. -I|--include) SHELLFU_PATH="$2:$SHELLFU_PATH"; shift 2 || usage ;;
  98. -s|--src) action=src; shift; break ;;
  99. -l|--ls) action=lsx; shift; break ;;
  100. -L|--lsmod) action=lsm; shift; break ;;
  101. --which) action=wch; shift; break ;;
  102. -o|--only-from) SHELLFU_PATH="$2"; SHELLFU_INCLUDE=""; shift 2 || usage ;;
  103. --lsvar) action=lsv; shift; break ;;
  104. --lsfun) action=lsf; shift; break ;;
  105. -e|--export) action=exp; format="$2"; shift 2 || usage; break ;;
  106. --encoding) encoding="$2"; shift 2 || usage ;;
  107. --name) RealModuleName="$2"; shift 2 || usage ;;
  108. -*) usage ;;
  109. *) break ;;
  110. esac done
  111. mspec="$1"; shift
  112. debug -v SHELLFU_INCLUDE SHELLFU_PATH SFDOC_SHOW_HIDDEN
  113. debug -v action format module RealModuleName
  114. case $action:$mspec in
  115. lsx:*|lsm:*|wch:*) true ;;
  116. *:) usage ;;
  117. esac
  118. case $spectype in
  119. obj) module=$(find_by "$mspec") \
  120. || die "no module found with: $mspec" ;;
  121. *) module=$mspec ;;
  122. esac
  123. case $action in
  124. lsm) : ;;
  125. *) mpath=$(select_mfile "$module") || die ;;
  126. esac
  127. case $action in
  128. exp) # --export
  129. grep -qw "$format" <<<manpage,markdown,pod \
  130. || die "unknown format: $format"
  131. sfdoc__export "$format" "${RealModuleName:-$module}" "$mpath" "$encoding"
  132. ;;
  133. lsm) # --lsmod
  134. sfdoc__ls_m | LC_ALL=C sort
  135. ;;
  136. lsv) # --lsvar MODULE
  137. sfdoc__ls v "$mpath" | LC_ALL=C sort
  138. ;;
  139. lsf) # --lsfun MODULE
  140. sfdoc__ls f "$mpath" | LC_ALL=C sort
  141. ;;
  142. lsx) # --ls [MODULE]
  143. case $module in
  144. "") shellfu _list_mfiles ;;
  145. *) echo "$mpath" ;;
  146. esac \
  147. | while read -r m;
  148. do
  149. sfdoc__ls v "$m"
  150. sfdoc__ls f "$m"
  151. done \
  152. | LC_ALL=C sort
  153. ;;
  154. man)
  155. which pod2man &>/dev/null \
  156. || die "pod2man is missing cannot use man page mode"
  157. man --version |& grep -qF 'man 2.' \
  158. || die "unknown \`man\` (only 2.x is supported): $(man --version 2>&1)"
  159. sfdoc__export manpage "${RealModuleName:-$module}" "$mpath" "$encoding" \
  160. | man -l -
  161. ;;
  162. src)
  163. local lesspipe
  164. lesspipe=$(find_lesspipe)
  165. if test -n "$lesspipe";
  166. then
  167. LESS="$LESS -R " \
  168. LESSOPEN="| $lesspipe %s" \
  169. less "$mpath"
  170. else
  171. less "$mpath"
  172. fi
  173. ;;
  174. wch)
  175. echo "$mpath"
  176. ;;
  177. esac
  178. }
  179. main "$@"