publish.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. ENGFILE=${FILE%.pt}
  23. ENGFILE=${ENGFILE%.pl}
  24. PLFILE="${ENGFILE}.pl"
  25. PTFILE="${ENGFILE}.pt"
  26. TMPFILE=$(mktemp -t transit_www.XXXXXXXXXX)
  27. firstchar=$(head -c 1 ${1})
  28. if [[ "${firstchar}" == "%" ]]; then
  29. title="$(head -n1 ${1})"
  30. pagetext="$(tail -n+2 ${1})"
  31. else
  32. title="$(head -n4 ${1})"
  33. pagetext="$(tail -n+5 ${1})"
  34. fi
  35. langselect="[English](${ENGFILE}.md) -- [polski](${ENGFILE}.pl.md) -- [Português](${ENGFILE}.pt.md)"
  36. printf "%s\n" "${title}" > "$TMPFILE"
  37. if [[ $FILE != "./fdl-1.3" && $FILE != "fdl-1.3" &&
  38. $FILE != "./license" && $FILE != "license" ]]; then
  39. printf "\n%s\n" "${langselect}" >> "$TMPFILE"
  40. fi
  41. printf "\n%s\n" "${pagetext}" >> "$TMPFILE"
  42. OPTS="-T Transit"
  43. if [[ $ENGFILE == "index" || $ENGFILE == "./index" ]]; then
  44. OPTS="--css /headercenter.css"
  45. else
  46. if [[ $FILE == $PTFILE ]]; then # portuguese
  47. if [[ $ENGFILE == *index ]]
  48. then
  49. DEST="../index.pt.html"
  50. else
  51. DEST="./index.pt.html"
  52. fi
  53. elif [[ $FILE == $PLFILE ]]; then # polish
  54. if [[ $ENGFILE == *index ]]
  55. then
  56. DEST="../index.pl.html"
  57. else
  58. DEST="./index.pl.html"
  59. fi
  60. else # english
  61. if [[ $ENGFILE == *index ]]
  62. then
  63. DEST="../"
  64. else
  65. DEST="./"
  66. fi
  67. fi
  68. fi
  69. if [[ $ENGFILE != "index" && $ENGFILE != "./index" ]]; then
  70. if [[ "$FILE" == "$ENGFILE" ]]; then
  71. RETURN="<strong><a href='/contrib.html#editing-the-website-and-documentation-wiki-style'>Edit this page</a></strong> -- <a href='$DEST'>Back to previous index</a>"
  72. OPTS="-T Transit"
  73. elif [[ "$FILE" == "$PTFILE" ]]; then
  74. RETURN="<strong><a href='/contrib.pt.html#editar-o-website-e-documentação-tal-como-se-tratasse-de-um-wiki'>Editar esta p&aacute;gina</a></strong> -- <a href='$DEST'>Voltar &agrave; p&aacute;gina anterior</a>"
  75. OPTS="-T Transit"
  76. elif [[ "$FILE" == "$PLFILE" ]]; then
  77. RETURN="<strong><a href='/contrib.pl.html#edytowanie-strony-i-dokumentacji-w-stylu-wiki'>Edytuj t&#281 stron&#281</a></strong> -- <a href='$DEST'>Powr&oacute;&#263; do poprzedniej strony</a>"
  78. OPTS="-T Transit"
  79. fi
  80. fi
  81. if [[ "$FILE" == "${PTFILE}" ]]; then # portuguese site
  82. if [[ $FILE != "./fdl-1.3" && $FILE != "fdl-1.3" &&
  83. $FILE != "./aup.pt" && $FILE != "aup.pt" ]]; then
  84. cat footer.pt.md >> "$TMPFILE"
  85. fi
  86. elif [[ "$FILE" == "${PLFILE}" ]]; then # polish site
  87. if [[ $FILE != "./fdl-1.3" && $FILE != "fdl-1.3" &&
  88. $FILE != "./aup.pl" && $FILE != "aup.pl" ]]; then
  89. cat footer.pl.md >> "$TMPFILE"
  90. fi
  91. else
  92. if [[ $FILE != "./fdl-1.3" && $FILE != "fdl-1.3" &&
  93. $FILE != "./aup" && $FILE != "aup" ]]; then
  94. cat footer.md >> "$TMPFILE"
  95. fi
  96. fi
  97. # change out .md -> .html
  98. sed -i -e 's/\.md\(#[a-z\-]*\)*)/.html\1)/g' "$TMPFILE"
  99. sed -i -e 's/\.md\(#[a-z\-]*\)*]/.html\1]/g' "$TMPFILE"
  100. # change out .md -> .html
  101. sed -i -e 's/\.md\(#[a-z\-]*\)*)/.html\1)/g' "$TMPFILE"
  102. sed -i -e 's/\.md\(#[a-z\-]*\)*]/.html\1]/g' "$TMPFILE"
  103. # work around issue #2872
  104. TOC=$(grep -q "^x-toc-enable: true$" "$TMPFILE" && printf '%s\n' "--toc --toc-depth=2") || TOC=""
  105. # work around heterogenous pandoc versions
  106. SMART=$(pandoc -v | grep -q '2\.0' || printf '%s\n' "--smart") || SMART=""
  107. # chuck through pandoc
  108. #
  109. # $OPTS must not be quoted, otherwise pandoc interprets '--css /headercenter.css'
  110. # as one argument, when it is actually two.
  111. pandoc $TOC $SMART "$TMPFILE" -s --css /global.css $OPTS \
  112. --template template.html --metadata return="$RETURN" > "$FILE.html"
  113. # generate section title anchors as [link]
  114. 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"
  115. # clean up temporary filerm -f "$TMPFILE"