-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvocab.go
350 lines (237 loc) · 9.32 KB
/
vocab.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"strings"
"strconv"
"bytes"
"text/template"
//"github.com/gojp/nihongo/lib/dictionary"
"github.com/therecipe/qt/core"
"github.com/therecipe/qt/gui"
"github.com/therecipe/qt/widgets"
)
type css struct {
Fontsize int
Fontfamily string
Color string
Lineheight int
}
type span struct {
Top css
Expression css
Reading css
Glossary css
}
var theme = span{
Top: css{20, "Meiryo", "black", 12},
Expression: css{18, "Meiryo", "red", 10},
Reading: css{18, "Meiryo", "green", 10},
Glossary: css{18, "Meiryo", "blue", 10}}
type Entry struct {
Word string `json:"word"`
Furigana string `json:"furigana"`
Definition string `json:"definition"`
Common bool `json:"common,omitempty"`
}
type VocabDoc struct {
*widgets.QTextBrowser
vocabDefs []string //entry ?
}
func NewVocabDocument(parent widgets.QWidget_ITF) *VocabDoc {
var vd = &VocabDoc{
QTextBrowser: widgets.NewQTextBrowser(parent),
}
var txt = buildDefHeader()
vd.SetHtml(txt)
vd.SetAcceptDrops(false)
vd.SetOpenLinks(false)
var font = gui.NewQFont2("Meiryo", 14, 2, false)
vd.SetFont(font)
vd.ConnectAnchorClicked(vd.anchorclicked)
return vd
}
func (vd *VocabDoc) anchorclicked(link *core.QUrl) {
splt := strings.Split(link.ToString(core.QUrl__None),`:`)
command := splt[0]
index,_ := strconv.Atoi(splt[1])
println("anchorclicked",command, index)
vd.executeVocabCommand(command, index)
}
func(vd *VocabDoc) executeVocabCommand(cmd string, idx int) {
//if idx >= len(vd.vocabDefs) { return }
def := vd.vocabDefs[idx]
if cmd == `copyvocabdef` {
html := buildDefHeader() + def + buildDefFooter()
linedefs.SetHtml(html)
}
}
func buildDefHeader() string {
var txt = ` <html><head><style>
body { background-color: white }
span.top { font-size: {{.Top.Fontsize}}px; font-family: '{{.Top.Fontfamily}}'; color: {{.Top.Color}}; line-height: {{.Top.Lineheight}}px }
span.expression { font-size: {{.Expression.Fontsize}}px; font-family: '{{.Expression.Fontfamily}}'; color: {{.Expression.Color}}; line-height: {{.Expression.Lineheight}}px }
span.reading { font-size: {{.Reading.Fontsize}}px; font-family: '{{.Reading.Fontfamily}}'; color: {{.Reading.Color}}; line-height: {{.Reading.Lineheight}}px }
span.glossary { font-size: {{.Glossary.Fontsize}}px; font-family: '{{.Glossary.Fontfamily}}'; color: {{.Glossary.Color}}; line-height: {{.Glossary.Lineheight}}px }
</style></head><body>`
buf := new(bytes.Buffer)
tmpl, err := template.New("txt").Parse(txt)
if err != nil {
panic(err)
}
err = tmpl.Execute(buf, theme)
if err != nil {
panic(err)
}
return buf.String()
}
func buildDefFooter() string { return "</body></html>" }
func buildEntry(e string) Entry {
println("entry is: ", e)
entry := strings.Split(e, `|`)
println("entry[0] >> ", entry[0])
k,_ := strconv.Atoi(entry[0][0:1])
r,_ := strconv.Atoi(entry[0][1:2])
p,_ := strconv.Atoi(entry[0][2:3])
g,_ := strconv.Atoi(entry[0][3:4])
if len(entry[0]) == 5 { g,_ = strconv.Atoi(entry[0][3:5]) }
var ks,rs,ps,gs string
for i, v := range entry {
if i !=0 {
if k != 0 {
ks += `; ` + v
k -= 1
continue
}
if k == 0 && r != 0 {
rs += `; ` + v
r -= 1
continue
}
if k == 0 && r == 0 && p != 0 {
ps += `; ` + v
p -= 1
continue
}
if k == 0 && r == 0 && p == 0 && g !=0 {
gs += `; ` + v
g -= 1
continue
}
}
}
if len(ks) > 2 { ks = ks[2:] }
if len(rs) > 2 { rs = rs[2:] }
if len(ps) > 2 { ps = ps[2:] }
if len(gs) > 2 { gs = gs[2:] }
return Entry{
Word: ks,
Furigana: rs,
Definition: ps + ` | ` + gs,
//Common: ps,
}
}
func (vd *VocabDoc) BuildVocabDefs(contentSample string, result uint64 ) {
entries := []Entry{}
println("result: ", result)
if result >= 100000000 {
result = result - 100000000
de := dictmap[result]
println("dictmap= ", de)
des := strings.Split(de, `|`)
for _, e := range des {
re,_ := strconv.Atoi(e)
entries = append(entries, buildEntry(dict[re]))
}
} else {
entries = append(entries, buildEntry(dict[int(result)]))
}
var html string
vd.vocabDefs = nil
for i, e := range entries {
h := vd.buildVocabDef(e, strconv.Itoa(i))
html += h
vd.vocabDefs = append(vd.vocabDefs, h)
}
top := strings.Replace(`<span class = "top">[{0}]<br/></span>`, `{0}`, contentSample, -1)
html = buildDefHeader() + top + html + buildDefFooter()
vd.SetHtml(html)
}
func (vd *VocabDoc) buildVocabDef(e Entry, i string) string {
slink := `<a href = "copyVocabDef:{0}"><img src = "icons/add.png" align = "right" /></a>` // height="24" width="24"
sreading := `<span class = "reading">[{0}]</span>` // <br/>
sexpression := `<span class = "expression">[{0}]<br/></span>`
sglossary := `<span class = "glossary">{0}</span>` //<br/>
link := strings.Replace(slink, `{0}`, i, -1)
reading := strings.Replace(sreading, `{0}`, e.Furigana, -1)
expression := strings.Replace(sexpression, `{0}`, e.Word, -1)
glossary := strings.Replace(sglossary, `{0}`, e.Definition, -1)
htmlbreak := `<hr width="90%" align="left" clear="all">` //`<span><hr width="75%"></span>` //`<br clear = "all"/>`
//html := link + reading + expression + glossary + htmlbreak
html := `<div>` + link + reading + expression + glossary + htmlbreak + `</div>`
return html
}
// def buildDefHeader(self):
// return u"""
// <html><head><style>
// body {{ background-color: {0} }}
// span.expression {{ font-size: {1}px; font-family: '{2}'; color: {3}; line-height: {10}px }}
// span.reading {{ font-size: {4}px; font-family: '{5}'; color: {6}; line-height: {11}px }}
// span.glossary {{ font-size: {7}px; font-family: '{8}'; color: {9}; line-height: {12}px }}
// </style></head><body>""".format(self.bg, self.efs, self.eft, self.efg, self.rfs, self.rft, self.rfg, self.gfs, self.gft, self.gfg, self.elh, self.rlh, self.glh) #+ html + "</body></html>"
// def buildDefFooter(self):
// return '</body></html>'
// def buildEmpty(self):
// return u"""
// <p>No definitions to display.</p>
// <p>Mouse over text with the <em>middle mouse button</em> or <em>shift key</em> pressed to search.</p>
// <p>You can also also input terms in the search box below."""
// def buildVocabDef(self, definition, index, query):
// reading = unicode()
// if definition['reading']:
// reading = u'<span class = "reading">[{0}]<br/></span>'.format(definition['reading'])
// rules = unicode()
// if len(definition['rules']) > 0:
// rules = ' < '.join(definition['rules'])
// rules = '<span class = "rules">({0})<br/></span>'.format(rules)
// links = '<a href = "copyVocabDef:{0}"><img src = "img/icon_add_expression.png" align = "right"/></a>'.format(index)
// if query is not None:
// if query('vocab', yomi_base.reader_util.markupVocabExp(definition)):
// links += '<a href = "addVocabExp:{0}"><img src = "://img/img/icon_add_expression.png" align = "right"/></a>'.format(index)
// if query('vocab', yomi_base.reader_util.markupVocabReading(definition)):
// links += '<a href = "addVocabReading:{0}"><img src = "://img/img/icon_add_reading.png" align = "right"/></a>'.format(index)
// html = u"""
// <span class = "links">{0}</span>
// <span class = "expression">{1}<br/></span>
// {2}
// <span class = "glossary">{3}<br/></span>
// {4}
// <br clear = "all"/>""".format(links, definition['expression'], reading, definition['glossary'], rules)
// #print html
// return html
// def buildVocabDefs(self, definitions, query):
// html = self.buildDefHeader()
// if len(definitions) > 0:
// for i, definition in enumerate(definitions):
// html += self.buildVocabDef(definition, i, query)
// else:
// html += self.buildEmpty()
// return html + self.buildDefFooter()
// def buildKanjiDef(self, definition, index, query):
// links = '<a href = "copyKanjiDef:{0}"><img src = "://img/img/icon_copy_definition.png" align = "right"/></a>'.format(index)
// if query is not None and query('kanji', yomi_base.reader_util.markupKanji(definition)):
// links += '<a href = "addKanji:{0}"><img src = "://img/img/icon_add_expression.png" align = "right"/></a>'.format(index)
// readings = ', '.join([definition['kunyomi'], definition['onyomi']])
// html = u"""
// <span class = "links">{0}</span>
// <span class = "expression">{1}<br/></span>
// <span class = "reading">[{2}]<br/></span>
// <span class = "glossary">{3}<br/></span>
// <br clear = "all"/>""".format(links, definition['character'], readings, definition['glossary'])
// return html
// def buildKanjiDefs(self, definitions, query):
// html = self.buildDefHeader()
// if len(definitions) > 0:
// for i, definition in enumerate(definitions):
// html += self.buildKanjiDef(definition, i, query)
// else:
// html += self.buildEmpty()
// return html + self.buildDefFooter()