Switch to gemserv; add a couple of translation links

This commit is contained in:
2020-11-26 22:49:49 +00:00
parent e3a6e39ac8
commit 9c909f42c5
6 changed files with 117 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ default: build
build:
@rm -rf dst
@kiln
chmod a+x dst/cgi-bin/*
@tree dst
deploy: build

View File

@@ -13,7 +13,7 @@ The Gemini capsule is hosted here:
## Tools
=> https://sr.ht/~adnano/kiln Kiln static site generator
=> gemini://gem.limpet.net/agate/ Agate Gemini server
=> gemini://80h.dev/projects/gemserv/ Gemserv Gemini server
## Usage
@@ -23,4 +23,5 @@ Ensure Kiln is installed and run:
make
```
Feed the `dst/` directory to Agate.
Feed the dst/ directory to Gemserv. I used to use Agate, but now I need cgi-bin
support.

13
src/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"

91
src/cgi-bin/translate Executable file
View File

@@ -0,0 +1,91 @@
#!/usr/bin/env ruby
# A cgi-bin script that translates between languages, scraping apertium's API
# to do so
#
# It's kind of bad manners; I'll transition it to the offline tools once I've
# worked those out.
#
# 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'
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_languages(path)
return if path.blank? || !path.start_with?('/')
_, src, dst, *rest = path.split('/')
return unless rest.empty?
# return unless LANGUAGES.include?(src) && LANGUAGES.include?(dst)
[src, dst]
end
def extract_text(query)
return "" if query.blank?
CGI.unescape(query)
end
SRC_LANG, DST_LANG = extract_languages(ENV['PATH_INFO'])
not_found! if SRC_LANG.blank? || DST_LANG.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 'net/http'
require 'json'
uri = URI.parse('https://www.apertium.org/apy/translate')
uri.query = URI.encode_www_form(q: TRANSLATE, langpair: [SRC_LANG, DST_LANG].join('|'))
rsp = Net::HTTP.get(uri)
temp_fail!("Couldn't get translation (1)") unless rsp
begin
structure = JSON.parse(rsp)
rescue
temp_fail!("Couldn't get translation (2)")
end
temp_fail!("Couldn't get transaltion (3)") unless structure['responseStatus'] == 200
ok!('text/plain', structure.dig('responseData', 'translatedText'))

View File

@@ -7,4 +7,9 @@
=> me@ur.gs.gpg.asc GPG key
## Translation service
=> /cgi-bin/translate/eng/spa English -> Español
=> /cgi-bin/translate/spa/eng Español -> English
## About Me

View File

@@ -30,3 +30,7 @@ Disallow:
# don't do that
User-Agent: webproxy
Disallow:
# Here be dragons
User-Agent: *
Disallow: /cgi-bin/