refactored all post creation to single function

This commit is contained in:
James Tomasino 2018-03-03 11:48:39 -05:00
parent 734953ecb4
commit 033c94e9d7
1 changed files with 135 additions and 111 deletions

246
burrow
View File

@ -10,15 +10,16 @@ editor=${EDITOR:-vi}
config_dir_gopher="$HOME/gopher/"
config_dir_recipebox="recipebox"
config_dir_phlog="phlog"
config_dir_topics="topics"
config_gopher_server="sdf.org"
config_gopher_port="70"
config_gopher_root="/users/username/"
config_post_phlog_command=""
config_post_recipebox_command=""
config_git_commit=false
config_git_push=false
config_autoindent=true
config_custom_editor=false
# vars from flags
flag_debug=0
@ -32,6 +33,7 @@ arg_longoptions=help,version,debug,noautoindent
arg_shortlist=0
arg_recipe=0
arg_phlog=0
arg_topic=0
arg_create_config=0
arg_update_git=0
@ -51,6 +53,7 @@ burrow [options] [commands]
COMMANDS:
phlog Create new phlog entry
recipe Add new recipe to box
topic Add or update a topic
create-config Create a default configuration file
update-git Silently pulls gopher git repo if it exists
@ -106,6 +109,7 @@ function parse_input() {
"shortlist") arg_shortlist=1 ;;
"phlog") arg_phlog=1 ;;
"recipe") arg_recipe=1 ;;
"topic") arg_topic=1 ;;
"create-config") arg_create_config=1 ;;
"update-git") arg_update_git=1 ;;
*) echo "Unknown command: $arg";;
@ -135,55 +139,138 @@ function check_directory() {
fi
}
function recipe_new() {
read -r -e -p "What is the name of your recipe: " title
function finish {
if [[ -f "$temp_gophermap" ]]; then
rm "$temp_gophermap"
fi
if [[ -f "$temp_links" ]]; then
rm "$temp_links"
fi
}
trap finish EXIT
function make_post() {
query="$1"
type="$2"
use_gophermap="$3"
use_date="$4"
update_root="$5"
read -r -e -p "$query" title
if [[ $title == "" ]]
then
echo "Cancelled."
exit 0
fi
title_slug=$(echo "${title}" | sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | tr -s '-' | tr '[:upper:]' '[:lower:]')
post_path="${config_dir_gopher}${config_dir_recipebox}/$title_slug.txt"
type_gophermap="${config_dir_gopher}${type}/gophermap"
if [[ -f $post_path ]]
then
echo "$post_path already exists"
$editor "$post_path"
else
if [[ -f "${config_dir_gopher}${config_dir_recipebox}/.template" ]]
then
cat "${config_dir_gopher}${config_dir_recipebox}/.template" > "$post_path"
if $use_gophermap; then
title_slug=$(echo "${title}" | sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | tr -s '-' | tr '[:upper:]' '[:lower:]')
if $use_date; then
post_dir="${config_dir_gopher}${type}/$(date +%Y%m%d)-$title_slug"
post_path="${config_gopher_root}${type}/$(date +%Y%m%d)-$title_slug"
else
echo "Creating $post_path"
post_dir="${config_dir_gopher}${type}/$title_slug"
post_path="${config_gopher_root}${type}/$title_slug"
fi
post_file="${post_dir}/gophermap"
post_file_path="${post_path}"
else
post_dir="${config_dir_gopher}${type}"
post_path="${config_gopher_root}${type}"
title_slug=$(sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' "${title}" | tr -s '-' | tr '[:upper:]' '[:lower:]')
if $use_date; then
title_slug="$(date +%Y%m%d)-${title_slug}"
fi
post_file="${post_dir}/${title_slug}.txt"
post_file_path="${post_path}/${title_slug}.txt"
fi
if [[ -f $post_file ]]
then
$editor "$post_file"
else
mkdir -p "${post_dir}"
if [[ -f "${post_dir}/.template" ]]
then
cat "${post_dir}/.template" > "$post_file"
else
echo "Creating $post_file"
{
echo "----------------------------------------"
echo "$title"
if $use_date; then
date +"%B %d$(day_suffix), %Y"
fi
echo "----------------------------------------"
echo ""
echo ""
} >> "$post_path"
if $use_gophermap; then
echo "Links:"
echo ""
fi
} >> "$post_file"
fi
$editor "$post_path"
$editor "$post_file"
echo "Updating recipebox listing"
echo -e "0$title\t${config_gopher_root}${config_dir_recipebox}/$(basename "$post_path")\t${config_gopher_server}\t${config_gopher_port}\n$(cat "${config_dir_gopher}${config_dir_recipebox}/gophermap")" > "${config_dir_gopher}${config_dir_recipebox}/gophermap"
sort -fo "${config_dir_gopher}${config_dir_recipebox}/gophermap" "${config_dir_gopher}${config_dir_recipebox}/gophermap"
update_gopher_date
if $use_gophermap; then
if [[ $config_autoindent == true ]] && [[ $flag_noautoindent == 0 ]]
then
echo "Processing for links and autoindenting"
temp_links=$(mktemp -t "$(basename "$0").links.XXXXXXX") || exit 1
sed -n '/^Links:$/,$p' "$post_file" | tail -n +2 > "$temp_links"
sed -i '/^Links:$/,$d' "$post_file"
sed -i 's/^/ /' "$post_file"
cat "$temp_links" >> "$post_file"
rm "$temp_links"
fi
fi
if [[ $config_post_recipebox_command != "" ]]
then
echo "Running post-recipe command"
$config_post_recipebox_command
echo "Updating $type"
temp_gophermap=$(mktemp -t "$(basename "$0").gophermap.XXXXXXX") || exit 1
if $use_gophermap; then
if $use_date; then
# if using gophermap and date
echo -e "1$(date +%Y-%m-%d) - $title\t${post_file_path}\t${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap"
else
# if using gophermap but not date
echo -e "1$title\t${post_file_path}\t${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap"
fi
else
if $use_date; then
# if not using gophermap but using date
echo -e "0$(date +%Y-%m-%d) - $title\t${post_file_path}\t${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap"
else
# if not using gophermap or date
echo -e "0$title\t${post_file_path}\t${config_gopher_server}\t${config_gopher_port}" > "$temp_gophermap"
fi
fi
cat "$type_gophermap" >> "$temp_gophermap"
cat "$temp_gophermap" > "$type_gophermap"
rm "$temp_gophermap"
# sort gophermap if not using date
if ! $use_date; then
sort -fo "${post_dir}/gophermap" "${post_dir}/gophermap"
fi
if $update_root; then
update_gopher_date
fi
if [[ $config_git_commit != false ]]
then
echo "Committing to git repository"
pushd "$config_dir_gopher"
git add "${config_dir_recipebox}/gophermap" "${post_path}" "${config_dir_gopher}/gophermap"
git commit -m "Recipe: $title"
git add "${post_dir}/gophermap" "${post_file}"
if $update_root; then
git add "${config_dir_gopher}/gophermap"
fi
git commit -m "$type: $title"
if [[ $config_git_push != false ]]
then
echo "Pushing to git remote"
@ -195,81 +282,16 @@ function recipe_new() {
fi
}
function topic_new() {
make_post "What topic?" "$config_dir_topics" false false true
}
function recipe_new() {
make_post "What is the name of your recipe?" "$config_dir_recipebox" false false true
}
function phlog_new() {
read -r -e -p "Enter a title for your post: " title
if [[ $title == "" ]]
then
echo "Cancelled."
exit 0
fi
title_slug=$(echo "${title}" | sed -E -e 's/[^[:alnum:]]/-/g' -e 's/^-+|-+$//g' | tr -s '-' | tr '[:upper:]' '[:lower:]')
post_dir="${config_dir_gopher}${config_dir_phlog}/$(date +%Y%m%d)-$title_slug"
post_path="$post_dir/gophermap"
if [[ -f $post_path ]]
then
echo "$post_path already exists"
$editor "$post_path"
else
if [[ -f "${config_dir_gopher}${config_dir_phlog}/.template" ]]
then
cat "${config_dir_gopher}${config_dir_phlog}/.template" > "$post_path"
else
echo "Creating $post_path"
mkdir -p "$post_dir"
{
echo "----------------------------------------"
echo "$title"
date +"%B %d$(day_suffix), %Y"
echo "----------------------------------------"
echo ""
echo ""
echo ""
echo "Links:"
echo ""
} >> "$post_path"
fi
$editor "$post_path"
if [[ $config_autoindent == true ]] && [[ $flag_noautoindent == 0 ]]
then
echo "Processing phlog for links and autoindenting"
links="${post_dir}/links"
sed -n '/^Links:$/,$p' "$post_path" | tail -n +2 > "$links"
sed -i '/^Links:$/,$d' "$post_path"
sed -i 's/^/ /' "$post_path"
cat "$links" >> "$post_path"
rm "$links"
fi
echo "Updating phlog listing"
echo -e "1$(date +%Y-%m-%d) - $title\t${config_gopher_root}${config_dir_phlog}/$(basename "$post_dir")\t${config_gopher_server}\t${config_gopher_port}\n$(cat "${config_dir_gopher}${config_dir_phlog}/gophermap")" > "${config_dir_gopher}${config_dir_phlog}/gophermap"
update_gopher_date
if [[ $config_post_phlog_command != "" ]]
then
echo "Running post-phlog command"
$config_post_phlog_command
fi
if [[ $config_git_commit != false ]]
then
echo "Committing to git repository"
pushd "$config_dir_gopher"
git add "${config_dir_phlog}/gophermap" "${post_path}" "${config_dir_gopher}/gophermap"
git commit -m "Phlog: $title"
if [[ $config_git_push != false ]]
then
echo "Pushing to git remote"
git pull
git push
fi
popd
fi
fi
make_post "Enter a title for your post:" "$config_dir_phlog" true true true
}
function create_config() {
@ -283,13 +305,13 @@ function create_config() {
echo "config_dir_gopher=\"$HOME/gopher/\""
echo "config_dir_phlog=\"phlog\""
echo "config_dir_recipebox=\"recipebox\""
echo "config_git_commit=false"
echo "config_dir_topics=\"topics\""
echo "config_gopher_server=\"sdf.org\""
echo "config_gopher_port=\"70\""
echo "config_gopher_root=\"/users/tomasino/\""
echo "config_gopher_root=\"/users/username/\""
echo "config_git_commit=false"
echo "config_git_push=false"
echo "config_autoindent=true"
echo "config_custom_editor=false"
} >> "$config"
else
echo "Configuration already exists. Abort."
@ -313,7 +335,7 @@ function main() {
if [[ $arg_shortlist -gt 0 ]]
then
echo "phlog recipe create-config update-git -v -h -d --version --help --debug --noautoindent"
echo "phlog topic recipe create-config update-git -v -h -d --version --help --debug --noautoindent"
exit 0
fi
@ -345,11 +367,6 @@ function main() {
fi
done
if [[ $config_custom_editor != false ]]
then
editor=$config_custom_editor
fi
if [[ ${arg_update_git} -gt 0 ]]
then
update_git
@ -362,6 +379,13 @@ function main() {
recipe_new
fi
if [[ ${arg_topic} -gt 0 ]]
then
check_directory "$config_dir_gopher"
check_directory "${config_dir_gopher}${config_dir_topics}"
topic_new
fi
if [[ ${arg_phlog} -gt 0 ]]
then
check_directory "$config_dir_gopher"