freedombone-mesh-blog 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Blogging functions for mesh clients
  12. #
  13. # License
  14. # =======
  15. #
  16. # This program is free software: you can redistribute it and/or modify
  17. # it under the terms of the GNU Affero General Public License as published by
  18. # the Free Software Foundation, either version 3 of the License, or
  19. # (at your option) any later version.
  20. #
  21. # This program is distributed in the hope that it will be useful,
  22. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. # GNU Affero General Public License for more details.
  25. #
  26. # You should have received a copy of the GNU Affero General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. PROJECT_NAME='freedombone'
  29. export TEXTDOMAIN=${PROJECT_NAME}-mesh-blog
  30. export TEXTDOMAINDIR="/usr/share/locale"
  31. BLOG_PATH=~/CreateBlog
  32. BLOG_CONTENT_PATH=$BLOG_PATH/content
  33. CURRENT_BLOG_INDEX=/home/$USER/.blog-index
  34. BLOG_EDITOR='pluma'
  35. function regenerate_blog {
  36. OLD_STAT_FILE=/home/$USER/.old_stat.txt
  37. if [ -f $OLD_STAT_FILE ]; then
  38. rm -f $OLD_STAT_FILE
  39. fi
  40. cd $BLOG_PATH
  41. make html
  42. }
  43. function view_blog {
  44. freedombone-mesh-visit-site '/Blog'
  45. exit 0
  46. }
  47. function new_blog {
  48. echo $'Blog Post Title' > ~/.new-blog-entry
  49. echo $'###############' >> ~/.new-blog-entry
  50. echo '' >> ~/.new-blog-entry
  51. echo $':date: 2020-01-01' >> ~/.new-blog-entry
  52. echo $":author: $(toxid --showuser)" >> ~/.new-blog-entry
  53. echo $':category: default' >> ~/.new-blog-entry
  54. echo $':tags: blog, tag' >> ~/.new-blog-entry
  55. echo '' >> ~/.new-blog-entry
  56. echo $'Add your text here' >> ~/.new-blog-entry
  57. echo '' >> ~/.new-blog-entry
  58. $BLOG_EDITOR ~/.new-blog-entry
  59. if grep -q $"Add your text here" ~/.new-blog-entry; then
  60. return
  61. fi
  62. if grep -q $"Blog Post Title" ~/.new-blog-entry; then
  63. return
  64. fi
  65. if [ ! -f $CURRENT_BLOG_INDEX ]; then
  66. echo '0' > $CURRENT_BLOG_INDEX
  67. fi
  68. # move to the content directory
  69. CURRENT_INDEX=$(cat $CURRENT_BLOG_INDEX)
  70. mv ~/.new-blog-entry $BLOG_CONTENT_PATH/${CURRENT_INDEX}_post.rst
  71. # increment the index
  72. CURRENT_INDEX=$((CURRENT_INDEX + 1))
  73. echo "$CURRENT_INDEX" > $CURRENT_BLOG_INDEX
  74. regenerate_blog
  75. }
  76. function edit_blog {
  77. if [ ! -f $CURRENT_BLOG_INDEX ]; then
  78. return
  79. fi
  80. CURRENT_INDEX=$(cat $CURRENT_BLOG_INDEX)
  81. LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${CURRENT_INDEX}_post.rst
  82. if [ ! -f $LAST_BLOG_ENTRY ]; then
  83. return
  84. fi
  85. $BLOG_EDITOR $LAST_BLOG_ENTRY
  86. regenerate_blog
  87. }
  88. function delete_blog {
  89. if [ ! -f $CURRENT_BLOG_INDEX ]; then
  90. return
  91. fi
  92. CURRENT_INDEX=$(cat $CURRENT_BLOG_INDEX)
  93. LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${CURRENT_INDEX}_post.rst
  94. if [ ! -f $LAST_BLOG_ENTRY ]; then
  95. return
  96. fi
  97. dialog --title $"Delete the previous blog entry" \
  98. --backtitle $"Freedombone Mesh" \
  99. --defaultno \
  100. --yesno $"\nAre you sure that you wish to delete the previous blog entry?" 8 60
  101. sel=$?
  102. case $sel in
  103. 0) rm $LAST_BLOG_ENTRY
  104. if [ $CURRENT_INDEX -gt 0 ]; then
  105. CURRENT_INDEX=$((CURRENT_INDEX - 1))
  106. echo "$CURRENT_INDEX" > $CURRENT_BLOG_INDEX
  107. fi
  108. regenerate_blog
  109. ;;
  110. esac
  111. }
  112. function change_theme {
  113. THEMES=()
  114. for d in $BLOG_PATH/themes/*/ ; do
  115. THEME_NAME=$(echo "$d" | awk -F '/' '{print $5}')
  116. THEMES+=("$THEME_NAME")
  117. done
  118. themelist=""
  119. n=1
  120. theme_index=0
  121. for a in "${THEMES[@]}"
  122. do
  123. if [[ $a == "basic" ]]; then
  124. themelist="$applist $n $a on"
  125. else
  126. themelist="$applist $n $a off"
  127. fi
  128. n=$[n+1]
  129. theme_index=$[theme_index+1]
  130. done
  131. data=$(tempfile 2>/dev/null)
  132. trap "rm -f $data" 0 1 2 5 15
  133. dialog --stdout --backtitle $"Freedombone Mesh" \
  134. --title $"Select Blog Theme" \
  135. --radiolist $'Choose:' \
  136. 80 40 20 $themelist 2> $data
  137. sel=$?
  138. case $sel in
  139. 1) exit 1;;
  140. 255) exit 1;;
  141. esac
  142. CHOSEN_THEME_INDEX=$(cat $data)
  143. CHOSEN_THEME_INDEX=$((CHOSEN_THEME_INDEX - 1))
  144. CHOSEN_THEME=${THEMES[$CHOSEN_THEME_INDEX]}
  145. if grep -q "THEME=" $BLOG_PATH/pelicanconf.py; then
  146. sed -i "s|THEME=.*|THEME='themes/${CHOSEN_THEME}'|g"
  147. else
  148. echo "THEME='themes/${CHOSEN_THEME}'" >> $BLOG_PATH/pelicanconf.py
  149. fi
  150. regenerate_blog
  151. }
  152. function menu_blog {
  153. data=$(tempfile 2>/dev/null)
  154. trap "rm -f $data" 0 1 2 5 15
  155. dialog --backtitle $"Freedombone Mesh" \
  156. --title $"Blogging" \
  157. --radiolist $"Choose an operation:" 19 50 12 \
  158. 1 $"View a blog" on \
  159. 2 $"New blog entry" off \
  160. 3 $"Edit the previous blog entry" off \
  161. 4 $"Delete the previous blog entry" off \
  162. 5 $"Change theme" off \
  163. 6 $"Exit" off 2> $data
  164. sel=$?
  165. case $sel in
  166. 1) exit 1;;
  167. 255) exit 1;;
  168. esac
  169. case $(cat $data) in
  170. 1) view_blog;;
  171. 2) new_blog;;
  172. 3) edit_blog;;
  173. 4) delete_blog;;
  174. 6) change_theme;;
  175. 7) break;;
  176. esac
  177. }
  178. menu_blog
  179. exit 0