diff --git a/README.md b/README.md index a0f760d..57f926e 100644 --- a/README.md +++ b/README.md @@ -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?! ;) diff --git a/bb.sh b/bb.sh index c034922..da50ff2 100755 --- a/bb.sh +++ b/bb.sh @@ -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 @@ -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" @@ -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" @@ -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 @@ -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 @@ -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 @@ -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()