-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
410 lines (359 loc) · 8.42 KB
/
template.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
package app
import (
"fmt"
"time"
"strconv"
"reflect"
"net/url"
"html/template"
"golang.org/x/net/html"
"github.com/h2object/h2object/page"
h2object "github.com/h2object/h2object/template"
)
//! page
func t_click(uri string) int {
ctx := get_context()
if ctx != nil {
if count, err := ctx.get_click(uri); err == nil {
return int(count)
}
}
return 0
}
func t_page(uri string) *page.Page {
ctx := get_context()
if ctx != nil {
return ctx.get_page(uri)
}
return nil
}
func t_title(pg *page.Page) string {
return pg.Title()
}
func t_published_datetime(pg *page.Page) time.Time {
return pg.PublishedDatetime()
}
func t_summary(max int, pg *page.Page) string {
return pg.Summary(max)
}
func t_uri(pg *page.Page) string {
if pg != nil {
return pg.Meta("uri").(string)
}
return ""
}
func t_meta(key string, pg *page.Page) interface{} {
if pg != nil {
return pg.Meta(key)
}
return nil
}
func t_meta_default(key string, dft interface{}, pg *page.Page) interface{} {
if pg != nil {
if v := pg.Meta(key); v != nil {
return v
}
}
return dft
}
func t_modify_datetime(pg *page.Page) time.Time {
if pg != nil {
return pg.ModifyDatetime()
}
return time.Time{}
}
func t_markdown(pg *page.Page) template.HTML {
if pg != nil {
return template.HTML(pg.Markdown())
}
return template.HTML("")
}
func t_tag(name string, pg *page.Page) []*html.Node {
if pg != nil {
return pg.TagNameAll(name)
}
return nil
}
func t_tag_id(id string, pg *page.Page) []*html.Node {
if pg != nil {
return pg.TagIDAll(id)
}
return nil
}
func t_tag_class(class string, pg *page.Page) []*html.Node {
if pg != nil {
return pg.TagClassAll(class)
}
return nil
}
func t_node_text(node *html.Node) string {
if node != nil {
return page.NodeText(node)
}
return ""
}
func t_node_attr(attr string, node *html.Node) string {
if node != nil {
return page.NodeAttribute(node, attr)
}
return ""
}
func t_node_html(node *html.Node) template.HTML {
if node != nil {
return page.NodeHtml(node)
}
return template.HTML("")
}
//! pages
func t_clicks(uri string, max int, suffix string) []Click {
ctx := get_context()
if ctx != nil {
if clicks, err := ctx.clicks_desc(uri, max, suffix); err == nil {
return clicks
}
return nil
}
return nil
}
func t_pages(uri string) *Pages {
ctx := get_context()
if ctx != nil {
return ctx.get_pages(uri)
}
return nil
}
func t_nested(flag bool, pgs *Pages) *Pages {
if pgs != nil {
pgs.Nested(flag)
}
return pgs
}
func t_query(json string, pgs *Pages) *Pages {
if pgs != nil {
pgs.Query(json)
}
return pgs
}
func t_query_regexp(field string, regexp string, pgs *Pages) *Pages {
if pgs != nil {
pgs.QueryRegexp(field, regexp)
}
return pgs
}
func t_query_string(field string, str string, pgs *Pages) *Pages {
if pgs != nil {
pgs.QueryString(field, str)
}
return pgs
}
func t_query_term(field string, term string, pgs *Pages) *Pages {
if pgs != nil {
pgs.QueryTerm(field, term)
}
return pgs
}
func t_query_term_prefix(field string, prefix string, pgs *Pages) *Pages {
if pgs != nil {
pgs.QueryTermPrefix(field, prefix)
}
return pgs
}
func t_query_match(field string, match string, pgs *Pages) *Pages {
if pgs != nil {
pgs.QueryMatch(field, match)
}
return pgs
}
func t_query_match_phrase(field string, phrase string, pgs *Pages) *Pages {
if pgs != nil {
pgs.QueryMatchPhrase(field, phrase)
}
return pgs
}
func t_query_num_range(field string, start, end interface{}, pgs *Pages) *Pages {
if pgs != nil {
pgs.QueryNumberRange(field, start, end)
}
return pgs
}
func t_query_num_range_inclusive(field string, start, end interface{}, startInc bool, endInc bool, pgs *Pages) *Pages {
if pgs != nil {
pgs.QueryNumberRangeInclusive(field, start, end, startInc, endInc)
}
return pgs
}
func t_query_date_range(field, start, end string, pgs *Pages) *Pages {
if pgs != nil {
pgs.QueryDatetimeRange(field, start, end)
}
return pgs
}
func t_query_date_range_inclusive(field, start, end string, startInc bool, endInc bool, pgs *Pages) *Pages {
if pgs != nil {
pgs.QueryDatetimeRangeInclusive(field, start, end, startInc, endInc)
}
return pgs
}
func t_order_by(field string, asc bool, pgs *Pages) *Pages {
if pgs != nil {
pgs.OrderBy(field, asc)
}
return pgs
}
func t_pagination(url string, size int, pgs *Pages) []*PaginationItem{
if pgs != nil {
return pgs.Pagination(url, size)
}
return nil
}
func t_total(pgs *Pages) int {
if pgs != nil {
return pgs.Total()
}
return 0
}
func t_limit(offset int, size int, pgs *Pages) []*page.Page {
if pgs != nil {
return pgs.Limit(int64(offset), int64(size))
}
return nil
}
func t_limit_by_page(page int, size int, pgs *Pages) []*page.Page {
if pgs != nil {
return pgs.Limit(int64(page*size), int64(size))
}
return nil
}
func t_all(pgs *Pages) []*page.Page {
if pgs != nil {
return pgs.All()
}
return nil
}
func t_page_no(item *PaginationItem) int {
if item != nil {
return item.PageNo
}
return 0
}
func t_page_size(item *PaginationItem) int {
if item != nil {
return item.PageSize
}
return 0
}
func t_page_link(item *PaginationItem) string {
if item != nil {
return item.PageLink
}
return ""
}
func t_path(u *url.URL) string {
if u != nil {
return u.Path
}
return ""
}
func t_qrcode(size int, u *url.URL) template.HTML {
ctx := get_context()
if ctx != nil {
if link, err := ctx.qrcode_link(u, size); err == nil {
return template.HTML(fmt.Sprintf(`<img src="%s" \>`, link))
}
}
return template.HTML("")
}
func t_param(key string, dft interface{}, u *url.URL) interface{} {
if u != nil {
val := u.Query().Get(key)
switch reflect.TypeOf(dft).Kind() {
case reflect.Bool:
if b, err := strconv.ParseBool(val); err == nil {
return b
}
case reflect.Int:
fallthrough
case reflect.Int8:
fallthrough
case reflect.Int16:
fallthrough
case reflect.Int32:
fallthrough
case reflect.Int64:
if i, err := strconv.ParseInt(val, 10, 64); err == nil {
return int(i)
}
case reflect.Uint:
fallthrough
case reflect.Uint8:
fallthrough
case reflect.Uint16:
fallthrough
case reflect.Uint32:
fallthrough
case reflect.Uint64:
if i, err := strconv.ParseUint(val, 10, 64); err == nil {
return int(i)
}
case reflect.Float32:
if f, err := strconv.ParseFloat(val, 32); err == nil {
return f
}
case reflect.Float64:
if f, err := strconv.ParseFloat(val, 64); err == nil {
return f
}
case reflect.String:
return val
}
}
return dft
}
func init() {
// url
h2object.Function("path", t_path)
h2object.Function("param", t_param)
h2object.Function("qrcode", t_qrcode)
// single page template method
h2object.Function("click", t_click)
h2object.Function("page", t_page)
h2object.Function("uri", t_uri)
h2object.Function("title", t_title)
h2object.Function("published_datetime", t_published_datetime)
h2object.Function("summary", t_summary)
h2object.Function("meta", t_meta)
h2object.Function("meta_default", t_meta_default)
h2object.Function("modify_datetime", t_modify_datetime)
h2object.Function("markdown", t_markdown)
h2object.Function("tag", t_tag)
h2object.Function("tag_id", t_tag_id)
h2object.Function("tag_class", t_tag_class)
h2object.Function("node_text", t_node_text)
h2object.Function("node_attr", t_node_attr)
h2object.Function("node_html", t_node_html)
// multi pages template method
h2object.Function("clicks", t_clicks)
h2object.Function("pages", t_pages)
h2object.Function("nested", t_nested)
h2object.Function("query", t_query)
h2object.Function("query_regexp", t_query_regexp)
h2object.Function("query_string", t_query_string)
h2object.Function("query_term", t_query_term)
h2object.Function("query_term_prefix", t_query_term_prefix)
h2object.Function("query_match", t_query_match)
h2object.Function("query_match_phrase", t_query_match_phrase)
h2object.Function("query_num_range", t_query_num_range)
h2object.Function("query_num_range_inclusive", t_query_num_range_inclusive)
h2object.Function("query_date_range", t_query_date_range)
h2object.Function("query_date_range_inclusive", t_query_date_range_inclusive)
h2object.Function("order_by", t_order_by)
h2object.Function("pagination", t_pagination)
h2object.Function("total", t_total)
h2object.Function("limit", t_limit)
h2object.Function("limit_by_page", t_limit_by_page)
h2object.Function("all", t_all)
// pagination
h2object.Function("page_no", t_page_no)
h2object.Function("page_size", t_page_size)
h2object.Function("page_link", t_page_link)
}