|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: the secureCodeBox authors |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: Apache-2.0 |
| 6 | + |
| 7 | +# |
| 8 | +# Helper script to create new blog posts |
| 9 | +# |
| 10 | +# What does this script for you? |
| 11 | +# 1. It creates the proper filename (eg. blog/2020-10-01-this-is-a-title.md). |
| 12 | +# - current date |
| 13 | +# - lower case |
| 14 | +# - spaces replaced by dashes |
| 15 | +# 2. Adds basic frontmatter into it. |
| 16 | +# |
| 17 | +# You may place a file .author_meta into the root of the repo to define some variables |
| 18 | +# for the frontmatter. The script will tell you if no meta file was found. |
| 19 | +# |
| 20 | + |
| 21 | +set -ue |
| 22 | + |
| 23 | +# @see: http://wiki.bash-hackers.org/syntax/shellvars |
| 24 | +[ -z "${SCRIPT_DIRECTORY:-}" ] \ |
| 25 | + && SCRIPT_DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" \ |
| 26 | + && export SCRIPT_DIRECTORY |
| 27 | + |
| 28 | +BASE_DIR=$(dirname "${SCRIPT_DIRECTORY}")/docs-builder |
| 29 | +BLOG_DIR="${BASE_DIR}/blog" |
| 30 | +USAGE="Usage: $(basename "$0") 'The title of Your Post'" |
| 31 | + |
| 32 | +if (( $# != 1 )); then |
| 33 | + echo "Error: The title is missing!" |
| 34 | + echo "${USAGE}" |
| 35 | + exit 1 |
| 36 | +fi |
| 37 | + |
| 38 | +if [ '-h' = "${1}" ]; then |
| 39 | + echo "${USAGE}" |
| 40 | + exit 0 |
| 41 | +fi |
| 42 | + |
| 43 | +AUTHOR_META_FILE="${BASE_DIR}/.author_meta" |
| 44 | +if [ -f "${AUTHOR_META_FILE}" ]; then |
| 45 | + echo "Using author meta file ${AUTHOR_META_FILE}." |
| 46 | + # shellcheck disable=SC1090 |
| 47 | + source "${AUTHOR_META_FILE}" |
| 48 | +else |
| 49 | + echo "No author meta file found at ${AUTHOR_META_FILE}!" |
| 50 | + echo |
| 51 | + echo "You could use one to predefine some variables. Just put this:" |
| 52 | + echo |
| 53 | + echo "AUTHOR='Gordon Shumway'" |
| 54 | + echo "AUTHOR_TITLE='Core Developer'" |
| 55 | + echo "AUTHOR_URL='https://...'" |
| 56 | + echo "AUTHOR_IMAGE_URL='https://...'" |
| 57 | + echo |
| 58 | + echo "Into the file ${AUTHOR_META_FILE}." |
| 59 | +fi |
| 60 | + |
| 61 | +TITLE="${1}" |
| 62 | +TITLE_CLEANSED=$(echo "${TITLE}" | tr '[:upper:]' '[:lower:]' | tr ' ' '-') |
| 63 | +DATE=$(date +'%Y-%m-%d') |
| 64 | +BLOG_POST_FILE="${BLOG_DIR}/${DATE}-${TITLE_CLEANSED}.md" |
| 65 | +cat << EOF > "${BLOG_POST_FILE}" |
| 66 | +--- |
| 67 | +title: ${TITLE} |
| 68 | +author: ${AUTHOR:-} |
| 69 | +author_title: ${AUTHOR_TITLE:-} |
| 70 | +author_url: ${AUTHOR_URL:-} |
| 71 | +author_image_url: ${AUTHOR_IMAGE_URL:-} |
| 72 | +tags: |
| 73 | +description: |
| 74 | +image: |
| 75 | +draft: true |
| 76 | +--- |
| 77 | +EOF |