From 7c0de8b77d1b1f0a0ea512d29781df558c664b6c Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Fri, 4 Oct 2024 17:53:15 +0200 Subject: refactor(common/init): Rewrite the `quotify` function in python The AWK version was getting confused with newlines (`\n`) in the input. --- common/init | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/common/init b/common/init index 35662e2..4298e68 100644 --- a/common/init +++ b/common/init @@ -31,11 +31,14 @@ die() { exit 1 } quotify() { - input="$1" - - # The quotes are actually fine like they are. - # shellcheck disable=SC1003 - echo "$input" | awk '{ gsub(/'\''/, "'\''\\'\'''\''", $0); print("'\''" $0 "'\''"); }' + python -c " +import sys +if len(sys.argv) > 1: + text =''.join(sys.argv[1:]) +else: + text = sys.stdin.read() +quoted_text = text.replace(\"'\", \"'\\\\''\") +print(f\"'{quoted_text}'\", end='')" "$@" } # Prompt the user for a specific variable. @@ -80,6 +83,8 @@ require mv require sed require chmod +require python + # necessary meta data prompt APPLICATION_NAME "The name of the application" "$(basename "$PWD")" prompt APPLICATION_NAME_STYLIZED "The stylized name of the application (for documentation)" "$(echo "$APPLICATION_NAME" | sed 's/[_-]/ /g' | sed 's/^\(\w\)/\U\1/g' | sed 's/ \(\w\)/ \U\1/g')" -- cgit 1.4.1