publish.sh 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (C) 2017 Alyssa Rosenzweig <alyssa@rosenzweig.io>
  4. # Copyright (C) 2017 Leah Rowe <info@minifree.org>
  5. # Copyright (C) 2017 Michael Reed <michael@michaelreed.io>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. [ "x${DEBUG+set}" = 'xset' ] && set -v
  20. set -e
  21. FILE=${1%.md}
  22. TMPFILE=$(mktemp -t sleepless_www.XXXXXXXXXX)
  23. firstchar=$(head -c 1 ${1})
  24. if [[ "${firstchar}" == "%" ]]; then
  25. title="$(head -n1 ${1})"
  26. pagetext="$(tail -n+2 ${1})"
  27. else
  28. title="$(head -n4 ${1})"
  29. pagetext="$(tail -n+5 ${1})"
  30. fi
  31. printf "%s\n" "${title}" > "$TMPFILE"
  32. printf "\n<p><strong>Do you support my art work? <a href=\"https://www.patreon.com/libreleah\">Click here to financially support my work</a> (via Patreon)</strong><br/><strong><a href=\"https://twitter.com/n4of7\">I am also on Twitter</a></strong></p>\n" >> "$TMPFILE"
  33. printf "\n%s\n" "${pagetext}" >> "$TMPFILE"
  34. OPTS="-T Sleepless"
  35. if [[ $FILE == "index" || $FILE == "./index" ]]; then
  36. OPTS="--css /headercenter.css"
  37. else
  38. if [[ $FILE == *index ]]
  39. then
  40. DEST="../"
  41. else
  42. DEST="./"
  43. fi
  44. fi
  45. if [[ $FILE != "index" && $FILE != "./index" ]]; then
  46. RETURN=""
  47. OPTS="-T Sleepless"
  48. fi
  49. if [[ $FILE != "./fdl-1.3" && $FILE != "fdl-1.3" &&
  50. $FILE != "./aup" && $FILE != "aup" ]]; then
  51. cat footer.md >> "$TMPFILE"
  52. fi
  53. # change out .md -> .html
  54. sed -i -e 's/\.md\(#[a-z\-]*\)*)/.html\1)/g' "$TMPFILE"
  55. sed -i -e 's/\.md\(#[a-z\-]*\)*]/.html\1]/g' "$TMPFILE"
  56. # change out .md -> .html
  57. sed -i -e 's/\.md\(#[a-z\-]*\)*)/.html\1)/g' "$TMPFILE"
  58. sed -i -e 's/\.md\(#[a-z\-]*\)*]/.html\1]/g' "$TMPFILE"
  59. # work around issue #2872
  60. TOC=$(grep -q "^x-toc-enable: true$" "$TMPFILE" && printf '%s\n' "--toc --toc-depth=2") || TOC=""
  61. # work around heterogenous pandoc versions
  62. SMART=$(pandoc -v | grep -q '2\.0' || printf '%s\n' "--smart") || SMART=""
  63. # chuck through pandoc
  64. #
  65. # $OPTS must not be quoted, otherwise pandoc interprets '--css /headercenter.css'
  66. # as one argument, when it is actually two.
  67. pandoc $TOC $SMART "$TMPFILE" -s --css /global.css $OPTS \
  68. --template template.html --metadata return="$RETURN" > "$FILE.html"
  69. # generate section title anchors as [link]
  70. sed -i -e 's_^<h\([123]\) id="\(.*\)">\(.*\)</h\1>_<div class="h"><h\1 id="\2">\3</h\1><a aria-hidden="true" href="#\2">[link]</a></div>_' "$FILE.html"
  71. # clean up temporary filerm -f "$TMPFILE"