freedombone-mesh-blog 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #!/bin/bash
  2. # _____ _ _
  3. # | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
  4. # | __| _| -_| -_| . | . | | . | . | | -_|
  5. # |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
  6. #
  7. # Freedom in the Cloud
  8. #
  9. # Blogging functions for mesh clients
  10. #
  11. # License
  12. # =======
  13. #
  14. # This program is free software: you can redistribute it and/or modify
  15. # it under the terms of the GNU Affero General Public License as published by
  16. # the Free Software Foundation, either version 3 of the License, or
  17. # (at your option) any later version.
  18. #
  19. # This program is distributed in the hope that it will be useful,
  20. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. # GNU Affero General Public License for more details.
  23. #
  24. # You should have received a copy of the GNU Affero General Public License
  25. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. PROJECT_NAME='freedombone'
  27. export TEXTDOMAIN=${PROJECT_NAME}-mesh-blog
  28. export TEXTDOMAINDIR="/usr/share/locale"
  29. IPFS_PATH=/usr/bin
  30. IPFS_COMMAND=$IPFS_PATH/ipfs
  31. IPFS_PUBLIC=/home/$USER/.ipfs-public
  32. BLOG_PATH=~/CreateBlog
  33. BLOG_CONTENT_PATH=$BLOG_PATH/content
  34. CURRENT_BLOG_INDEX=/home/$USER/.blog-index
  35. BLOG_EDITOR='pluma'
  36. DEFAULT_BLOG_TITLE=$"Freedombone Blog"
  37. function remove_bad_blog_links {
  38. find ./ -type f -name "*.css" -exec sed -i -e '/googleapi/d' {} \;
  39. find ./ -type f -name "*.scss" -exec sed -i -e '/googleapi/d' {} \;
  40. find ./ -type f -name "*.html" -exec sed -i -e '/googleapi/d' {} \;
  41. find ./ -type f -name "*.css" -exec sed -i -e '/bootstrapcdn/d' {} \;
  42. find ./ -type f -name "*.scss" -exec sed -i -e '/bootstrapcdn/d' {} \;
  43. find ./ -type f -name "*.html" -exec sed -i -e '/bootstrapcdn/d' {} \;
  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. $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 || exit 246872648
  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. cd $BLOG_PATH || exit 23682468
  73. remove_bad_blog_links
  74. ipfs_publish
  75. }
  76. function view_blog {
  77. freedombone-mesh-visit-site '/Blog'
  78. exit 0
  79. }
  80. function new_blog {
  81. DATESTR=$(date "+%Y-%m-%d %H:%M:%S")
  82. { echo $'Title: Blog Post Title';
  83. echo $"Date: ${DATESTR}";
  84. echo $"Author: $(toxid --showuser)";
  85. echo $'Category: default';
  86. echo $'Tags: blog, tag';
  87. echo '';
  88. echo $'Add your text here';
  89. echo '';
  90. echo -n $'To include an image copy it into the ~/CreateBlog/content/images directory, ';
  91. echo $'then link to it with:';
  92. echo '';
  93. echo $'![My image]({filename}images/myimage.jpg)';
  94. echo ''; } > ~/.new-blog-entry
  95. $BLOG_EDITOR ~/.new-blog-entry
  96. if grep -q $"Add your text here" ~/.new-blog-entry; then
  97. return
  98. fi
  99. if grep -q $"Blog Post Title" ~/.new-blog-entry; then
  100. return
  101. fi
  102. if [ ! -f "$CURRENT_BLOG_INDEX" ]; then
  103. echo '0' > "$CURRENT_BLOG_INDEX"
  104. fi
  105. # move to the content directory
  106. CURRENT_INDEX=$(cat "$CURRENT_BLOG_INDEX")
  107. # shellcheck disable=SC2086
  108. mv ~/.new-blog-entry $BLOG_CONTENT_PATH/${CURRENT_INDEX}_post.md
  109. # increment the index
  110. CURRENT_INDEX=$((CURRENT_INDEX + 1))
  111. echo "$CURRENT_INDEX" > "$CURRENT_BLOG_INDEX"
  112. regenerate_blog
  113. }
  114. function edit_blog {
  115. if [ ! -f "$CURRENT_BLOG_INDEX" ]; then
  116. return
  117. fi
  118. CURRENT_INDEX=$(cat "$CURRENT_BLOG_INDEX")
  119. PREVIOUS_INDEX=$((CURRENT_INDEX - 1))
  120. LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${PREVIOUS_INDEX}_post.md
  121. if [ ! -f $LAST_BLOG_ENTRY ]; then
  122. return
  123. fi
  124. $BLOG_EDITOR $LAST_BLOG_ENTRY
  125. regenerate_blog
  126. }
  127. function delete_blog {
  128. if [ ! -f "$CURRENT_BLOG_INDEX" ]; then
  129. return
  130. fi
  131. CURRENT_INDEX=$(cat "$CURRENT_BLOG_INDEX")
  132. PREVIOUS_INDEX=$((CURRENT_INDEX - 1))
  133. LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${PREVIOUS_INDEX}_post.md
  134. if [ ! -f $LAST_BLOG_ENTRY ]; then
  135. return
  136. fi
  137. 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
  138. rm $LAST_BLOG_ENTRY
  139. if [ "$CURRENT_INDEX" -gt 0 ]; then
  140. CURRENT_INDEX=$PREVIOUS_INDEX
  141. echo "$CURRENT_INDEX" > "$CURRENT_BLOG_INDEX"
  142. else
  143. rm -f "$CURRENT_BLOG_INDEX"
  144. fi
  145. regenerate_blog
  146. fi
  147. }
  148. function change_theme {
  149. THEMES=()
  150. for d in $BLOG_PATH/themes/*/ ; do
  151. THEME_NAME=$(echo "$d" | awk -F '/' '{print $6}')
  152. THEMES+=("$THEME_NAME")
  153. done
  154. n=1
  155. curr_theme_index=
  156. if [ -f "/home/$USER/.blog-theme-index" ]; then
  157. curr_theme_index=$(cat "/home/$USER/.blog-theme-index")
  158. fi
  159. if [ -f /tmp/.blog-themes ]; then
  160. rm /tmp/.blog-themes
  161. fi
  162. # shellcheck disable=SC2068
  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. # shellcheck disable=SC2002
  170. cat /tmp/.blog-themes | \
  171. awk -F ' ' '{
  172. for(i=1;i<=NF;i++){
  173. print $i;
  174. }
  175. }' | \
  176. zenity --list \
  177. --title=$'Select Blog Theme' \
  178. --column=$'Index' --column=$'Theme' \
  179. --print-column=1 --hide-column=1 --width=300 --height=400)
  180. rm /tmp/.blog-themes
  181. if [ ! "$CHOSEN_THEME_INDEX" ]; then
  182. exit 1
  183. fi
  184. echo "$CHOSEN_THEME_INDEX" > "/home/$USER/.blog-theme-index"
  185. CHOSEN_THEME_INDEX=$((CHOSEN_THEME_INDEX - 1))
  186. CHOSEN_THEME=${THEMES[$CHOSEN_THEME_INDEX]}
  187. cd "$BLOG_PATH/themes/$CHOSEN_THEME" || exit 346746824
  188. remove_bad_blog_links
  189. if grep -q "THEME=" $BLOG_PATH/pelicanconf.py; then
  190. sed -i "s|THEME=.*|THEME='themes/${CHOSEN_THEME}'|g" $BLOG_PATH/pelicanconf.py
  191. else
  192. echo "THEME='themes/${CHOSEN_THEME}'" >> $BLOG_PATH/pelicanconf.py
  193. fi
  194. regenerate_blog
  195. }
  196. function menu_blog {
  197. 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)
  198. sel=$?
  199. case $sel in
  200. 1) exit 1;;
  201. 255) exit 1;;
  202. esac
  203. case $data in
  204. 1) view_blog;;
  205. 2) new_blog;;
  206. 3) edit_blog;;
  207. 4) delete_blog;;
  208. 5) change_theme;;
  209. esac
  210. }
  211. menu_blog
  212. exit 0