Last active
April 11, 2024 18:09
-
-
Save andersonbosa/2596ce7856f08299e0e065ccfe878bb4 to your computer and use it in GitHub Desktop.
Add new item from a defined template. Useful for initing new documents like RFC's from a template.
This file contains 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/bash | |
# Author: https://github.com/andersonbosa | |
# Source: https://gist.githubusercontent.com/andersonbosa/2596ce7856f08299e0e065ccfe878bb4/raw/fd823a48c736288ab9d0b7da3cab05e2f60ff2a4/add_new_document.sh | |
set -e | |
####################################### | |
# Function to convert a string to title case | |
# Arguments: | |
# $1: String to be converted to title case | |
# Returns: | |
# Title-cased string | |
####################################### | |
title_case() { | |
local string="$@" | |
# Convert the string to lowercase and split it into an array of words | |
local -a words=($string) | |
# Iterate over each word in the array | |
for ((i = 0; i < ${#words[@]}; i++)); do | |
# Capitalize the first letter of each word | |
words[i]="${words[i]^}" | |
done | |
# Join the title-cased words back into a single string | |
local title_cased="${words[*]}" | |
echo "$title_cased" | |
} | |
####################################### | |
# Function to replace a keyword with a provided value in a text file | |
# Arguments: | |
# $1: File path of the text file | |
# $2: Keyword to be replaced | |
# $3: Value to replace the keyword with | |
# Returns: | |
# None | |
####################################### | |
replace_keyword() { | |
local file_path="$1" | |
local keyword="$2" | |
local keyword_value="$3" | |
# Check if the file exists | |
if [ ! -f "$file_path" ]; then | |
echo "Error: File $file_path not found." | |
return 1 | |
fi | |
# Replace the keyword with the provided value using sed | |
sed -i "s/$keyword/$keyword_value/g" "$file_path" | |
[ $VERBOSE ] && echo "[INFO] Keyword '$keyword' replaced with '$keyword_value' successfully in $file_path." | |
} | |
init_new_file_from_template() { | |
# Receive user input and sanitize it to serve as a filename | |
if [ -z "$1" ]; then | |
echo "[ERROR] Please provide an argument for the filename." | |
exit 1 | |
fi | |
# Store the current directory | |
original_dir=$(pwd) | |
# Check if the script is being executed in the correct folder | |
if [ "$(basename "$(pwd)")" != "$(basename "$0")" ]; then | |
# echo "[INFO] Please execute this script in the folder where it is located." | |
# Get the directory where the script is located | |
script_dir=$(dirname "$0") | |
# Change to that directory | |
cd "$script_dir" || exit | |
# echo "[INFO] Changing directory to $script_dir" | |
fi | |
# Capture all user input parameters | |
raw_filename="$@" | |
# Generate a datestamp for the filename | |
# datestamp=$(date +%Y-%m-%d_%H-%M-%S) | |
datestamp=$(date +%Y_%m_%d) | |
# Sanitize the input by removing invalid characters for filenames | |
filename=$(echo "$raw_filename" | sed 's/[^A-Za-z0-9._-]/_/g') | |
# Concatenate the datestamp and filename to generate a filename | |
new_filename="${datestamp}--${filename}.md" | |
# Copy the file 000000_template.md and paste with the newly created name | |
if [ ! -f "000000_template.md" ]; then | |
echo "[ERROR] The file 000000_template.md was not found." | |
exit 1 | |
fi | |
if [ -f $new_filename ]; then | |
echo "[ERROR] The file \"$new_filename\" already exists" | |
exit 1 | |
fi | |
cp "000000_template.md" "${new_filename}" | |
if [ $? -eq 0 ]; then | |
echo "[INFO] File ${new_filename} created successfully." | |
else | |
echo "[ERROR] Failed to create file ${new_filename}." | |
exit 1 | |
fi | |
replace_keyword $new_filename '{{title}}' "$(title_case $raw_filename)" | |
# Return to the original directory | |
cd "$original_dir" || exit | |
exit 0 | |
} | |
init_new_file_from_template $@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment