rivet: a simple static site generator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
4.5 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #!/bin/sh
  2. set -e
  3. unset SKIP_FOOTER
  4. unset SKIP_HEADER
  5. unset SKIP_LIST
  6. unset VERBOSE
  7. unset PRINT_HELP
  8. unset SKIP_SITEMAP
  9. usage() {
  10. printf "Usage: rivet [-hovelfsu] [-p string] [-o destdir] srcdir domain\n"
  11. printf "\t-h: prints this message\n"
  12. printf "\t-o [destdir]: specifies the output direcotory to be [destdir]\n"
  13. printf "\t-p: [string]: Rename the \"Pages\" section to [string]\n"
  14. printf "\t-v: Makes the script verbose\n"
  15. printf "\t-e: Do not prepend _header.html to .html files\n"
  16. printf "\t-f: Do not prepend _footer.html to .html files\n"
  17. printf "\t-l: Do not generate \"Pages\" section in index.html\n"
  18. printf "\t-s: Do not generate sitemap.xml\n"
  19. printf "\t-u: Makes all references to the url 'http' instead of 'https'\n"
  20. exit 2
  21. }
  22. convert() {
  23. tmpfile="tmpconvfile.tmp"
  24. infile="$1"
  25. outfile="${infile%???}.html"
  26. cp "$1" "$tmpfile"
  27. # TODO: convert links to .md to .html
  28. lowdown -s -Thtml -o "$outfile" "$tmpfile"
  29. rm -f "$tmpfile" "$1"
  30. }
  31. # Check dependencies
  32. if ! command -v lowdown > /dev/null; then
  33. echo "lowdown is not installed"
  34. exit
  35. fi
  36. destdir='dst'
  37. prefix='https'
  38. linksec='Pages'
  39. while getopts 'o:vhelfsup:' c
  40. do
  41. case "$c" in
  42. o) destdir=${OPTARG%%\/} ;;
  43. v) VERBOSE=true ;;
  44. h) PRINT_HELP=true ;;
  45. e) SKIP_HEADER=true ;;
  46. l) SKIP_LIST=true ;;
  47. f) SKIP_FOOTER=true ;;
  48. s) SKIP_SITEMAP=true ;;
  49. u) prefix='http' ;;
  50. p) linksec="$OPTARG" ;;
  51. *) ;;
  52. esac
  53. done
  54. shift $((OPTIND - 1))
  55. if ! [ "$1" ] || ! [ "$2" ]; then
  56. echo "Not enough arguments"
  57. usage
  58. fi
  59. src="$1"
  60. srcdir=${src%%\/}
  61. unset src
  62. # Check if index.md is present
  63. if ! [ -e "$srcdir"/index.md ]; then
  64. echo "Missing index.md in $srcdir"
  65. exit 1
  66. fi
  67. # Check header and footer files
  68. if ! [ -e "$headerfile" ]; then
  69. echo "Missing _header.html in $srcdir"
  70. exit 1
  71. fi
  72. if ! [ -e "$footerfile" ]; then
  73. echo "Missing _footer.html in $srcdir"
  74. exit 1
  75. fi
  76. # Remove junk from {header,footer} files
  77. sed -i 's/<header.*>//' "$headerfile"
  78. sed -i 's/<\/header>//' "$headerfile"
  79. sed -i 's/<footer.*>//' "$footerfile"
  80. sed -i 's/<\/footer>//' "$footerfile"
  81. # Remove any junk from the domain eg. [https://]domain.com[/]
  82. url="$(echo "$2" |
  83. sed -e 's/^https*:\/\///' |
  84. sed -e 's/\/$//' |
  85. sed -e 's/[]\/$*.^[]/\\&/g')"
  86. headerfile=$srcdir/_header.html
  87. footerfile=$srcdir/_footer.html
  88. if [ "$PRINT_HELP" ]; then
  89. usage
  90. fi
  91. if [ "$VERBOSE" ]; then
  92. set -x
  93. fi
  94. if ! [ -d "$srcdir" ]; then
  95. echo "Error: missing source direcotry"
  96. usage
  97. fi
  98. mkdir -p "$destdir"
  99. cp -r "$srcdir"/* "$destdir"
  100. rm -f "$destdir"/_header.html "$destdir"/_footer.html
  101. # Convert markdown files
  102. find "$destdir" -name "*.md" |
  103. while IFS="" read -r file; do
  104. convert "$file"
  105. done
  106. # Prepare the header
  107. if ! [ "$SKIP_HEADER" ]; then
  108. find "$destdir" -name "*.html" |
  109. while IFS="" read -r file; do
  110. sed -i "/<head>/r $headerfile" "$file"
  111. done
  112. fi
  113. # Prepate the footer
  114. if ! [ "$SKIP_FOOTER" ]; then
  115. tmpfoot="tmpfootfile.tmp"
  116. cp "$footerfile" "$tmpfoot"
  117. sed -i '1s/^/<footer>/' "$tmpfoot"
  118. echo '</footer>' >> "$tmpfoot"
  119. find "$destdir" -name "*.html" |
  120. while IFS="" read -r file; do
  121. sed -i "/<\/body>/r $tmpfoot" "$file"
  122. done
  123. rm -f "$tmpfoot"
  124. fi
  125. # Prepare the sitemap & file list
  126. if ! [ "$SKIP_SITEMAP" ] || ! [ "$SKIP_LIST" ]; then
  127. linklist="linklist.tmp"
  128. # echo "" > "$linklist"
  129. rm -f "$linklist" "$destdir"/sitemap.xml
  130. find "$destdir" -name "*.html" |
  131. while IFS="" read -r file; do
  132. echo "${file#$destdir/}" >> "$linklist"
  133. done
  134. if ! [ "$SKIP_LIST" ]; then
  135. tmpfile="linkindex.tmp"
  136. rm -f "$tmpfile"
  137. cat << EOF >> "$tmpfile"
  138. <div id="map">
  139. <h2 id="Pages">"$linksec"</h2>
  140. EOF
  141. while IFS="" read -r line; do
  142. if echo "$line" | grep -q 'index\.html'; then
  143. continue
  144. fi
  145. title="$(grep -e '^<title>.*<\/title>' "$destdir"/"$line" |
  146. sed -e 's/<title>//' -e 's/<\/title>//')"
  147. if ! [ "$title" ]; then
  148. title=${line%?????}
  149. fi
  150. printf "<p><a href=\"%s\">%s</a></p>\n" "$line" "$title" >> "$tmpfile"
  151. done < "$linklist"
  152. echo '</div>' >> "$tmpfile"
  153. sed -i '/<\/body>/i REPLACE' "$destdir"/index.html
  154. sed -i "/^REPLACE/r $tmpfile" "$destdir"/index.html
  155. sed -i 's/^REPLACE//' "$destdir"/index.html
  156. rm -f "$tmpfile"
  157. fi
  158. if ! [ "$SKIP_SITEMAP" ]; then
  159. sed -i -e "s/^/$prefix:\/\/$url\//" "$linklist"
  160. cat << EOF >> "$destdir"/sitemap.xml
  161. <?xml version="1.0" encoding="UTF-8"?>
  162. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  163. </urlset>
  164. EOF
  165. while IFS="" read -r line; do
  166. sed -i "/<\/urlset>/i \
  167. <url><loc>$line<\/loc><\/url>" "$destdir"/sitemap.xml
  168. done < "$linklist"
  169. sed -i 's/^<url>/\t<url>/' "$destdir"/sitemap.xml
  170. fi
  171. rm -f "$linklist"
  172. fi
  173. exit