From d00018a25dfc4bf8161dedd2a0c95793edcee8b7 Mon Sep 17 00:00:00 2001 From: James Dixon Date: Wed, 27 May 2020 16:42:25 -0400 Subject: [PATCH] move blog function into a script --- bash/bashrc | 27 +------ scripts/completions/hugoctl_comp.bash | 25 ++++++ scripts/hugoctl | 105 ++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 26 deletions(-) create mode 100644 scripts/completions/hugoctl_comp.bash create mode 100755 scripts/hugoctl diff --git a/bash/bashrc b/bash/bashrc index bb4c236..627518e 100644 --- a/bash/bashrc +++ b/bash/bashrc @@ -140,32 +140,7 @@ pathappend() { done } -bkup() { - # create backup file with date appended - if [ -f "$1" ]; then - cp "${1}" "${1}.bkup.$(date +'%F.%R')" - fi -} - -# hugo -- create and edit a new post -new-post() { -postname="" -if [ -z "$1" ]; then - echo -n "Please enter a postname: " - read -r postname -else - postname="$1" -fi - -post_filename="$postname.md" -blog_dir="$HOME/src/blog" -local_url="http://localhost:1313/posts/$postname" - -hugo new -s "$blog_dir" "posts/$post_filename" -hugo serve -s "$blog_dir" -D &> /dev/null & -xdg-open "$local_url" &> /dev/null & -vim "$blog_dir/content/posts/$post_filename" && kill %1 -} +bkup() { if [ -f "$1" ]; then cp "${1}" "${1}.bkup.$(date +'%F.%R')"; fi } # git lazygit() { git commit -a -m "$*" && git push; } diff --git a/scripts/completions/hugoctl_comp.bash b/scripts/completions/hugoctl_comp.bash new file mode 100644 index 0000000..da570fd --- /dev/null +++ b/scripts/completions/hugoctl_comp.bash @@ -0,0 +1,25 @@ +# bash completion for hugctl + +_hugctl_completions() { + local cur + cur="${COMP_WORDS[COMP_CWORD]}" + + local blog_dir="$HOME/src/blog" + case ${COMP_CWORD} in + 1) + OPTS="new list edit serve kill deploy" + compopt -o bashdefault -o default + COMPREPLY=( $(compgen -W "${OPTS[*]}" -- $cur) ) + ;; + 2) + case ${COMP_CWORD[2]} in + *) + COMPREPLY=($(compgen -W "$(find $blog_dir/content/posts/ -iname '*.md' -type f | xargs -I@ basename @)" -- $cur)); + ;; + esac + ;; + esac + return 0 +} + +complete -F _hugctl_completions hugctl diff --git a/scripts/hugoctl b/scripts/hugoctl new file mode 100755 index 0000000..ceff588 --- /dev/null +++ b/scripts/hugoctl @@ -0,0 +1,105 @@ +#!/bin/bash + +# hugoctl -- hugo wrapper script to help manage a hugo blog + +scriptname=$(basename "$0") +post_filename="$postname.md" +blog_dir="$HOME/src/blog" +local_url="http://localhost:1313/posts/$postname" + +new-post() { + postname="" + if [ -z "$1" ]; then + echo -n "Please enter a postname: " + read -r postname + else + postname="$1" + fi + + hugo new -s "$blog_dir" "posts/$post_filename" + hugo serve -s "$blog_dir" -D &> /dev/null & + xdg-open "$local_url" &> /dev/null & + $EDITOR "$blog_dir/content/posts/$post_filename" && kill %1 +} + +edit-post() { + filename="$1" + if [[ ! "$filename" =~ .md$ ]]; then + filename="$filename.md" + fi + postname="${filename%???}" + + hugo serve -s "$blog_dir" &> /dev/null & + xdg-open "http://localhost:1313/posts/$postname" + $EDITOR "$blog_dir/content/posts/$filename" +} + +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" "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" "edit-post (alises: edit,ep,e) [post-name]" "edit a post in the 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 + new-post "$1" + break + ;; + list-post|list|lp|l|ls) + ls -ltr "$blog_dir/content/posts" + break + ;; + list-drafts|listdraft|ld) + hugo list drafts -s "$blog_dir" + break + ;; + edit-post|edit|ep|e) + shift + edit-post "$1" + break + ;; + server|serve|s) + pkill "hugo" + hugo serve -s "$blog_dir" -D & + xdg-open "http://localhost:1313" + break + ;; + kill|killall|ka|k) + pkill "hugo" + break + ;; + deploy|dep|d) + deploy_blog + break + ;; + help|-h|--help) + print_help + break + ;; + *) + print_help + break + ;; + esac +done