Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugins #161

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ Features
- Support for tags/categories
- Support for Markdown, Disqus comments, Twitter, Feedburner, Google Analytics.
- The project is still maintained as of 2016. Bugs are fixed, and new features are considered (see "Contributing")
- Optional powerful plugin system.
Drop your plugins into a directory called plugins, or for system wide, /usr/share/bashblog/plugins.
- Everything stored in a single ~1k lines bash script, how cool is that?! ;)


Expand Down
41 changes: 40 additions & 1 deletion bb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ edit() {
touch -t "$touch_timestamp" "$1"
chmod 644 "$filename"
echo "Posted $filename"
call_plugins posted_html
tags_after=$(tags_in_post "$filename")
relevant_tags=$(echo "$tags_before $tags_after" | tr ',' ' ' | tr ' ' '\n' | sort -u | tr '\n' ' ')
if [[ ! -z $relevant_tags ]]; then
Expand Down Expand Up @@ -595,7 +596,9 @@ EOF
filename=""
while [[ $post_status != "p" && $post_status != "P" ]]; do
[[ -n $filename ]] && rm "$filename" # Delete the generated html file, if any
call_plugins pre_edit "$TMPFILE"
$EDITOR "$TMPFILE"
call_plugins post_edit "$TMPFILE"
if [[ $fmt == md ]]; then
html_from_md=$(markdown "$TMPFILE")
parse_file "$html_from_md"
Expand Down Expand Up @@ -635,6 +638,7 @@ EOF
fi
chmod 644 "$filename"
echo "Posted $filename"
call_plugins posted_md
relevant_tags=$(tags_in_post $filename)
if [[ -n $relevant_tags ]]; then
relevant_posts="$(posts_with_tags $relevant_tags) $filename"
Expand Down Expand Up @@ -1082,6 +1086,7 @@ reset() {
else
echo "Phew! You dodged a bullet there. Nothing was modified."
fi
call_plugins reset
}

# Detects if GNU date is installed
Expand Down Expand Up @@ -1118,6 +1123,27 @@ date_version_detect() {
# $1 command to run
# $2 file name of a draft to continue editing (optional)
do_main() {
# Load any available plugins from plugins directory in current path or /usr/share/bashblog/plugins
if [[ -d "${BASH_SOURCE[0]%/*}/plugins" ]]; then
for f in "${BASH_SOURCE[0]%/*}/plugins/"*.sh ; do
source "$f"
done
fi
if [[ -d "/usr/share/bashblog/plugins" ]]; then
for f in "/usr/share/bashblog/plugins/"*.sh ; do
source "$f"
done
fi
# Get a list of all functions available.
mapfile -t plugins < <(declare -F)
# Loop through the function names and strip away stuff that is not needed.
for i in "${!plugins[@]}" ; do
plugins[i]="${plugins[i]#*f }"
# Remove functions that do not start with plugin_
if ! [[ "${plugins[i]}" =~ ^plugin_ ]]; then
unset plugins[i]
fi
done
# Detect if using BSD date or GNU date
date_version_detect
# Load default configuration, then override settings with the config file
Expand All @@ -1129,7 +1155,8 @@ do_main() {
[[ -z $EDITOR ]] &&
echo "Please set your \$EDITOR environment variable. For example, to use nano, add the line 'export EDITOR=nano' to your \$HOME/.bashrc file" && exit

# Check for validity of argument
# Check for validity of argument including plugins
LC_ALL=C type "plugin_command_$1" 2> /dev/null | grep -q "plugin_command_$1 is a function" && call_plugins command_$1
[[ $1 != "reset" && $1 != "post" && $1 != "rebuild" && $1 != "list" && $1 != "edit" && $1 != "delete" && $1 != "tags" ]] &&
usage && exit

Expand Down Expand Up @@ -1185,6 +1212,18 @@ do_main() {
}


# Plugins
call_plugins() {
local functionName="$1"
shift
for i in "${plugins[@]}" ; do
if [[ "$i" =~ ^plugin_${functionName} ]]; then
${i} "$@"
fi
done
}


#
# MAIN
# Do not change anything here. If you want to modify the code, edit do_main()
Expand Down