seen.sh 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/sh
  2. # Seen, a simple static blog generator
  3. # Copyright (C) 2021,2022 Ferass 'Vitali64' EL HAFIDI <vitali64pmemail@protonmail.com>
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # Please see the LICENSE file for more details.
  9. usage() {
  10. echo "Usage : ./seen [OPTIONS]"
  11. echo "--help : Print this help message"
  12. echo "--clear : Remove generated blog"
  13. echo "--license : Show license"
  14. }
  15. die() {
  16. echo "-> [$(date +%R)] (!) ERROR: Unexpected error, please report : $1 (!)"
  17. exit 1
  18. }
  19. echo "Seen, a simple static blog generator"
  20. if [ "$1" = "--clear" ]; then
  21. echo "-> [$(date +%R)] Deleting generated blog ..."
  22. rm -Rf www/* || die "Cannot delete generated blog!"
  23. exit 0
  24. elif [ "$1" = "--help" ]; then
  25. usage
  26. exit 0
  27. elif [ "$1" = "--license" ]; then
  28. echo "Copyright (C) 2021 Vitali64 <vitali64pmemail@protonmail.com>"
  29. echo
  30. cat "./LICENSE"
  31. exit 0
  32. elif [ "$1" = "" ]; then
  33. echo
  34. else
  35. echo "-> [$(date +%R)] (!) ERROR : Invalid option, cannot continue. (!)"
  36. usage
  37. exit 1
  38. fi
  39. mkdir -p "www" || die "Cannot create www/ directory: mkdir -p returns an error!" # Create the www/ folder if removed
  40. # ---HTML---
  41. cat "templates/header.html" > www/index.html || die "Cannot insert header into index.html!" # Erase www/index.html and insert the header
  42. cat "templates/indextext.html" >> www/index.html || die "Cannot insert indextext into index.html!" # insert indextext
  43. articles="$(find articles/*.md|sed -e 's@.md@@g' -e 's@articles/@@g')" || die "Unknown error" # Detect articles
  44. # Set the defaults
  45. name="(!) no name (!)"
  46. desc="(!) no description (!)"
  47. date="1st January 1970"
  48. for line in ${articles} # In the 'for' loop
  49. do
  50. . "articles/${line}.cfg" || die "Cannot read ${line}.cfg file!" # Override the defaults
  51. mkdir "www/articles" -p
  52. cat "templates/header.html" > "www/articles/${line}.html" || die "Cannot insert header into ${line}.html!" # Erase <article>.html and insert the header
  53. markdown "articles/${line}.md" >> "www/articles/${line}.html" || hoedown "articles/${line}.md" >> "www/articles/${line}.html" || die "Cannot convert ${line}.md to html!" # Convert the markdown text to html
  54. cat "templates/footer.html" >> "www/articles/${line}.html" || die "Cannot insert footer into ${line}.html!" # Insert the footer into the article page
  55. cp -r templates/*.css www/. # Add css files if present
  56. echo "-> [$(date +%R)] Adding ${line} entry to index.html ..." # Add an entry in index.html
  57. sed -e "s@path-of-article@articles/${line}@" -e "s@name-of-article@${name}@" -e "s@date-of-article@${date}@" -e "s@description-of-article@${desc}@" \
  58. "templates/article.html">> "www/index.html" || die "Cannot add entry to index.html!"
  59. done
  60. cat "templates/footer.html" >> "www/index.html" # Insert the footer into the index.html
  61. # ---RSS---
  62. # Set the defaults
  63. rssEnabled="n"
  64. rssBlogTitle="blog"
  65. rssBlogDescription="It's a blog"
  66. . "./rss.cfg" # Override the defaults
  67. if [ "${rssEnabled}" = "n" ]; then
  68. echo "-> [$(date +%R)] RSS is disabled, skipping..."
  69. exit 0
  70. fi
  71. # Header of the RSS XML file
  72. printf '%s\n' '<rss version="2.0">' > "www/rss.xml"
  73. printf '%s\n' '<channel>' >> "www/rss.xml"
  74. printf '%s\n' "<title>${rssBlogTitle}</title>" >> "www/rss.xml"
  75. printf '%s\n' "<link>BLANK</link>" >> "www/rss.xml"
  76. printf '%s\n' "<description>$rssBlogDescription</description>" >> "www/rss.xml"
  77. for line in ${articles} # Items
  78. do
  79. echo "-> [$(date +%R)] Inserting XML ${line} item ..."
  80. . "articles/${line}.cfg"
  81. printf '%s\n' '<item>' >> "www/rss.xml"
  82. printf '%s\n' "<title>${name}</title>" >> "www/rss.xml"
  83. printf '%s\n' "<description>$(cat articles/${line}.md)</description>" >> "www/rss.xml"
  84. printf '%s\n' '</item>' >> "www/rss.xml"
  85. done
  86. # Footer of the RSS XML file
  87. printf '%s\n' '</channel>' >> "www/rss.xml"
  88. printf '%s\n' '</rss>' >> "www/rss.xml"