freedombone-mesh-blog 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. DEFAULT_BLOG_TITLE=$"Freedombone Blog"
  36. function regenerate_blog {
  37. clear
  38. echo ''
  39. echo $'Regenerating blog...'
  40. OLD_STAT_FILE=/home/$USER/.old_stat.txt
  41. if [ -f $OLD_STAT_FILE ]; then
  42. rm -f $OLD_STAT_FILE
  43. fi
  44. cd $BLOG_PATH
  45. if grep -q "SITENAME=u'${DEFAULT_BLOG_TITLE}'" $BLOG_PATH/pelicanconf.py; then
  46. TOX_NICK=$(toxid --showuser)
  47. BLOG_TITLE=$"${TOX_NICK}'s Blog"
  48. sed -i "s|SITENAME=.*|SITENAME=u\"${BLOG_TITLE}\"|g" $BLOG_PATH/pelicanconf.py
  49. fi
  50. make html
  51. }
  52. function view_blog {
  53. freedombone-mesh-visit-site '/Blog'
  54. exit 0
  55. }
  56. function new_blog {
  57. DATESTR=$(date "+%Y-%m-%d %H:%M:%S")
  58. echo $'Blog Post Title' > ~/.new-blog-entry
  59. echo $'###############' >> ~/.new-blog-entry
  60. echo '' >> ~/.new-blog-entry
  61. echo $":date: ${DATESTR}" >> ~/.new-blog-entry
  62. echo $":author: $(toxid --showuser)" >> ~/.new-blog-entry
  63. echo $':category: default' >> ~/.new-blog-entry
  64. echo $':tags: blog, tag' >> ~/.new-blog-entry
  65. echo '' >> ~/.new-blog-entry
  66. echo $'Add your text here' >> ~/.new-blog-entry
  67. echo '' >> ~/.new-blog-entry
  68. $BLOG_EDITOR ~/.new-blog-entry
  69. if grep -q $"Add your text here" ~/.new-blog-entry; then
  70. return
  71. fi
  72. if grep -q $"Blog Post Title" ~/.new-blog-entry; then
  73. return
  74. fi
  75. if [ ! -f $CURRENT_BLOG_INDEX ]; then
  76. echo '0' > $CURRENT_BLOG_INDEX
  77. fi
  78. # move to the content directory
  79. CURRENT_INDEX=$(cat $CURRENT_BLOG_INDEX)
  80. mv ~/.new-blog-entry $BLOG_CONTENT_PATH/${CURRENT_INDEX}_post.rst
  81. # increment the index
  82. CURRENT_INDEX=$((CURRENT_INDEX + 1))
  83. echo "$CURRENT_INDEX" > $CURRENT_BLOG_INDEX
  84. regenerate_blog
  85. }
  86. function edit_blog {
  87. if [ ! -f $CURRENT_BLOG_INDEX ]; then
  88. return
  89. fi
  90. CURRENT_INDEX=$(cat $CURRENT_BLOG_INDEX)
  91. PREVIOUS_INDEX=$((CURRENT_INDEX - 1))
  92. LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${PREVIOUS_INDEX}_post.rst
  93. if [ ! -f $LAST_BLOG_ENTRY ]; then
  94. return
  95. fi
  96. $BLOG_EDITOR $LAST_BLOG_ENTRY
  97. regenerate_blog
  98. }
  99. function delete_blog {
  100. if [ ! -f $CURRENT_BLOG_INDEX ]; then
  101. return
  102. fi
  103. CURRENT_INDEX=$(cat $CURRENT_BLOG_INDEX)
  104. PREVIOUS_INDEX=$((CURRENT_INDEX - 1))
  105. LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${PREVIOUS_INDEX}_post.rst
  106. if [ ! -f $LAST_BLOG_ENTRY ]; then
  107. return
  108. fi
  109. dialog --title $"Delete the previous blog entry" \
  110. --backtitle $"Freedombone Mesh" \
  111. --defaultno \
  112. --yesno $"\nAre you sure that you wish to delete the previous blog entry?" 8 60
  113. sel=$?
  114. case $sel in
  115. 0) rm $LAST_BLOG_ENTRY
  116. if [ $CURRENT_INDEX -gt 0 ]; then
  117. CURRENT_INDEX=$PREVIOUS_INDEX
  118. echo "$CURRENT_INDEX" > $CURRENT_BLOG_INDEX
  119. else
  120. rm -f $CURRENT_BLOG_INDEX
  121. fi
  122. regenerate_blog
  123. ;;
  124. esac
  125. }
  126. function change_theme {
  127. THEMES=()
  128. for d in $BLOG_PATH/themes/*/ ; do
  129. THEME_NAME=$(echo "$d" | awk -F '/' '{print $5}')
  130. THEMES+=("$THEME_NAME")
  131. done
  132. themelist=""
  133. n=1
  134. theme_index=0
  135. for a in "${THEMES[@]}"
  136. do
  137. if [[ $a == "basic" ]]; then
  138. themelist="$applist $n $a on"
  139. else
  140. themelist="$applist $n $a off"
  141. fi
  142. n=$[n+1]
  143. theme_index=$[theme_index+1]
  144. done
  145. data=$(tempfile 2>/dev/null)
  146. trap "rm -f $data" 0 1 2 5 15
  147. dialog --stdout --backtitle $"Freedombone Mesh" \
  148. --title $"Select Blog Theme" \
  149. --radiolist $'Choose:' \
  150. 80 40 20 $themelist 2> $data
  151. sel=$?
  152. case $sel in
  153. 1) exit 1;;
  154. 255) exit 1;;
  155. esac
  156. CHOSEN_THEME_INDEX=$(cat $data)
  157. CHOSEN_THEME_INDEX=$((CHOSEN_THEME_INDEX - 1))
  158. CHOSEN_THEME=${THEMES[$CHOSEN_THEME_INDEX]}
  159. if grep -q "THEME=" $BLOG_PATH/pelicanconf.py; then
  160. sed -i "s|THEME=.*|THEME='themes/${CHOSEN_THEME}'|g"
  161. else
  162. echo "THEME='themes/${CHOSEN_THEME}'" >> $BLOG_PATH/pelicanconf.py
  163. fi
  164. regenerate_blog
  165. }
  166. function menu_blog {
  167. data=$(tempfile 2>/dev/null)
  168. trap "rm -f $data" 0 1 2 5 15
  169. dialog --backtitle $"Freedombone Mesh" \
  170. --title $"Blogging" \
  171. --radiolist $"Choose an operation:" 19 50 12 \
  172. 1 $"View a blog" on \
  173. 2 $"New blog entry" off \
  174. 3 $"Edit the previous blog entry" off \
  175. 4 $"Delete the previous blog entry" off \
  176. 5 $"Change theme" off \
  177. 6 $"Exit" off 2> $data
  178. sel=$?
  179. case $sel in
  180. 1) exit 1;;
  181. 255) exit 1;;
  182. esac
  183. case $(cat $data) in
  184. 1) view_blog;;
  185. 2) new_blog;;
  186. 3) edit_blog;;
  187. 4) delete_blog;;
  188. 5) change_theme;;
  189. 6) break;;
  190. esac
  191. }
  192. menu_blog
  193. exit 0