index.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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="Leah Rowe's Sleepless Rants"
  19. BLOGBASE="https://blog.vimuser.org/"
  20. BLOGDESCRIPTION="Leah Rowe's Sleepless Rants"
  21. # MANIFEST determines the order of news articles in news/index.md
  22. FILES=$(< 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-article/--)
  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}</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 > index.md
  65. for f in $FILES
  66. do
  67. meta "$f" >> index.md
  68. done
  69. # generate the RSS index
  70. rss_header > feed.xml
  71. for f in $FILES
  72. do
  73. rss_main "$f" >> feed.xml
  74. done
  75. rss_footer >> feed.xml