#!/bin/bash # hugoctl -- hugo wrapper script to help manage a hugo blog # hugo variables (local machine) scriptname=$(basename "$0") blog_dir="$HOME/src/blog" # or the path to your blog local_url="http://localhost:1313/" post_filename="" postname="" full_post_dir="" full_post_url="" # deployment variables # TODO set deployment variables set_vars() { # handle extraneous .md file extension local arg="$1" if [[ ! "$arg" =~ .md$ ]]; then postname="$arg" else postname="${arg%???}" fi post_filename="$postname.md" # hugo post location for server full_post_dir="$blog_dir/content/posts/$post_filename" # url for browser full_post_url="$local_url/posts/$postname" } serve_and_open() { killall hugo > /dev/null 2>&1 hugo serve -s "$blog_dir" -D > /dev/null 2>&1 & xdg-open "$full_post_url" } edit_post() { serve_and_open $EDITOR "$full_post_dir" && kill %1 } new_post() { hugo new -s "$blog_dir" "posts/$post_filename" edit_post } deploy_blog() { # TODO make this transparent but don't leak stuff printf "${green}%s\n${reset}" "Deploying..." } print_help() { printf "%s\n" "Usage: " printf "%s\n\n" "$scriptname [args]" printf "%s\n" "Options: " printf "\t%s:\n\t\t%s\n\n" "new-post (aliases: newpost,np,new,n) [post-name]" "create a new post, open editor and live server" printf "\t%s:\n\t\t%s\n\n" "edit-post (alises: edit,ep,e) [post-name]" "edit a post in the content directory" printf "\t%s:\n\t\t%s\n\n" "list-post (aliases: list,lp,ls,l)" "list all posts in content directory" printf "\t%s:\n\t\t%s\n\n" "list-drafts (alises: listdraft,ld)" "list all posts in content directory" printf "\t%s:\n\t\t%s\n\n" "server (alises: serve,s)" "serve blog from your blog directory" printf "\t%s:\n\t\t%s\n\n" "kill (alises: killlall,ka,k)" "kill all hugo processes" printf "\t%s:\n\t\t%s\n\n" "deploy (aliases: dep,d)" "deploy to a remote server" printf "\n" } if [[ "$#" -lt 1 ]]; then print_help exit 1 fi while (($#)); do case "$1" in new-post|newpost|np|new|n) shift set_vars "$1" new_post "$1" break ;; edit-post|edit|ep|e) shift set_vars "$1" edit_post "$1" break ;; list-post|list|lp|l|ls) # hugo list all | cut -d, -f1 | tail -n +2 hugo list all | cut -f1,4 -d, | tail -n +2 | tr ',' ' ' | column -t break ;; list-drafts|listdraft|ld) hugo list drafts -s "$blog_dir" break ;; server|serve|s) serve_and_open break ;; kill|killall|ka|k) killall "hugo" break ;; deploy|dep|d) deploy_blog break ;; help|-h|--help) print_help break ;; *) print_help break ;; esac done