wd2/wd2

109 lines
2.3 KiB
Plaintext
Raw Permalink Normal View History

2022-12-12 14:32:40 +03:00
#! /usr/bin/env bash
2022-12-13 15:12:06 +03:00
set -e
2022-12-12 14:32:40 +03:00
d2_args=( )
d2_input_file=
function print_help() {
cat <<EOF
Usage:
2023-03-16 11:46:01 +03:00
wd2 [--watch] [FLAGS...] file.d2 [file.svg | file.png]
2022-12-12 14:32:40 +03:00
2023-03-16 11:46:01 +03:00
A wrapper over d2 which allows to use cli/env configuratios as additional
specific attributes from d2 file. All these attributes will be passed to the d2
cli and overwrite cli parameters with the same name.
# Supported configurations
- port
- layout
- theme
- dark-theme
- pad
- sketch
- force-appendix
2023-03-20 17:05:45 +03:00
- center
- animated-interval
2023-04-03 18:34:42 +03:00
- font-regular
- font-italic
- font-bold
2023-03-16 11:46:01 +03:00
-------------------------------------------------------------------------------
2022-12-12 14:32:40 +03:00
2022-12-12 17:59:50 +03:00
$(d2 --help | tail -n +6 - | sed -e 's/d2 \(layout\|fmt\)/wd2 \1/')
2022-12-12 14:32:40 +03:00
EOF
}
function parse_args() {
if [ "$#" == "0" ]; then
print_help
exit 1
fi
while (( "$#" )); do
case "$1" in
--help)
print_help
exit 0
;;
# extract input file arg
*.d2)
d2_input_file="$1"
d2_args+=( "$1" )
;;
# other args
*)
d2_args+=( "$1" )
;;
esac
shift
done
}
function get_attr_value() {
2023-03-16 11:46:01 +03:00
grep "^#" "$d2_input_file" \
| grep "$1" \
| awk -F"$1: " '{ print $2 }' \
| cut -d';' -f1
2022-12-12 14:32:40 +03:00
}
parse_args "$@"
if [ -z "$d2_input_file" ]; then
print_help
exit 1
fi
2022-12-13 15:12:06 +03:00
set +e
2022-12-12 14:32:40 +03:00
d2_layout=$(get_attr_value "layout")
d2_theme=$(get_attr_value "theme")
2023-02-25 22:11:14 +03:00
d2_dark_theme=$(get_attr_value "dark-theme")
2022-12-12 14:32:40 +03:00
d2_pad=$(get_attr_value "pad")
2022-12-26 23:47:00 +03:00
d2_sketch=$(get_attr_value "sketch")
2023-02-16 23:52:40 +03:00
d2_port=$(get_attr_value "port")
2023-03-16 11:46:01 +03:00
d2_force_appendix=$(get_attr_value "force-appendix")
2023-03-20 17:05:45 +03:00
d2_center=$(get_attr_value "center")
d2_animated_interval=$(get_attr_value "animated-interval")
2023-04-03 18:34:42 +03:00
d2_font_regular=$(get_attr_value "font-regular")
d2_font_italic=$(get_attr_value "font-italic")
d2_font_bold=$(get_attr_value "font-bold")
2022-12-13 15:12:06 +03:00
set -e
2022-12-12 14:32:40 +03:00
d2 "${d2_args[@]}" \
2022-12-26 23:47:00 +03:00
${d2_layout:+--layout=${d2_layout}} \
${d2_theme:+--theme=${d2_theme}} \
2023-02-25 22:11:14 +03:00
${d2_dark_theme:+--dark-theme=${d2_dark_theme}} \
2022-12-26 23:47:00 +03:00
${d2_pad:+--pad=${d2_pad}} \
2023-02-16 23:52:40 +03:00
${d2_sketch:+--sketch=${d2_sketch}} \
2023-03-16 11:46:01 +03:00
${d2_port:+--port=${d2_port}} \
${d2_force_appendix:+--force-appendix=${d2_force_appendix}}
${d2_center:+--center=${d2_center}} \
2023-04-03 18:34:42 +03:00
${d2_animated_interval:+--animated-interval=${d2_animated_interval}} \
${d2_font_regular:+--font-regular=${d2_font_regular}} \
${d2_font_italic:+--font-italic=${d2_font_italic}} \
${d2_font_bold:+--font-bold=${d2_font_bold}}
2022-12-12 14:32:40 +03:00