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:
|
|
|
|
wd2 [FLAGS...] file.d2 [file.svg | file.png]
|
|
|
|
|
2023-02-25 22:11:14 +03:00
|
|
|
A wrapper over d2 which allows to use 'layout', 'theme', 'dark-theme',
|
|
|
|
'pad', 'sketch' and 'port' attributes from d2 file. All arguments will
|
|
|
|
be passed to the d2 cli, but these additional configs overwrite cli
|
2023-02-16 23:52:40 +03:00
|
|
|
arguments with the same name.
|
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-02-17 00:44:20 +03:00
|
|
|
head -n10 "$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")
|
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}} \
|
|
|
|
${d2_port:+--port=${d2_port}}
|
2022-12-12 14:32:40 +03:00
|
|
|
|