diff options
author | EuAndreh <eu@euandre.org> | 2022-05-02 22:05:19 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2022-05-02 22:05:19 -0300 |
commit | 1330592dee095e68cdfda1846006a6345445d0e5 (patch) | |
tree | 8eed575125e6b008f2b5885098e21b99c26a5e61 | |
parent | src/development/lib.sh: Add helper file (diff) | |
download | euandre.org-1330592dee095e68cdfda1846006a6345445d0e5.tar.gz euandre.org-1330592dee095e68cdfda1846006a6345445d0e5.tar.xz |
src/development/frontmatter-env.sh: Generate .env file from .md frontmatter
-rwxr-xr-x | src/development/frontmatter-env.sh | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/src/development/frontmatter-env.sh b/src/development/frontmatter-env.sh new file mode 100755 index 0000000..efe4f65 --- /dev/null +++ b/src/development/frontmatter-env.sh @@ -0,0 +1,91 @@ +#!/bin/sh +set -eu +. src/development/lib.sh + +usage() { + cat <<-'EOF' + Usage: + frontmatter-env.sh < STDIN + frontmatter-env.sh -h + EOF +} + +help() { + cat <<-'EOF' + + + Options: + -h, --help + + + Examples: + + FIXME: + + $ FIXME + EOF +} + +for flag in "$@"; do + case "$flag" in + --) + break + ;; + --help) + usage + help + exit + ;; + *) + ;; + esac +done + +while getopts 'h' flag; do + case "$flag" in + h) + usage + help + exit + ;; + *) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + +DELIM="$(uuid)" +awk -vDELIM="$DELIM" -F: ' + BEGIN { + separator = 0 + } + + /^---$/ { + separator++ + if (separator > 1) { + exit + } else { + next + } + } + + /^$/ { next } + + { + printf "%s=\"$(\n\tcat <<-\"%s\"\n\t\t", $1, DELIM + base_index = 1 + offset = base_index + length($1) + length(": ") + + # printf "\t%s\n", substr($0, offset + quote, length($0) - offset - quote) + # JEKYLL_COMPAT: quoted titles because of the frontmatter + if ($3 != "") { + quote = length("\"") + printf "%s", substr($0, offset + quote, length($0) - offset - quote) + } else { + printf "%s", substr($0, offset) + } + printf "\n\t%s\n)\"\n\n", DELIM + } +' |