Upgrade to kiln 0.2

This commit is contained in:
2022-01-03 15:56:24 +00:00
parent f6a2f62c92
commit 04ef9421b8
49 changed files with 100 additions and 35 deletions

13
static/cgi-bin/debug Executable file
View File

@@ -0,0 +1,13 @@
#!/bin/bash
if [ "$QUERY_STRING" = "" ]; then
echo -ne "10 Enter a query string\r\n"
exit 0
fi
echo -ne "20 text/plain\r\n"
echo -ne "SCRIPT_NAME: $SCRIPT_NAME\r\n"
echo -ne "PATH_INFO: $PATH_INFO\r\n"
echo -ne "QUERY_STRING: $QUERY_STRING\r\n"

87
static/cgi-bin/translate Executable file
View File

@@ -0,0 +1,87 @@
#!/usr/bin/ruby --disable-all
# A cgi-bin script that translates between languages. It relies on apertium
# being installed on the machine.
#
# CGI works with environment variables. Here are the ones that matter:
#
# PATH_INFO: /<from>/<to>
# QUERY_STRING: Some%20text%20to%20translate
#
# We expect to be called like: /cgi-bin/translate/eng/spa?Food
#
# If we don't have two languages, make it a Not Found error.
# If we don't have a query string to translate, ask for one.
require 'cgi'
LANGUAGE_PAIRS = %w[en-es es-en]
Encoding.default_internal = Encoding::UTF_8
Encoding.default_external = Encoding::UTF_8
class Object
def blank?
nil? || self&.empty?
end
end
def respond!(code, meta, body = nil)
STDOUT.print("#{code} #{meta}\r\n")
STDOUT.print(body) unless body.blank?
exit 0
end
def ask!(prompt)
respond!(10, prompt)
end
def ok!(meta, body = nil)
respond!(20, meta, body)
end
def temp_fail!(meta = 'Temporary Failure')
respond!(40, meta)
end
def not_found!(meta = 'Not Found')
respond!(51, meta)
end
def extract_langpair(path)
return if path.blank? || !path.start_with?('/')
_, src, dst, *rest = path.split('/')
return unless rest.empty?
pair = [src, dst].join('-')
return unless LANGUAGE_PAIRS.include?(pair)
pair
end
def extract_text(query)
return "" if query.blank?
CGI.unescape(query)
end
LANG_PAIR = extract_langpair(ENV['PATH_INFO'])
not_found! if LANG_PAIR.blank?
TRANSLATE = extract_text(ENV['QUERY_STRING'])
# TODO: we could detect a URL and translate the whole page sometime, perhaps
ask!('Enter text to translate') if TRANSLATE.blank?
require 'open3'
translation, status = Open3.capture2({'LC_ALL' => 'C.UTF-8'}, 'apertium', '-un', LANG_PAIR, stdin_data: TRANSLATE, binmode: true)
translation = translation.force_encoding("UTF-8")
temp_fail!("Couldn't get translation") unless status.success?
ok!('text/plain; charset="utf-8"', translation)