sitegen.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/bash
  2. set -e
  3. shopt -s extglob
  4. genpage() { # genpage <base dir> <source file>
  5. local basedir="$(realpath $1)"
  6. local sourcefile="$(realpath $2)"
  7. case "$sourcefile" in
  8. *.md)
  9. pagetitle="$(head -n1 "$sourcefile")"
  10. /usr/bin/pandoc -f markdown_mmd+backtick_code_blocks -t html5 "$sourcefile" > /tmp/page.htm
  11. ;;
  12. *.rst)
  13. pagetitle="$(head -n1 "$sourcefile")"
  14. rst2html5 --template=rst-template.txt "$sourcefile" > /tmp/page.htm
  15. ;;
  16. *)
  17. return
  18. ;;
  19. esac
  20. cd wepage-tpl
  21. m4 -DBASEDIR="${basedir}" -DSOURCEFILE="${sourcefile}" << EOF
  22. define(pageTitle, $pagetitle)dnl
  23. include(\`page_tpl.m4')dnl
  24. EOF
  25. cd ..
  26. }
  27. indexpage() { # indexpage <base> <relative path>
  28. basedir="$1"
  29. path="$2"
  30. echo '<h1 class="dir-list-head">' "${path}" '</h1><ul class="dir-list">'
  31. for fn in $(ls -F "$basedir/$path")
  32. do
  33. if test "x$fn" = 'xindex.htm'; then
  34. continue
  35. fi
  36. case "$fn" in
  37. *.htm)
  38. name="${fn%\.htm}"
  39. ;;
  40. *)
  41. name="${fn}"
  42. ;;
  43. esac
  44. echo "<li><a href=\"${fn}\">${name}</a></li>"
  45. done
  46. echo '</ul>'
  47. }
  48. genindex() { # genindex <base> <relative path>
  49. local basedir="$(realpath $1)"
  50. local sourcefile="${basedir}/$2"
  51. indexpage "$1" "$2" > /tmp/page.htm
  52. cd wepage-tpl
  53. m4 -DBASEDIR="${basedir}" -DSOURCEFILE="${sourcefile}/index.htm" << EOF
  54. define(pageTitle, $pagetitle)dnl
  55. include(\`page_tpl.m4')dnl
  56. EOF
  57. cd ..
  58. }
  59. basedir="${1%/}"
  60. destdir="$(realpath $2)"
  61. if test -e "$destdir"; then
  62. echo "${destdir} exists, please use another directory." >&2
  63. exit 1
  64. fi
  65. FILELIST=($(find ${basedir} -type f -and \( ! -path '*/.*' -and ! -path '*/_*' -and ! -path '*~' \)))
  66. for f in "${FILELIST[@]}"
  67. do
  68. filepath="${f#$basedir}" # remove prefix
  69. install -d "$destdir/$(dirname "$filepath")"
  70. case "$filepath" in
  71. *.rst)
  72. newname="${filepath%.rst}".htm
  73. ;;
  74. *.md)
  75. newname="${filepath%.md}".htm
  76. ;;
  77. *)
  78. cp "$f" "$destdir/$filepath"
  79. continue
  80. ;;
  81. esac
  82. echo genpage "$basedir" "$f" >&2
  83. genpage "$basedir" "$f" > "$destdir/$newname"
  84. done
  85. DIRLIST=($(find "$destdir" -type d))
  86. for d in "${DIRLIST[@]}"
  87. do
  88. if [ ! -f "${d}/index.htm" ]; then
  89. filepath="${d#$destdir}" # remove the prefix
  90. filepath="${filepath#/}" # also remove the prefix /
  91. echo genindex "$destdir" "$filepath" >&2
  92. genindex "${destdir}" "${filepath}" > "${d}/index.htm"
  93. fi
  94. done
  95. cp -r pub "${destdir}/"