freedombone/src/freedombone-mesh-blog

245 lines
7.1 KiB
Plaintext
Raw Normal View History

2016-09-08 21:45:04 +02:00
#!/bin/bash
2018-04-08 14:30:21 +02:00
# _____ _ _
# | __|___ ___ ___ _| |___ _____| |_ ___ ___ ___
# | __| _| -_| -_| . | . | | . | . | | -_|
# |__| |_| |___|___|___|___|_|_|_|___|___|_|_|___|
2016-09-08 21:45:04 +02:00
#
2018-04-08 14:30:21 +02:00
# Freedom in the Cloud
2016-09-08 21:45:04 +02:00
#
# Blogging functions for mesh clients
#
# License
# =======
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
PROJECT_NAME='freedombone'
export TEXTDOMAIN=${PROJECT_NAME}-mesh-blog
export TEXTDOMAINDIR="/usr/share/locale"
IPFS_PATH=/usr/bin
IPFS_COMMAND=$IPFS_PATH/ipfs
IPFS_PUBLIC=/home/$USER/.ipfs-public
2016-09-08 21:45:04 +02:00
BLOG_PATH=~/CreateBlog
BLOG_CONTENT_PATH=$BLOG_PATH/content
CURRENT_BLOG_INDEX=/home/$USER/.blog-index
BLOG_EDITOR='pluma'
2016-09-09 10:31:27 +02:00
DEFAULT_BLOG_TITLE=$"Freedombone Blog"
2016-09-08 21:45:04 +02:00
2017-12-09 14:52:10 +01:00
function remove_bad_blog_links {
2018-03-02 20:17:02 +01:00
find ./ -type f -name "*.css" -exec sed -i -e '/googleapi/d' {} \;
find ./ -type f -name "*.scss" -exec sed -i -e '/googleapi/d' {} \;
find ./ -type f -name "*.html" -exec sed -i -e '/googleapi/d' {} \;
find ./ -type f -name "*.css" -exec sed -i -e '/bootstrapcdn/d' {} \;
find ./ -type f -name "*.scss" -exec sed -i -e '/bootstrapcdn/d' {} \;
find ./ -type f -name "*.html" -exec sed -i -e '/bootstrapcdn/d' {} \;
2017-12-31 00:33:13 +01:00
}
function ipfs_publish {
DIR_TO_CHECK=/home/$USER/Public
2018-03-02 20:17:02 +01:00
if [ ! -d "$DIR_TO_CHECK" ]; then
return
fi
echo ''
2016-09-09 12:06:33 +02:00
echo $'Publishing to IPFS. This may take some time...'
OLD_STAT_FILE=/home/$USER/.old_stat.txt
2018-03-02 20:17:02 +01:00
NEW_STAT=$(stat -t "$DIR_TO_CHECK")
$IPFS_COMMAND add -rq "/home/$USER/Public" | tail -n 1 > "$IPFS_PUBLIC"
echo "$NEW_STAT" > "$OLD_STAT_FILE"
2018-03-02 20:17:02 +01:00
if [ -f "$IPFS_PUBLIC" ]; then
IPFS_PUBLIC_ID=$(cat "$IPFS_PUBLIC")
$IPFS_COMMAND name publish "/ipfs/$IPFS_PUBLIC_ID"
fi
}
2016-09-08 21:45:04 +02:00
function regenerate_blog {
2016-09-09 10:33:35 +02:00
clear
echo ''
echo $'Regenerating blog...'
2018-03-02 20:17:02 +01:00
cd $BLOG_PATH || exit 246872648
2016-09-09 10:31:27 +02:00
if grep -q "SITENAME=u'${DEFAULT_BLOG_TITLE}'" $BLOG_PATH/pelicanconf.py; then
TOX_NICK=$(toxid --showuser)
BLOG_TITLE=$"${TOX_NICK}'s Blog"
sed -i "s|SITENAME=.*|SITENAME=u\"${BLOG_TITLE}\"|g" $BLOG_PATH/pelicanconf.py
fi
2016-09-08 21:45:04 +02:00
make html
2018-03-02 20:17:02 +01:00
cd $BLOG_PATH || exit 23682468
2017-12-31 00:33:13 +01:00
remove_bad_blog_links
ipfs_publish
2016-09-08 21:45:04 +02:00
}
function view_blog {
2018-05-02 12:22:36 +02:00
${PROJECT_NAME}-mesh-visit-site '/Blog'
2016-09-08 21:45:04 +02:00
exit 0
}
function new_blog {
2016-09-09 10:41:51 +02:00
DATESTR=$(date "+%Y-%m-%d %H:%M:%S")
2018-03-02 20:17:02 +01:00
{ echo $'Title: Blog Post Title';
echo $"Date: ${DATESTR}";
echo $"Author: $(toxid --showuser)";
echo $'Category: default';
echo $'Tags: blog, tag';
echo '';
echo $'Add your text here';
echo '';
echo -n $'To include an image copy it into the ~/CreateBlog/content/images directory, ';
echo $'then link to it with:';
echo '';
echo $'![My image]({filename}images/myimage.jpg)';
echo ''; } > ~/.new-blog-entry
2016-09-08 21:45:04 +02:00
$BLOG_EDITOR ~/.new-blog-entry
if grep -q $"Add your text here" ~/.new-blog-entry; then
return
fi
if grep -q $"Blog Post Title" ~/.new-blog-entry; then
return
fi
2018-03-02 20:17:02 +01:00
if [ ! -f "$CURRENT_BLOG_INDEX" ]; then
echo '0' > "$CURRENT_BLOG_INDEX"
2016-09-08 21:45:04 +02:00
fi
# move to the content directory
2018-03-02 20:17:02 +01:00
CURRENT_INDEX=$(cat "$CURRENT_BLOG_INDEX")
# shellcheck disable=SC2086
mv ~/.new-blog-entry $BLOG_CONTENT_PATH/${CURRENT_INDEX}_post.md
2016-09-08 21:45:04 +02:00
# increment the index
CURRENT_INDEX=$((CURRENT_INDEX + 1))
2018-03-02 20:17:02 +01:00
echo "$CURRENT_INDEX" > "$CURRENT_BLOG_INDEX"
2016-09-08 21:45:04 +02:00
regenerate_blog
}
function edit_blog {
2018-03-02 20:17:02 +01:00
if [ ! -f "$CURRENT_BLOG_INDEX" ]; then
2016-09-08 21:45:04 +02:00
return
fi
2018-03-02 20:17:02 +01:00
CURRENT_INDEX=$(cat "$CURRENT_BLOG_INDEX")
2016-09-09 10:14:52 +02:00
PREVIOUS_INDEX=$((CURRENT_INDEX - 1))
2016-09-09 17:35:33 +02:00
LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${PREVIOUS_INDEX}_post.md
2016-09-08 21:45:04 +02:00
if [ ! -f $LAST_BLOG_ENTRY ]; then
return
fi
$BLOG_EDITOR $LAST_BLOG_ENTRY
regenerate_blog
}
function delete_blog {
2018-03-02 20:17:02 +01:00
if [ ! -f "$CURRENT_BLOG_INDEX" ]; then
2016-09-08 21:45:04 +02:00
return
fi
2018-03-02 20:17:02 +01:00
CURRENT_INDEX=$(cat "$CURRENT_BLOG_INDEX")
PREVIOUS_INDEX=$((CURRENT_INDEX - 1))
2016-09-09 17:35:33 +02:00
LAST_BLOG_ENTRY=$BLOG_CONTENT_PATH/${PREVIOUS_INDEX}_post.md
2016-09-08 21:45:04 +02:00
if [ ! -f $LAST_BLOG_ENTRY ]; then
return
fi
2018-03-02 20:17:02 +01:00
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
2017-12-06 23:20:05 +01:00
rm $LAST_BLOG_ENTRY
2018-03-02 20:17:02 +01:00
if [ "$CURRENT_INDEX" -gt 0 ]; then
2017-12-06 23:20:05 +01:00
CURRENT_INDEX=$PREVIOUS_INDEX
2018-03-02 20:17:02 +01:00
echo "$CURRENT_INDEX" > "$CURRENT_BLOG_INDEX"
2017-12-06 23:20:05 +01:00
else
2018-03-02 20:17:02 +01:00
rm -f "$CURRENT_BLOG_INDEX"
2017-12-06 23:20:05 +01:00
fi
regenerate_blog
fi
2016-09-08 21:45:04 +02:00
}
function change_theme {
THEMES=()
for d in $BLOG_PATH/themes/*/ ; do
2016-09-09 10:49:07 +02:00
THEME_NAME=$(echo "$d" | awk -F '/' '{print $6}')
2016-09-08 21:45:04 +02:00
THEMES+=("$THEME_NAME")
done
n=1
curr_theme_index=
2018-03-02 20:17:02 +01:00
if [ -f "/home/$USER/.blog-theme-index" ]; then
curr_theme_index=$(cat "/home/$USER/.blog-theme-index")
fi
2017-12-09 14:52:10 +01:00
if [ -f /tmp/.blog-themes ]; then
rm /tmp/.blog-themes
fi
2018-03-08 15:38:15 +01:00
# shellcheck disable=SC2068
for a in ${THEMES[@]}
2016-09-08 21:45:04 +02:00
do
2017-12-09 14:52:10 +01:00
echo "$n $a" >> /tmp/.blog-themes
2018-03-02 20:17:02 +01:00
n=$((n+1))
2016-09-08 21:45:04 +02:00
done
2017-12-09 14:52:10 +01:00
CHOSEN_THEME_INDEX=$(
2018-03-02 20:17:02 +01:00
# shellcheck disable=SC2002
2017-12-09 14:52:10 +01:00
cat /tmp/.blog-themes | \
awk -F ' ' '{
for(i=1;i<=NF;i++){
print $i;
}
}' | \
zenity --list \
--title=$'Select Blog Theme' \
--column=$'Index' --column=$'Theme' \
--print-column=1 --hide-column=1 --width=300 --height=400)
rm /tmp/.blog-themes
2018-03-02 20:17:02 +01:00
if [ ! "$CHOSEN_THEME_INDEX" ]; then
2017-12-09 14:52:10 +01:00
exit 1
fi
2016-09-08 21:45:04 +02:00
2018-03-02 20:17:02 +01:00
echo "$CHOSEN_THEME_INDEX" > "/home/$USER/.blog-theme-index"
2016-09-08 21:45:04 +02:00
CHOSEN_THEME_INDEX=$((CHOSEN_THEME_INDEX - 1))
CHOSEN_THEME=${THEMES[$CHOSEN_THEME_INDEX]}
2018-03-02 20:17:02 +01:00
cd "$BLOG_PATH/themes/$CHOSEN_THEME" || exit 346746824
2017-12-31 19:17:01 +01:00
remove_bad_blog_links
2016-09-08 21:45:04 +02:00
if grep -q "THEME=" $BLOG_PATH/pelicanconf.py; then
2016-09-09 11:12:34 +02:00
sed -i "s|THEME=.*|THEME='themes/${CHOSEN_THEME}'|g" $BLOG_PATH/pelicanconf.py
2016-09-08 21:45:04 +02:00
else
echo "THEME='themes/${CHOSEN_THEME}'" >> $BLOG_PATH/pelicanconf.py
fi
regenerate_blog
}
function menu_blog {
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)
2016-09-08 21:45:04 +02:00
sel=$?
case $sel in
1) exit 1;;
255) exit 1;;
esac
case $data in
2016-09-08 21:45:04 +02:00
1) view_blog;;
2) new_blog;;
3) edit_blog;;
4) delete_blog;;
2016-09-09 10:47:02 +02:00
5) change_theme;;
2016-09-08 21:45:04 +02:00
esac
}
menu_blog
exit 0