freedombone-mesh-blog 7.1KB

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