comasy.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #!/usr/bin/env sh
  2. #
  3. # Comasy - COntent MAnagement SYstem
  4. # Copyright (C) 2019 SEPBIT
  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 <https://www.gnu.org/licenses/>.
  18. #
  19. # dash version 0.5
  20. #
  21. # @author Vitor Guia <contato@vitor.guia.nom.br>
  22. # @copyright 2019 SEPBIT
  23. # @license http://www.gnu.org/licenses GPL-3.0-or-later
  24. # @see https://notabug.org/sepbit/comasy Repository of Comasy
  25. #
  26. #
  27. # Powered by coreutils, dash and sed
  28. #
  29. #
  30. # Global variables
  31. #
  32. . ./global.sh
  33. #
  34. # Parses
  35. #
  36. parseVars(){
  37. echo "$1" | sed -e 's/.html/.sh/'
  38. }
  39. parseTemplate(){
  40. echo "$1" | awk '{split($0,a,"/"); print a[3]}'
  41. }
  42. parseUri(){
  43. echo "$1" | sed -e 's/.\/src\/pages\///' -e 's/.\/src\/broadcasts\///' \
  44. -e 's/.\/src\/posts\///' -e 's/.html//'
  45. }
  46. #
  47. # Build atom
  48. #
  49. buildAtom() {
  50. find './src/broadcasts' './src/posts' -type f \
  51. -name '*.html' -exec ls -Rt {} + > atom.tmp
  52. eval "echo \"$(sed -e "s/\"/\\\\\"/g" ./src/templates/broadcasts.xml)\"" \
  53. > ./www/broadcasts.xml
  54. eval "echo \"$(sed -e "s/\"/\\\\\"/g" ./src/templates/posts.xml)\"" \
  55. > ./www/posts.xml
  56. while IFS= read -r line; do
  57. eval "$(cat "$(parseVars "$line")")"
  58. { echo " <entry>
  59. <title>$title</title>
  60. <link href=\"$SITEBASE/$(parseUri "$line")\" />
  61. <id>$SITEBASE/$(parseUri "$line")</id>
  62. <updated>$(date -Iseconds -r "$line")</updated>
  63. <summary>$summary</summary>
  64. <content type=\"html\">
  65. <![CDATA[";
  66. eval "echo \"$(sed -e "s/\"/\\\\\"/g" "$line")\"";
  67. echo " ]]>
  68. </content>"; } >> ./www/"$(parseTemplate "$line")".xml
  69. if [ "$(parseTemplate "$line")" = "broadcasts" ]; then
  70. echo " <link rel=\"enclosure\" type=\"$enclosureType\" \
  71. length=\"$enclosureLength\" href=\"$enclosure\" />" >> ./www/broadcasts.xml
  72. fi
  73. echo " </entry>" >> ./www/"$(parseTemplate "$line")".xml
  74. done < atom.tmp
  75. echo "</feed>" >> ./www/broadcasts.xml && echo "</feed>" >> ./www/posts.xml
  76. rm atom.tmp
  77. }
  78. #
  79. # Build home
  80. #
  81. buildHome(){
  82. eval "$(cat ./src/home.sh)"
  83. sed -e "s/\"/\\\\\"/g" ./src/home.html > ./www/index.html
  84. eval "echo \"$(cat ./www/index.html)\"" \
  85. > ./www/"$(parseUri "$line")"/index.html
  86. }
  87. #
  88. # Build html
  89. #
  90. buildHtml(){
  91. while IFS= read -r line; do
  92. if [ "$enclosureType" = "audio/ogg" ]; then
  93. eval "player=\"$(sed -e "s/\"/\\\\\"/g" ./src/templates/html5Audio.html)\""
  94. fi
  95. if [ "$enclosureType" = "video/ogg" ]; then
  96. eval "player=\"$(sed -e "s/\"/\\\\\"/g" ./src/templates/html5Video.html)\""
  97. fi
  98. podcast="/assets/sounds/$(parseUri "$line").ogg" && export podcast
  99. updated="$(date -Iseconds -r "$line")" && export updated
  100. mkdir -p ./www/"$(parseUri "$line")"
  101. eval "$(cat "$(parseVars "$line")")"
  102. sed -e "/\$content/r $line" -e "/\$content/d" \
  103. ./src/templates/"$(parseTemplate "$line")".html \
  104. > ./www/"$(parseUri "$line")"/index.html
  105. sed -i -e "s/\"/\\\\\"/g" ./www/"$(parseUri "$line")"/index.html
  106. eval "echo \"$(cat www/"$(parseUri "$line")"/index.html)\"" \
  107. > ./www/"$(parseUri "$line")"/index.html
  108. done < html.tmp
  109. }
  110. #
  111. # build sitemap
  112. #
  113. buildSiteMap() {
  114. eval "echo \"$(sed -e "s/\"/\\\\\"/g" ./src/templates/sitemap.xml)\"" \
  115. > ./www/sitemap.xml
  116. while IFS= read -r line; do
  117. echo " <url>
  118. <loc>$(echo "$SITEBASE/$(parseUri "$line")" | sed -e 's/index//')</loc>
  119. <lastmod>$(date -Iseconds -r "$line")</lastmod>
  120. <priority>0.5</priority>
  121. </url>" >> ./www/sitemap.xml
  122. done < html.tmp
  123. echo "</urlset>" >> ./www/sitemap.xml
  124. }
  125. if [ -d "./www" ]; then
  126. rm -r ./www
  127. fi
  128. mkdir ./www && cp -r ./src/statics/* ./www/
  129. find './src/pages' './src/broadcasts' './src/posts' \
  130. -type f -name '*.html' -exec ls -Rt {} + > html.tmp
  131. #
  132. # Call functions
  133. #
  134. buildAtom
  135. buildHome
  136. buildHtml
  137. buildSiteMap
  138. rm html.tmp