index.sh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (C) 2017 Alyssa Rosenzweig <alyssa@rosenzweig.io>
  4. # Copyright (C) 2017 Michael Reed <michael@michaelreed.io>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. BLOGTITLE="QTAU News"
  19. BLOGBASE="https://qtau.de/"
  20. BLOGDESCRIPTION="News on QTAU development"
  21. # MANIFEST determines the order of news articles in news/index.md
  22. FILES=$(< news/MANIFEST)
  23. # usage: title file
  24. title() {
  25. sed -n 1p "$1" | sed -e s-^..--
  26. }
  27. # usage: meta file
  28. meta() {
  29. file=$1
  30. URL=$(printf '%s\n' "${file%.md}.html" | sed -e s-news/--)
  31. printf '%s\n' "[$(title "$file")]($URL){.title}"
  32. printf '%s\n' "[$(sed -n 3p "$file" | sed -e s-^..--)]{.date}"
  33. printf '\n'
  34. tail -n +5 "$file" | perl -p0e 's/(\.|\?|\!)( |\n)(.|\n)*/.../g'
  35. printf '\n'
  36. printf '\n'
  37. }
  38. # usage: rss_header
  39. rss_header() {
  40. printf '%s\n' '<rss version="2.0">'
  41. printf '%s\n' '<channel>'
  42. printf '%s\n' "<title>$BLOGTITLE</title>"
  43. printf '%s\n' "<link>${BLOGBASE}news/</link>"
  44. printf '%s\n' "<description>$BLOGDESCRIPTION</description>"
  45. }
  46. # usage: rss_main file
  47. rss_main() {
  48. file=$1
  49. # render content and escape
  50. desc=$(sed -e 's/</\&lt;/g' "${file%.md}.bare.html" | sed -e 's/>/\&gt;/g')
  51. url="${file%.md}.html"
  52. printf '%s\n' '<item>'
  53. printf '%s\n' "<title>$(title "$file")</title>"
  54. printf '%s\n' "<link>$BLOGBASE$url</link>"
  55. printf '%s\n' "<description>$desc</description>"
  56. printf '%s\n' '</item>'
  57. }
  58. # usage: rss_footer
  59. rss_footer() {
  60. printf '%s\n' '</channel>'
  61. printf '%s\n' '</rss>'
  62. }
  63. # generate the index file
  64. cat news-list.md > news/index.md
  65. for f in $FILES
  66. do
  67. meta "$f" >> news/index.md
  68. done
  69. # generate the RSS index
  70. rss_header > news/feed.xml
  71. for f in $FILES
  72. do
  73. rss_main "$f" >> news/feed.xml
  74. done
  75. rss_footer >> news/feed.xml
  76. cp news/feed.xml feed.xml