Created
April 11, 2021 22:04
-
-
Save loweryaustin/a517e7092f4e4e8046624a8ec26002dc to your computer and use it in GitHub Desktop.
A POSIX shell script designed to hook onto the hugo binary to enable immediate editing of newly created markdown files / posts.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
### The MIT License (MIT) | |
### Copyright © 2021 Austin Lowery | |
### | |
### Permission is hereby granted, free of charge, to any person obtaining a copy | |
### of this software and associated documentation files (the “Software”), to deal | |
### in the Software without restriction, including without limitation the rights | |
### to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
### copies of the Software, and to permit persons to whom the Software is furnished | |
### to do so, subject to the following conditions: | |
### | |
### The above copyright notice and this permission notice shall be included in all | |
### copies or substantial portions of the Software. | |
### | |
### THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
### IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | |
### FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | |
### COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | |
### IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |
### WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
############################ | |
### | |
### Purpose: To provide an insanely over-engineered solution for saving the 0.735 seconds | |
### it takes to manually open for editing the markdown file that hugo generates on: | |
### hugo new path/to/markdown.md | |
### | |
### Dependencies: $EDITOR, sh, expr, read, sed, wc, printf, column, date, hugo | |
### | |
### Written and Tested on: Ubuntu 20.10 | |
### | |
### Note: While this is written in /bin/sh in an attempt to be portable, YYMV | |
### due to the fact that this is my first pure /bin/sh script. Feedback/advice | |
### is always welcome. | |
### | |
############################ | |
### | |
### Installation: | |
### | |
### 1. Copy this script to any directory within your $PATH and name it hugoHook | |
### (or whatever you like) | |
### | |
### 2. chmod +x /path/to/your/hugoHook | |
### | |
### 3. Add the alias to your favorite config script: ~/.bashrc ~/.bash_aliases etc | |
### alias hugo="hugoHook" | |
### | |
### 4. source ~/.bashrc | |
### | |
############################# | |
### | |
### Usage and Example Output: | |
### | |
### $ hugo new posts/my-first-blog-post.md | |
### | |
### Edit /home/user/Websites/myfirstblog/content/posts/my-first-blog-post.md ? [y/n] | |
### y | |
### | |
### Edit stats: | |
### Mins: 01 | |
### Secs: 15 | |
### Lines: 7 | |
### Words: 53 | |
### Chars: 282 | |
### Bytes: 282 | |
### | |
############################# | |
### | |
### Option Vars | |
### | |
hugoBinary="/usr/bin/hugo" | |
printStats="true" | |
setEditorIfEmpty () { | |
[ -z "$EDITOR" ] && printf "\nThe \e[1;34;4m\$EDITOR\e[0m evironment variable is not set. Using \e[32mvim\e[0m.(as one should anyway)\nYou get what you get. Don't throw a fit.\n\n" && EDITOR="vim" | |
} | |
### | |
### Meat and Potatoes | |
### | |
if expr "$2" : ".*md$" 1>/dev/null | |
then | |
# If an md file was passed to hugo as the second arg | |
# Examine the hugo output and catch the newly created | |
# filename. Prompt the user so that they can edit it | |
# directly after creation if they so desire. | |
hugoStdOut=$($hugoBinary "$@") | |
if ! expr "$hugoStdOut" : ".*md.*" 1>/dev/null | |
then | |
# If we can't find an md file in stdout we | |
# should just print whatever we found and bail | |
printf "%s\n" "$hugoStdOut" && exit | |
fi | |
mdFile=$(printf "%s" "$hugoStdOut" | sed 's/\(^.*md\).*/\1/') | |
setEditorIfEmpty | |
printf "Edit \e[32m%s\e[0m ? [y/n]\n" "$mdFile" | |
read -r YNPROMPT | |
if expr "$YNPROMPT" : "[yY]" 1>/dev/null | |
then | |
tstart=$(date +%s) | |
$EDITOR "$mdFile" | |
tfinish=$(date +%s) | |
totalSecs=$(( $tfinish - $tstart )) | |
stats="$(date -d@$totalSecs -u +'aHours: %H\naMins: %M\naSecs: %S' | sed 's/a.*00\\n//g;s/a/\\e\[1m/g;s/:/:\\e\[0m/g;')\n" | |
noHeaderMd=$(sed '/^---/,/^---/d;s/\s*//;/^$/d' "$mdFile") | |
stats=$stats"\e[1mLines:\e[0m $(printf "%s\n" "$noHeaderMd" | wc -l)\n" | |
stats=$stats"\e[1mWords:\e[0m $(printf "%s" "$noHeaderMd" | wc -w)\n" | |
stats=$stats"\e[1mChars:\e[0m $(printf "%s" "$noHeaderMd" | wc -c)\n" | |
bytes="$(printf "%s" "$noHeaderMd" | wc -m)" | |
mebibytes="$(( $bytes / 1048576 ))" | |
[ "$mebibytes" -gt 0 ] && stats=$stats"\e[1mMiBs:\e[0m $mebibytes\n" | |
[ "$mebibytes" -eq 0 ] && stats=$stats"\e[1mBytes:\e[0m $bytes\n" | |
printf "\n" | |
[ $printStats = "true" ] && printf "\e[1;34;4mEdit stats:\e[0m\n" && printf "$stats" | column -t | |
fi | |
else | |
# Otherwise pass the hugo command through sans action or alteration | |
$hugoBinary "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment