about summary refs log tree commit diff stats
path: root/bonus
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-01-08 00:37:54 +0100
committerbptato <nincsnevem662@gmail.com>2024-01-08 00:37:54 +0100
commitb0547ba9f48bf402665b89f84b88b80ee58d8824 (patch)
tree39bf645df6ea365f912040255eb136c5deafe2b1 /bonus
parent10a02b2ae2613b383453ba99318330560e9371ac (diff)
downloadchawan-b0547ba9f48bf402665b89f84b88b80ee58d8824.tar.gz
Add urlenc, urldec; fix a URL encoding bug; improve trans.cgi
* Fix incorrect internal definition of the fragment percent-encode set
* urlenc, urldec: these are simple utility programs mainly for use
  with shell local CGI scripts. (Sadly the printf + xargs solution is
  not portable.)
* Pass libexec directory as an env var to local CGI scripts
* Update trans.cgi to use urldec and add an example for combining
  it with selections
Diffstat (limited to 'bonus')
-rwxr-xr-xbonus/trans.cgi32
1 files changed, 20 insertions, 12 deletions
diff --git a/bonus/trans.cgi b/bonus/trans.cgi
index bfaca713..693dec2a 100755
--- a/bonus/trans.cgi
+++ b/bonus/trans.cgi
@@ -1,16 +1,24 @@
 #!/bin/sh
 # Needs https://github.com/soimort/translate-shell to work.
 # Usage: cgi-bin:trans.cgi?word
+# You can also set it as a keybinding (in config.toml):
+#
+# [page]
+# gT = '''
+# async () => {
+#   if (!pager.currentSelection) {
+#     pager.alert("No selection to translate.");
+#     return;
+#   }
+#   const text = await pager.getSelectionText(pager.currentSelection);
+#   pager.cursorToggleSelection();
+#   pager.load(`cgi-bin:trans.cgi?${encodeURIComponent(text)}\n`);
+# }
+# '''
 
-decode() {
-	# URL-decode the string passed as the first parameter
-	printf '%s\n' "$1" | \
-		sed 's/+/ /g;s/%/\\x/g' | \
-		xargs -0 printf "%b"
-}
-
-# QUERY_STRING is URL-encoded. We decode it using the decode() function.
-TEXT="$(decode "$QUERY_STRING")"
+# QUERY_STRING is URL-encoded. We decode it using the urldec utility provided
+# by Chawan.
+TEXT=$(printf '%s\n' "$QUERY_STRING" | "$CHA_LIBEXEC_DIR"/urldec)
 
 # Write a Content-Type HTTP header. The `trans' command outputs plain text,
 # so we use text/plain.
@@ -21,10 +29,10 @@ printf 'Content-Type: text/plain\n'
 printf '\n'
 
 # Check if the `trans' program exists, and if not, die.
-type trans >/dev/null || {
-	printf "ERROR: translator not found"
+if ! type trans >/dev/null
+then	printf "ERROR: translator not found"
 	exit 1
-}
+fi
 
 # Call the `trans' program. It writes its output to standard out, which
 # Chawan's local CGI will read in as the content body.