mirror of
https://github.com/nicholaswilde/recipes.git
synced 2025-12-01 12:36:44 +00:00
173 lines
4.0 KiB
Bash
173 lines
4.0 KiB
Bash
#!/bin/bash
|
|
|
|
ROOT_DIR="$(git rev-parse --show-toplevel)"
|
|
DOCS_PATH="${ROOT_DIR}/docs"
|
|
COOK_PATH="${ROOT_DIR}/cook"
|
|
IMAGES_PATH="${DOCS_PATH}/assets/images"
|
|
|
|
# readonly DIR
|
|
readonly ROOT_DIR
|
|
readonly DOCS_PATH
|
|
readonly IMAGES_PATH
|
|
readonly COOK_PATH
|
|
|
|
# Check is variable is null
|
|
function is_null {
|
|
[ -z "${1}" ]
|
|
}
|
|
|
|
# Check if command exists
|
|
function command_exists(){
|
|
command -v "${1}" &> /dev/null
|
|
}
|
|
|
|
# Check if file exists
|
|
function file_exists(){
|
|
[ -f "{1}" ]
|
|
}
|
|
|
|
# printf usage_error if something isn't right.
|
|
function usage_error() {
|
|
show_usage "${1}"
|
|
printf "\nTry %s -h for more options.\n" "${1}" >&2
|
|
exit 1
|
|
}
|
|
|
|
function show_usage(){
|
|
printf "Usage: %s [OPTIONS] FILENAMES\n" "${1}"
|
|
}
|
|
|
|
function show_version(){
|
|
printf "%s version %s\n" "${1}" "${2}"; exit 0
|
|
}
|
|
|
|
function script_desc(){
|
|
printf "%s\n\n" "${1}"
|
|
}
|
|
|
|
# Show the help
|
|
function show_help(){
|
|
show_usage "${1}"
|
|
script_desc "${2}"
|
|
printf "Mandatory arguments:\n"
|
|
printf " FILENAME The cook filename to move\n\n"
|
|
printf "Options:\n"
|
|
printf " -h, --help Print this Help.\n"
|
|
printf " -v, --version Print script version and exit.\n"
|
|
exit 0
|
|
}
|
|
|
|
function get_category(){
|
|
d1=$(basename "$(dirname "${1}")")
|
|
d2=$(basename "$(dirname "$(dirname "${1}")")")
|
|
if [ "${d2}" == "cook" ]; then
|
|
printf "%s\n" "${d1}"
|
|
else
|
|
printf "%s/%s\n" "${d2}" "${d1}"
|
|
fi
|
|
}
|
|
|
|
function show_error(){
|
|
printf "Could not get \`%s\`\n" "${1}"
|
|
exit 1
|
|
}
|
|
|
|
function get_extension() {
|
|
printf '%s\n' "${1#*.}"
|
|
}
|
|
|
|
function to_lower() {
|
|
s="${1// /-}"
|
|
printf '%s\n' "${s,,}"
|
|
}
|
|
|
|
function remove_extension() {
|
|
printf '%s\n' "${1%%.*}"
|
|
}
|
|
|
|
function get_image() {
|
|
s="${1}"
|
|
if [ -f "${s}.jpg" ]; then
|
|
readlink -f "${s}.jpg"
|
|
elif [ -f "${s}.png" ]; then
|
|
readlink -f "${s}.png"
|
|
fi
|
|
}
|
|
|
|
function get_filename() {
|
|
basename "${1}"
|
|
}
|
|
|
|
function get_recipe_path(){
|
|
s=$(readlink -f "${1}")
|
|
printf '%s\n' "${s}"
|
|
}
|
|
|
|
function get_recipe_filename(){
|
|
get_filename "${1}"
|
|
}
|
|
|
|
function get_recipe_name(){
|
|
recipe_filename=$(get_filename "${1}")
|
|
remove_extension "${recipe_filename}"
|
|
}
|
|
|
|
function get_markdown_path(){
|
|
recipe_name=$(get_recipe_name "${1}")
|
|
category=$(get_category "${1}")
|
|
lower=$(to_lower "${recipe_name}")
|
|
readlink -f "${COOK_PATH}/${category}/${lower}.md"
|
|
}
|
|
|
|
function get_image_path(){
|
|
category=$(get_category "${1}")
|
|
recipe_name=$(get_recipe_name "${1}")
|
|
get_image "${COOK_PATH}/${category}/${recipe_name}"
|
|
}
|
|
|
|
function get_new_image_path(){
|
|
recipe_name=$(get_recipe_name "${1}")
|
|
lower=$(to_lower "${recipe_name}")
|
|
image_path=$(get_image_path "${1}")
|
|
image_extension=$(get_extension "${image_path}")
|
|
printf "%s/%s.%s" "${IMAGES_PATH}" "${lower}" "${image_extension}"
|
|
}
|
|
|
|
function get_new_markdown_path(){
|
|
recipe_name=$(get_recipe_name "${1}")
|
|
category=$(get_category "${1}")
|
|
lower=$(to_lower "${recipe_name}")
|
|
printf "%s/%s/%s.md" "${DOCS_PATH}" "${category}" "${lower}"
|
|
}
|
|
|
|
function debug_print(){
|
|
recipe_path=$(get_recipe_path "${1}")
|
|
printf "recipe_path: %s\n" "${recipe_path}"
|
|
printf "category: %s\n" "$(get_category "${recipe_path}")"
|
|
printf "recipe_filename: %s\n" "$(get_filename "${recipe_path}")"
|
|
printf "recipe_name: %s\n" "$(get_recipe_name "${recipe_path}")"
|
|
printf "markdown_path: %s\n" "$(get_markdown_path "${recipe_path}")"
|
|
printf "image_path: %s\n" "$(get_image_path "${recipe_path}")"
|
|
printf "new_image_path: %s\n" "$(get_new_image_path "${recipe_path}")"
|
|
printf "new_markdown_path: %s\n" "$(get_new_markdown_path "${recipe_path}")"
|
|
}
|
|
|
|
function do_checks(){
|
|
filename=$(get_filename "${1}")
|
|
file_extension=$(get_extension "${filename}")
|
|
if ! [ "${file_extension}" == "cook" ]; then
|
|
printf "File extension is not \`cook\`, %s\n" "${filename}"
|
|
exit 1
|
|
fi
|
|
test -f "${1}" || (printf "File does not exist, %s\n" "${1}" && exit 1)
|
|
mime_type=$(get_mime_type "${1}")
|
|
if ! [ "${mime_type}" == "text/plain" ]; then
|
|
printf "Mime type is not \`tex/plain\`, %s\n" "${mime_type}"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
function get_mime_type(){
|
|
file -b --mime-type "${1}"
|
|
}
|