| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: UTF-8 -*- |
| 3 |
|
| 4 |
import cgi, sys, codecs, time, random, os |
| 5 |
|
| 6 |
from ludevit_trans.translator import Translator |
| 7 |
from ludevit_trans import tables_ludevit |
| 8 |
|
| 9 |
|
| 10 |
logdir='/var/log/ludevit/' |
| 11 |
|
| 12 |
|
| 13 |
def writelog(text): |
| 14 |
"write text to a logfile, text is a plain 8-bit string, not unicode" |
| 15 |
if not logdir or not text: |
| 16 |
return |
| 17 |
remote_addr = os.environ.get('REMOTE_ADDR', '') |
| 18 |
try: |
| 19 |
if not os.path.exists(logdir): |
| 20 |
os.makedirs(logdir) |
| 21 |
fname = time.strftime('%Y%m%d_%H%M%S', time.gmtime())+'_%0x'%random.randint(0,0xff) |
| 22 |
fname = os.path.join(logdir, fname) |
| 23 |
f = file(fname, 'w') |
| 24 |
f.write(remote_addr+'\n') |
| 25 |
f.write(text) |
| 26 |
f.close() |
| 27 |
except (IOError, OSError): |
| 28 |
pass |
| 29 |
return |
| 30 |
|
| 31 |
def header(): |
| 32 |
r = u'''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
| 33 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| 34 |
<html xmlns="http://www.w3.org/1999/xhtml" lang="sk" xml:lang="sk"> |
| 35 |
<head> |
| 36 |
<title>ludevít</title> |
| 37 |
<meta http-equiv="Content-Type" |
| 38 |
content="text/html; charset=utf-8" /> |
| 39 |
<link href="/pics/favicon-stur.ico" rel="shortcut icon" /> |
| 40 |
|
| 41 |
</head> |
| 42 |
|
| 43 |
<body> |
| 44 |
''' |
| 45 |
return r |
| 46 |
|
| 47 |
def footer(): |
| 48 |
return u''' |
| 49 |
<p> |
| 50 |
Vaše názori posjelajťe na adresu <strong>ludevit @ juls.savba.sk</strong> |
| 51 |
</p> |
| 52 |
</body> |
| 53 |
</html> |
| 54 |
''' |
| 55 |
|
| 56 |
def form(text='', nfkd='none'): |
| 57 |
text = cgi.escape(text) |
| 58 |
r = u''' |
| 59 |
<form action="" method="post"> |
| 60 |
<textarea name="text" cols="70" rows="20" wrap="soft">%s</textarea> |
| 61 |
''' % text |
| 62 |
if nfkd<>'none': |
| 63 |
checked = 'checked' |
| 64 |
else: |
| 65 |
checked = '' |
| 66 |
r += u''' |
| 67 |
<br /> |
| 68 |
<input type="checkbox" name="nfkd" %s />Vísledok zobraz v NFKD normalizácii |
| 69 |
<a href="/ludevit/why_nfkd.html">(prečo bi som mau?)</a> |
| 70 |
''' % checked |
| 71 |
|
| 72 |
r += u''' |
| 73 |
<br /> |
| 74 |
<input type="submit" value="prelož" /> |
| 75 |
</form> |
| 76 |
''' |
| 77 |
return r |
| 78 |
|
| 79 |
def format_translation(text): |
| 80 |
if not text: |
| 81 |
return '' |
| 82 |
r = '''<div> |
| 83 |
%s |
| 84 |
</div><hr />''' % cgi.escape(text).replace('\n', '<br />') |
| 85 |
return r |
| 86 |
|
| 87 |
sys.stdout = codecs.getwriter('utf-8')(sys.stdout) |
| 88 |
|
| 89 |
print "Content-type: text/html" # HTML is following |
| 90 |
print # blank line, end of headers |
| 91 |
|
| 92 |
|
| 93 |
print header() |
| 94 |
|
| 95 |
f = cgi.FieldStorage() |
| 96 |
|
| 97 |
text = f.getfirst("text", "kokot") |
| 98 |
do_nfkd = f.getfirst("nfkd", False) |
| 99 |
nfkd = 'none' |
| 100 |
if do_nfkd: |
| 101 |
nfkd = 'hack' |
| 102 |
|
| 103 |
if text: |
| 104 |
writelog(text) |
| 105 |
try: |
| 106 |
text = unicode(text, 'utf-8')[:40000] # safeguard |
| 107 |
translator = Translator(tables_ludevit.table_voc, tables_ludevit.table_ort, tables_ludevit.postprocess) |
| 108 |
t = translator.translate_text(text, nfkd) |
| 109 |
except UnicodeDecodeError: |
| 110 |
t = u''' |
| 111 |
Text ňebou v UTF-8 koduvaní. Možno váš browser ňepodporuje |
| 112 |
UTF-8. Všetki modernje browseri toto podporujú, skúsťe novú versiu. Ibažebi |
| 113 |
sťe k stránke ňepristupovali z običajnjeho počítača, ale z dajakjeho |
| 114 |
inšjeho zarjaďeňja, napriklad z PDA, kďe browseri často UTF-8 aňi žjadne |
| 115 |
Slovenskje písmeni ňepodporujú. To je nám lúto. |
| 116 |
''' |
| 117 |
print format_translation(t) |
| 118 |
else: |
| 119 |
t = u''' |
| 120 |
Toto je automatickí prekladač textu zo spisovnej Slovenčini do štúrovskej. |
| 121 |
Napíšťe krátki text v spisovnom nárečí so správnou diakritikou a klikňiťe na «prelož». |
| 122 |
''' |
| 123 |
print format_translation(t) |
| 124 |
|
| 125 |
print form(text='', nfkd=nfkd) |
| 126 |
|
| 127 |
print footer() |
| 128 |
|