freedombone-mesh-blog 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. IPFS_PATH=/usr/bin
  32. IPFS_COMMAND=$IPFS_PATH/ipfs
  33. IPFS_PUBLIC=/home/$USER/.ipfs-public
  34. BLOG_PATH=~/CreateBlog
  35. BLOG_CONTENT_PATH=$BLOG_PATH/content
  36. CURRENT_BLOG_INDEX=/home/$USER/.blog-index
  37. BLOG_EDITOR='pluma'
  38. DEFAULT_BLOG_TITLE=$"Freedombone Blog"
  39. function remove_bad_blog_links {
  40. current_theme=$1
  41. if [ -f $BLOG_PATH/themes/$current_theme/static/css/style.css ]; then
  42. sed -i '/googleapi/d' $BLOG_PATH/themes/$current_theme/static/css/style.css
  43. fi
  44. }
  45. function ipfs_publish {
  46. DIR_TO_CHECK=/home/$USER/Public
  47. if [ ! -d $DIR_TO_CHECK ]; then
  48. return
  49. fi
  50. echo ''
  51. echo $'Publishing to IPFS. This may take some time...'
  52. OLD_STAT_FILE=/home/$USER/.old_stat.txt
  53. NEW_STAT=$(stat -t $DIR_TO_CHECK)
  54. echo $($IPFS_COMMAND add -rq /home/$USER/Public | tail -n 1) > $IPFS_PUBLIC
  55. echo "$NEW_STAT" > $OLD_STAT_FILE
  56. if [ -f $IPFS_PUBLIC ]; then
  57. IPFS_PUBLIC_ID=$(cat $IPFS_PUBLIC)
  58. $IPFS_COMMAND name publish /ipfs/$IPFS_PUBLIC_ID
  59. fi
  60. }
  61. function regenerate_blog {
  62. clear
  63. echo ''
  64. echo $'Regenerating blog...'
  65. cd $BLOG_PATH
  66. if grep -q "SITENAME=u'${DEFAULT_BLOG_TITLE}'" $BLOG_PATH/pelicanconf.py; then
  67. TOX_NICK=$(toxid --showuser)
  68. BLOG_TITLE=$"${TOX_NICK}'s Blog"
  69. sed -i "s|SITENAME=.*|SITENAME=u\"${BLOG_TITLE}\"|g" $BLOG_PATH/pelicanconf.py
  70. fi
  71. make html
  72. ipfs_publish
  73. }
  74. function view_blog {
  75. freedombone-mesh-visit-site '/Blog'
  76. exit 0
  77. }
  78. function new_blog {
  79. DATESTR=$(date "+%Y-%m-%d %H:%M:%S")
  80. echo $'Title: Blog Post Title' > ~/.new-blog-entry
  81. echo $"Date: ${DATESTR}" >> ~/.new-blog-entry
  82. echo $"Author: $(toxid --showuser)" >> ~/.new-blog-entry
  83. echo $'Category: default' >> ~/.new-blog-entry
  84. echo $'Tags: blog, tag' >> ~/.new-blog-entry
  85. echo '' >> ~/.new-blog-entry
  86. echo $'Add your text here' >> ~/.new-blog-entry
  87. echo '' >> ~/.new-blog-entry
  88. echo -n $'To include an image copy it into the ~/CreateBlog/content/images directory, ' >> ~/.new-blog-entry
  89. echo $'then link to it with:' >> ~/.new-blog-entry
  90. echo '' >> ~/.new-blog-entry
  91. echo $'![My image]({filename}images/myimage.jpg)' >> ~/.new-blog-entry
  92. echo '' >> ~/.new-blog-entry
  93. $BLOG_EDITOR ~/.new-blog-entry
  94. if grep -q $"Add your text here" ~/.new-blog-entry; then
  95. return
  96. fi
  97. if grep -q $"Blog Post Title" ~/.new-blog-entry; then
  98. return
  99. fi
  100. if [ ! -f $CURRENT_BLOG_INDEX ]; then
  101. echo '0' > $CURRENT_BLOG_INDEX
  102. fi
  103. # move to the content directory
  104. CURRENT_INDEX=$(cat $CURRENT_BLOG_INDEX)
  105. mv ~/.new-blog-entry $BLOG_CONTENT_PATH/${CURRENT_INDEX}_post.md
  106. # increment the index
  107. CURRENT_INDEX=$((CURRENT_INDEX + 1))
  108. echo "$CURRENT_INDEX" > $CURRENT_BLOG_INDEX
  109. regenerate_blog
  110. }
  111. function edit_blog {
  112. if [ ! -f $CURRENT_BLOG_INDEX ]; then
  113. return
  114. fi
  115. CURRENT_INDEX=$(cat $CURRENT_BLOG_INDEX)
  116. PREVIOUS_INDEX=$((CURRENT_INDEX - 1))
  117. LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${PREVIOUS_INDEX}_post.md
  118. if [ ! -f $LAST_BLOG_ENTRY ]; then
  119. return
  120. fi
  121. $BLOG_EDITOR $LAST_BLOG_ENTRY
  122. regenerate_blog
  123. }
  124. function delete_blog {
  125. if [ ! -f $CURRENT_BLOG_INDEX ]; then
  126. return
  127. fi
  128. CURRENT_INDEX=$(cat $CURRENT_BLOG_INDEX)
  129. PREVIOUS_INDEX=$((CURRENT_INDEX - 1))
  130. LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${PREVIOUS_INDEX}_post.md
  131. if [ ! -f $LAST_BLOG_ENTRY ]; then
  132. return
  133. fi
  134. if ! zenity --question --title=$'Delete the previous blog entry' --text=$"\nAre you sure that you wish to delete the previous blog entry?" --ok-label=No --cancel-label=Yes --width=300; then
  135. rm $LAST_BLOG_ENTRY
  136. if [ $CURRENT_INDEX -gt 0 ]; then
  137. CURRENT_INDEX=$PREVIOUS_INDEX
  138. echo "$CURRENT_INDEX" > $CURRENT_BLOG_INDEX
  139. else
  140. rm -f $CURRENT_BLOG_INDEX
  141. fi
  142. regenerate_blog
  143. fi
  144. }
  145. function change_theme {
  146. THEMES=()
  147. for d in $BLOG_PATH/themes/*/ ; do
  148. THEME_NAME=$(echo "$d" | awk -F '/' '{print $6}')
  149. THEMES+=("$THEME_NAME")
  150. done
  151. n=1
  152. curr_theme_index=
  153. if [ -f /home/$USER/.blog-theme-index ]; then
  154. curr_theme_index=$(cat /home/$USER/.blog-theme-index)
  155. fi
  156. if [ -f /tmp/.blog-themes ]; then
  157. rm /tmp/.blog-themes
  158. fi
  159. for a in "${THEMES[@]}"
  160. do
  161. echo "$n $a" >> /tmp/.blog-themes
  162. n=$[n+1]
  163. done
  164. CHOSEN_THEME_INDEX=$(
  165. cat /tmp/.blog-themes | \
  166. awk -F ' ' '{
  167. for(i=1;i<=NF;i++){
  168. print $i;
  169. }
  170. }' | \
  171. zenity --list \
  172. --title=$'Select Blog Theme' \
  173. --column=$'Index' --column=$'Theme' \
  174. --print-column=1 --hide-column=1 --width=300 --height=400)
  175. rm /tmp/.blog-themes
  176. if [ ! $CHOSEN_THEME_INDEX ]; then
  177. exit 1
  178. fi
  179. echo "$CHOSEN_THEME_INDEX" > /home/$USER/.blog-theme-index
  180. CHOSEN_THEME_INDEX=$((CHOSEN_THEME_INDEX - 1))
  181. CHOSEN_THEME=${THEMES[$CHOSEN_THEME_INDEX]}
  182. remove_bad_blog_links $CHOSEN_THEME
  183. if grep -q "THEME=" $BLOG_PATH/pelicanconf.py; then
  184. sed -i "s|THEME=.*|THEME='themes/${CHOSEN_THEME}'|g" $BLOG_PATH/pelicanconf.py
  185. else
  186. echo "THEME='themes/${CHOSEN_THEME}'" >> $BLOG_PATH/pelicanconf.py
  187. fi
  188. regenerate_blog
  189. }
  190. function menu_blog {
  191. data=$(zenity --list 1 $"View a blog" 2 $"New blog entry" 3 $"Edit the previous blog entry" 4 $"Delete the previous blog entry" 5 $"Change theme" --column="id" --title $"Blogging" --column=$"Choose an operation:" --hide-column=1 --print-column=1 --height=250)
  192. sel=$?
  193. case $sel in
  194. 1) exit 1;;
  195. 255) exit 1;;
  196. esac
  197. case $data in
  198. 1) view_blog;;
  199. 2) new_blog;;
  200. 3) edit_blog;;
  201. 4) delete_blog;;
  202. 5) change_theme;;
  203. esac
  204. }
  205. menu_blog
  206. exit 0