move blog function into a script

This commit is contained in:
James Dixon 2020-05-27 16:42:25 -04:00
parent 86834a0fef
commit d00018a25d
3 changed files with 131 additions and 26 deletions

View File

@ -140,32 +140,7 @@ pathappend() {
done done
} }
bkup() { bkup() { if [ -f "$1" ]; then cp "${1}" "${1}.bkup.$(date +'%F.%R')"; fi }
# 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
}
# git # git
lazygit() { git commit -a -m "$*" && git push; } lazygit() { git commit -a -m "$*" && git push; }

View File

@ -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

105
scripts/hugoctl Executable file
View File

@ -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