blob: 693dec2a25acca4bd675ff138f9c5577b4c2913a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/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`);
# }
# '''
# 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.
printf 'Content-Type: text/plain\n'
# We must write a newline here, so Chawan knows that all headers have been
# written and incoming data from now on belongs to the body.
printf '\n'
# Check if the `trans' program exists, and if not, die.
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.
trans -- "$TEXT"
|