3 Commits 926c2a8fa9 ... 4c23f6e384

Author SHA1 Message Date
  encrypt 4c23f6e384 add watch and fix flags on serve command 3 years ago
  encrypt 6f94289811 add publish callback 3 years ago
  encrypt 58a254e467 [shellcheck refactor] quotes 3 years ago
1 changed files with 31 additions and 14 deletions
  1. 31 14
      boh

+ 31 - 14
boh

@@ -20,11 +20,12 @@ boh_help() {
     echo "  build, b             Build your pages"
     echo "  serve, server, s     Serve your pages locally"
     echo "  new                  Creates boh basic stuff"
+    echo "  watch                Wait for changes then rebuild all the things"
 }
 
 boh_new() {
-    mkdir $destination_dir $layouts_dir
-    cat <<EOF > "$layouts_dir"/default.html
+    mkdir "$destination_dir" "$layouts_dir"
+    cat <<EOF > "$layouts_dir/default.html"
 <!doctype html>
 <html lang="en">
   <head>
@@ -39,12 +40,12 @@ EOF
 }
 
 boh_serve() {
-    cd $destination_dir
-    python2 -m SimpleHTTPServer
+    cd "$destination_dir"
+    python2 -m SimpleHTTPServer $@
 }
 
 boh_check() {
-    if [ ! -d $layouts_dir ];then
+    if [ ! -d "$layouts_dir" ];then
 	>&2 echo "'$layouts_dir' directory not found. Maybe you should 'boh new'"
 	exit 1
     fi
@@ -66,7 +67,7 @@ boh_check() {
 
 boh_parse_frontmatter(){
     frontmatter=$(cat "$1" | sed -n '1{/^---/{:a N;/\n---/!ba;p}}' | sed '1d;$d' | sed -e 's/:[^:\/\/]/="/g;s/$/"/g;s/ *=/=/g')
-    echo $frontmatter
+    echo "$frontmatter"
 }
 
 boh_markdown() {
@@ -76,11 +77,11 @@ boh_markdown() {
 boh_build() {
     echo "Building all the things..."
     rm -rf $destination_dir/*
-    mkdir -p $destination_dir
+    mkdir -p "$destination_dir"
     type boh_pre_build &> /dev/null && { boh_pre_build; }
     # extra "/" is used to increment the final count by 1
-    cut_at=$(echo "/"$source_dir | fold -w 1 | grep -c /)
-    find $source_dir -type f -name "*.md" | while read file
+    cut_at=$(echo "/$source_dir" | fold -w 1 | grep -c /)
+    find "$source_dir" -type f -name "*.md" | while read file
     do
 	layout="default"
 	title=$(echo "$file" | rev | cut -d / -f 1 | rev | sed s/.md//)
@@ -89,7 +90,7 @@ boh_build() {
 	newpath="$destination_dir/"$(echo "$file" | rev | cut -d . -f 2- | rev | cut -d / -f $cut_at-)".html"
 	frontmatter=$(boh_parse_frontmatter "$file")
 	if [ "$frontmatter" ];then
-	    eval $frontmatter
+	    eval "$frontmatter"
 	fi
 	mkdir -p "${newpath%/*}"
 	export title body
@@ -100,6 +101,15 @@ boh_build() {
     echo "Yay!"
 }
 
+boh_watch() {
+    echo "watching $(pwd)"
+    inotifywait -r -q -e close_write -m --exclude _site/ $(pwd) | while read events
+    do
+	boh_check
+	boh_build
+    done
+}
+
 ## defaults
 source_dir="./"
 destination_dir="./_site"
@@ -130,19 +140,26 @@ do
     esac
 done
 
+if [ -f "$source_dir/boh.config" ];then
+    source "$source_dir/boh.config"
+fi
+
 case $command in
     "build"|"b")
 	boh_check
-	if [ -f $source_dir"/boh.config" ];then
-	    source $source_dir"/boh.config"
-	fi
 	boh_build
 	;;
     "new")
 	boh_new
 	;;
     "serve"|"server"|"s")
-	boh_serve
+	boh_serve $@
+	;;
+    "publish")
+	type boh_publish &> /dev/null && { boh_publish; }
+	;;
+    "watch")
+	boh_watch
 	;;
     *)
 	boh_help