gen-doc.sh 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/sh
  2. #
  3. # Generate documentation
  4. #
  5. basedir="$(dirname "$0")"
  6. [ "$(echo "$basedir" | cut -c1)" = '/' ] || basedir="$PWD/$basedir"
  7. srcdir="$basedir/.."
  8. die()
  9. {
  10. echo "$*" >&2
  11. exit 1
  12. }
  13. gen_rst()
  14. {
  15. local rst="$1"
  16. local docname="$(basename "$rst" .rst)"
  17. local dir="$(dirname "$rst")"
  18. local html="$dir/$docname.html"
  19. echo "Generating $(realpath --relative-to="$srcdir" "$html") from $(realpath --relative-to="$srcdir" "$rst") ..."
  20. python3 -m readme_renderer -o "$html" "$rst" ||\
  21. die "Failed to generate"
  22. }
  23. gen_pydoc()
  24. {
  25. local py="$1"
  26. local basename="$(basename "$py" .py)"
  27. local dir="$(dirname "$py")"
  28. local reldir="$(realpath --relative-to="$srcdir" "$dir")"
  29. local targetdoc="$srcdir/doc/api/$reldir"
  30. if [ "$reldir" = "tests" -o \
  31. "$basename" = "__init__" -o \
  32. "$basename" = "__main__" -o \
  33. "$basename" = "setup" -o \
  34. "$basename" = "examplescript" ]; then
  35. return
  36. fi
  37. (
  38. mkdir -p "$targetdoc" || die "Failed to create target dir"
  39. cd "$targetdoc" || die "Failed to cd"
  40. export PYTHONPATH="$srcdir"
  41. pydoc3 -w "$py" || die "Failed to generate pydoc3"
  42. ) || die
  43. }
  44. opt_genrst=1
  45. opt_genpydoc=1
  46. while [ $# -ge 1 ]; do
  47. case "$1" in
  48. -r)
  49. opt_genrst=0
  50. ;;
  51. -p)
  52. opt_genpydoc=0
  53. ;;
  54. *)
  55. die "Unknown option: $1"
  56. ;;
  57. esac
  58. shift
  59. done
  60. if [ $opt_genrst -ne 0 ]; then
  61. for i in $(find "$srcdir" -name '*.rst' -print); do
  62. gen_rst "$i"
  63. done
  64. fi
  65. if [ $opt_genpydoc -ne 0 ]; then
  66. for i in $(find "$srcdir" -name '*.py' -print); do
  67. gen_pydoc "$i"
  68. done
  69. fi
  70. exit 0