-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
266 lines (242 loc) · 6.68 KB
/
main.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright 2016 Mathieu Lonjaret
// Given a (pseudo-)phonetic russian input, phoru outputs (cyrillic) russian.
package main
import (
"flag"
"fmt"
"html/template"
"log"
"net/http"
"os"
"sort"
"strings"
"github.com/mpl/phoru"
"github.com/mpl/simpletls"
)
// TODO(mpl): make it use github.com/mpl/phoru.Translate
var (
flagHelp = flag.Bool("h", false, "show this help")
flagVerbose = flag.Bool("v", false, "verbose")
flagTLS = flag.Bool("tls", true, "Enable TLS for the http server mode.")
flagHttp = flag.String("http", "", "Run in http server mode, on the given address.")
)
func usage() {
fmt.Fprintf(os.Stderr, "example:\n\t echo privet mir | phoru\n")
fmt.Fprintf(os.Stderr, "\ntranslation tables:\n")
var sortedSingle, sortedDouble, sortedTriple []string
for k, _ := range phoru.Single {
sortedSingle = append(sortedSingle, k)
}
for k, _ := range phoru.Double {
sortedDouble = append(sortedDouble, k)
}
for k, _ := range phoru.Triple {
sortedTriple = append(sortedTriple, k)
}
sort.Strings(sortedSingle)
sort.Strings(sortedDouble)
sort.Strings(sortedTriple)
for _, v := range sortedSingle {
fmt.Fprintf(os.Stderr, "\t %v : %v\n", v, string(phoru.Single[v]))
}
for _, v := range sortedDouble {
fmt.Fprintf(os.Stderr, "\t %v : %v\n", v, string(phoru.Double[v]))
}
for _, v := range sortedTriple {
fmt.Fprintf(os.Stderr, "\t %v : %v\n", v, string(phoru.Triple[v]))
}
fmt.Fprintf(os.Stderr, "\nin server mode:\n\t phoru -http=:6060\n")
flag.PrintDefaults()
os.Exit(2)
}
/*
The Russian alphabet
А а Б б В в Г г Д д Е е Ё ё Ж ж З з И и Й й
К к Л л М м Н н О о П п Р р С с Т т У у Ф ф
Х х Ц ц Ч ч Ш ш Щ щ Ъ ъ Ы ы Ь ь Э э Ю ю Я я
*/
// TODO(mpl): use same pseudo-phonetics as goog translate maybe?
// TODO(mpl): greediness can break some cases. ex: with iy->й, and ya->я, when I
// want to write ия, I get йa instead. I need to rethink all bi and tri letter
// combinations. worst case scenario, I add delimiters (single quotes maybe?)
// around combinations. For now, changing iy into ï. I didn't hit any other
// breaking example so far, but we'll see when my vocabulary extends.
func main() {
flag.Usage = usage
flag.Parse()
if *flagHelp {
usage()
}
if *flagHttp != "" {
baseData = &Translation{
Single: phoru.Single,
Double: phoru.Double,
Triple: phoru.Triple,
}
tmpl = template.Must(template.New("root").Parse(HTML))
http.HandleFunc("/phoru/", makeHandler(apiHandler))
http.HandleFunc("/", makeHandler(rootHandler))
if *flagTLS {
listener, err := simpletls.Listen(*flagHttp)
if err != nil {
log.Fatal(err)
}
log.Fatal(http.Serve(listener, nil))
}
log.Fatal(http.ListenAndServe(*flagHttp, nil))
}
trans, err := phoru.Translate(os.Stdin)
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(os.Stdout, "%v", string(trans))
}
const idstring = "http://golang.org/pkg/http/#ListenAndServe"
var (
tmpl *template.Template
baseData *Translation
)
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
defer func() {
if e, ok := recover().(error); ok {
http.Error(w, e.Error(), http.StatusInternalServerError)
return
}
}()
title := r.URL.Path
w.Header().Set("Server", idstring)
fn(w, r, title)
}
}
type Translation struct {
Input string
Output string
IsPost bool // TODO(mpl): meh. tired. do better later.
Single map[string]rune
Double map[string]rune
Triple map[string]rune
}
func apiHandler(w http.ResponseWriter, r *http.Request, url string) {
if r.URL.Path != "/phoru/" {
http.Error(w, "not found", 404)
return
}
latin := r.FormValue("q")
if latin == "" {
if _, err := w.Write([]byte("")); err != nil {
log.Printf("error sending translation: %v", err)
}
return
}
cyril, err := phoru.Translate(strings.NewReader(latin))
if err != nil {
log.Printf("translation error: %v", err)
http.Error(w, "translation error", 500)
return
}
if _, err := w.Write([]byte(string(cyril))); err != nil {
log.Printf("error sending translation: %v", err)
}
}
func rootHandler(w http.ResponseWriter, r *http.Request, url string) {
if r.Method == "GET" {
if err := tmpl.Execute(w, baseData); err != nil {
log.Printf("template error: %v", err)
}
return
}
if r.Method != "POST" {
http.Error(w, "not a POST", http.StatusMethodNotAllowed)
return
}
input := r.FormValue("inputtext")
trans, err := phoru.Translate(strings.NewReader(input))
if err != nil {
log.Printf("translation error: %v", err)
http.Error(w, "translation error", 500)
return
}
data := &Translation{
Input: input,
Output: string(trans),
IsPost: true,
Single: phoru.Single,
Double: phoru.Double,
Triple: phoru.Triple,
}
if err := tmpl.Execute(w, data); err != nil {
log.Printf("template error: %v", err)
}
}
var HTML = `
<!DOCTYPE html>
<html>
<head>
<title>Phoru</title>
</head>
<body>
<h1>Translate from pseudo-phonetics Russian, into Cyrillic Russian.</h1>
<form action="/translate" method="POST" id="translateform" enctype="multipart/form-data">
{{if .IsPost}}
<textarea rows="5" cols="100" name="inputtext" form="translateform">{{.Input}}</textarea>
{{else}}
<textarea rows="5" cols="100" name="inputtext" form="translateform">Privet Mir!</textarea>
{{end}}
<input type="submit" id="textsubmit" value="Translate">
</form>
{{if .Output}}
<h2>Conversion result</h1>
<p>
<pre>{{.Output}}</pre>
</p>
{{end}}
<h2>Conversion table</h2>
<p>
<table style="border: 1px solid black; border-collapse: collapse">
<tr>
{{range $latin,$cyr := .Single}}
<td style="border: 1px solid black; padding: 7px; text-align: center">{{$latin}}</td>
{{end}}
</tr>
<tr>
{{range $latin,$cyr := .Single}}
<td style="border: 1px solid black; padding: 7px; text-align: center">{{printf "%c" $cyr}}</td>
{{end}}
</tr>
</table>
</p>
<p>
<table style="border: 1px solid black; border-collapse: collapse">
<tr>
{{range $latin,$cyr := .Double}}
<td style="border: 1px solid black; padding: 7px; text-align: center">{{$latin}}</td>
{{end}}
</tr>
<tr>
{{range $latin,$cyr := .Double}}
<td style="border: 1px solid black; padding: 7px; text-align: center">{{printf "%c" $cyr}}</td>
{{end}}
</tr>
</table>
</p>
<p>
<table style="border: 1px solid black; border-collapse: collapse">
<tr>
{{range $latin,$cyr := .Triple}}
<td style="border: 1px solid black; padding: 7px; text-align: center">{{$latin}}</td>
{{end}}
</tr>
<tr>
{{range $latin,$cyr := .Triple}}
<td style="border: 1px solid black; padding: 7px; text-align: center">{{printf "%c" $cyr}}</td>
{{end}}
</tr>
</table>
</p>
<p>
Source code at: <a href="https://github.com/mpl/phoru">mpl/phoru</a>
</p>
</body>
</html>
`