forked from kabukky/journey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlebars.go
865 lines (806 loc) · 30.2 KB
/
handlebars.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
package templates
import (
"bytes"
"github.com/kabukky/journey/conversion"
"github.com/kabukky/journey/database"
"github.com/kabukky/journey/filenames"
"github.com/kabukky/journey/structure"
"html"
"log"
"net/url"
"strconv"
"strings"
"time"
)
// Ghost always includes a link to jquery in it's footer func. Mimic this.
var jqueryCodeForFooter = []byte("<script src=\"" + filenames.JqueryFilename + "\"></script>")
// Helper fuctions
func nullFunc(helper *Helper, values *structure.RequestData) []byte {
log.Println("Warning: This helper is not implemented:", helper.Name)
return []byte{}
}
func paginationDotTotalFunc(helper *Helper, values *structure.RequestData) []byte {
if values.CurrentTemplate == 0 { // index
return []byte(strconv.FormatInt(values.Blog.PostCount, 10))
} else if values.CurrentTemplate == 3 { // author
count, err := database.RetrieveNumberOfPostsByUser(values.Posts[values.CurrentPostIndex].Author.Id)
if err != nil {
log.Println("couldn't get number of posts", err.Error())
return []byte{}
}
return []byte(strconv.FormatInt(count, 10))
} else if values.CurrentTemplate == 2 { // tag
count, err := database.RetrieveNumberOfPostsByTag(values.CurrentTag.Id)
if err != nil {
log.Println("couldn't get number of posts", err.Error())
return []byte{}
}
return []byte(strconv.FormatInt(count, 10))
}
return []byte{}
}
func pluralFunc(helper *Helper, values *structure.RequestData) []byte {
countString := string(helper.Arguments[0].Function(helper, values))
if countString == "" {
log.Println("Couldn't get count in plural helper")
return []byte{}
}
for _, argument := range helper.Arguments[1:] {
if countString == "0" && strings.HasPrefix(argument.Name, "empty") {
output := argument.Name[len("empty"):]
output = strings.Replace(output, "%", countString, -1)
return []byte(output)
} else if countString == "1" && strings.HasPrefix(argument.Name, "singular") {
output := argument.Name[len("singular"):]
output = strings.Replace(output, "%", countString, -1)
return []byte(output)
} else if countString != "0" && countString != "1" && strings.HasPrefix(argument.Name, "plural") {
output := argument.Name[len("plural"):]
output = strings.Replace(output, "%", countString, -1)
return []byte(output)
}
}
return []byte{}
}
func prevFunc(helper *Helper, values *structure.RequestData) []byte {
if values.CurrentIndexPage > 1 {
return []byte{1}
}
return []byte{}
}
func nextFunc(helper *Helper, values *structure.RequestData) []byte {
var count int64
var err error
if values.CurrentTemplate == 0 { // index
count = values.Blog.PostCount
} else if values.CurrentTemplate == 2 { // tag
count, err = database.RetrieveNumberOfPostsByTag(values.CurrentTag.Id)
if err != nil {
log.Println("couldn't get number of posts for tag", err.Error())
return []byte{}
}
} else if values.CurrentTemplate == 3 { // author
count, err = database.RetrieveNumberOfPostsByUser(values.Posts[values.CurrentPostIndex].Author.Id)
if err != nil {
log.Println("couldn't get number of posts for author", err.Error())
return []byte{}
}
}
maxPages := int64((float64(count) / float64(values.Blog.PostsPerPage)) + 0.5)
if int64(values.CurrentIndexPage) < maxPages {
return []byte{1}
}
return []byte{}
}
func pageFunc(helper *Helper, values *structure.RequestData) []byte {
return []byte(strconv.Itoa(values.CurrentIndexPage))
}
func pagesFunc(helper *Helper, values *structure.RequestData) []byte {
var count int64
var err error
if values.CurrentTemplate == 0 { // index
count = values.Blog.PostCount
} else if values.CurrentTemplate == 2 { // tag
count, err = database.RetrieveNumberOfPostsByTag(values.CurrentTag.Id)
if err != nil {
log.Println("couldn't get number of posts for tag", err.Error())
return []byte{}
}
} else if values.CurrentTemplate == 3 { // author
count, err = database.RetrieveNumberOfPostsByUser(values.Posts[values.CurrentPostIndex].Author.Id)
if err != nil {
log.Println("couldn't get number of posts for author", err.Error())
return []byte{}
}
}
maxPages := int64((float64(count) / float64(values.Blog.PostsPerPage)) + 0.5)
return []byte(strconv.FormatInt(maxPages, 10))
}
func page_urlFunc(helper *Helper, values *structure.RequestData) []byte {
if len(helper.Arguments) != 0 {
if helper.Arguments[0].Name == "prev" || helper.Arguments[0].Name == "pagination.prev" {
if values.CurrentIndexPage > 1 {
var buffer bytes.Buffer
if values.CurrentIndexPage == 2 {
if values.CurrentTemplate == 3 { // author
buffer.WriteString("/author/")
//TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
} else if values.CurrentTemplate == 2 { // tag
buffer.WriteString("/tag/")
//TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.CurrentTag.Slug)
}
buffer.WriteString("/")
} else {
if values.CurrentTemplate == 3 { // author
buffer.WriteString("/author/")
//TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
} else if values.CurrentTemplate == 2 { // tag
buffer.WriteString("/tag/")
//TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.CurrentTag.Slug)
}
buffer.WriteString("/page/")
buffer.WriteString(strconv.Itoa(values.CurrentIndexPage - 1))
buffer.WriteString("/")
}
return buffer.Bytes()
}
} else if helper.Arguments[0].Name == "next" || helper.Arguments[0].Name == "pagination.next" {
var count int64
var err error
if values.CurrentTemplate == 0 { // index
count = values.Blog.PostCount
} else if values.CurrentTemplate == 2 { // tag
count, err = database.RetrieveNumberOfPostsByTag(values.CurrentTag.Id)
if err != nil {
log.Println("couldn't get number of posts for tag", err.Error())
return []byte{}
}
} else if values.CurrentTemplate == 3 { // author
count, err = database.RetrieveNumberOfPostsByUser(values.Posts[values.CurrentPostIndex].Author.Id)
if err != nil {
log.Println("couldn't get number of posts for author", err.Error())
return []byte{}
}
}
maxPages := int64((float64(count) / float64(values.Blog.PostsPerPage)) + 0.5)
if int64(values.CurrentIndexPage) < maxPages {
var buffer bytes.Buffer
if values.CurrentTemplate == 3 { // author
buffer.WriteString("/author/")
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
} else if values.CurrentTemplate == 2 { // tag
buffer.WriteString("/tag/")
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.CurrentTag.Slug)
}
buffer.WriteString("/page/")
buffer.WriteString(strconv.Itoa(values.CurrentIndexPage + 1))
buffer.WriteString("/")
return buffer.Bytes()
}
}
}
return []byte{}
}
func extendFunc(helper *Helper, values *structure.RequestData) []byte {
if len(helper.Arguments) != 0 {
return []byte(helper.Arguments[0].Name)
}
return []byte{}
}
func featuredFunc(helper *Helper, values *structure.RequestData) []byte {
if values.Posts[values.CurrentPostIndex].IsFeatured {
return []byte{1}
}
return []byte{}
}
func body_classFunc(helper *Helper, values *structure.RequestData) []byte {
if values.CurrentTemplate == 1 { // post
// TODO: is there anything else that needs to get output here?
var buffer bytes.Buffer
buffer.WriteString("post-template")
// If page
if values.Posts[values.CurrentPostIndex].IsPage {
buffer.WriteString(" page-template page")
}
for _, tag := range values.Posts[values.CurrentPostIndex].Tags {
buffer.WriteString(" tag-")
buffer.WriteString(tag.Slug)
}
return buffer.Bytes()
} else if values.CurrentTemplate == 0 { // index
if values.CurrentIndexPage == 1 {
return []byte("home-template")
} else {
return []byte("paged archive-template")
}
} else if values.CurrentTemplate == 3 { // author
var buffer bytes.Buffer
buffer.WriteString("author-template author-")
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
if values.CurrentIndexPage > 1 {
buffer.WriteString(" paged archive-template")
}
return buffer.Bytes()
} else if values.CurrentTemplate == 2 { // tag
var buffer bytes.Buffer
buffer.WriteString("tag-template tag-")
buffer.WriteString(values.CurrentTag.Slug)
if values.CurrentIndexPage > 1 {
buffer.WriteString(" paged archive-template")
}
return buffer.Bytes()
}
// TODO: Delete this. Probably not needed.
return []byte("post-template")
}
func ghost_headFunc(helper *Helper, values *structure.RequestData) []byte {
// TODO: Implement
return []byte{}
}
func ghost_footFunc(helper *Helper, values *structure.RequestData) []byte {
// TODO: This seems to just output a jquery link in ghost. Keep for compatibility?
return jqueryCodeForFooter
}
func meta_titleFunc(helper *Helper, values *structure.RequestData) []byte {
if values.CurrentTemplate == 1 { // post or page
return evaluateEscape(values.Posts[values.CurrentPostIndex].Title, helper.Unescaped)
} else if values.CurrentTemplate == 3 { // author
var buffer bytes.Buffer
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.Write(values.Posts[values.CurrentPostIndex].Author.Name)
buffer.WriteString(" - ")
buffer.Write(values.Blog.Title)
return evaluateEscape(buffer.Bytes(), helper.Unescaped)
} else if values.CurrentTemplate == 2 { // tag
var buffer bytes.Buffer
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.Write(values.CurrentTag.Name)
buffer.WriteString(" - ")
buffer.Write(values.Blog.Title)
return evaluateEscape(buffer.Bytes(), helper.Unescaped)
}
// index
return evaluateEscape(values.Blog.Title, helper.Unescaped)
}
func meta_descriptionFunc(helper *Helper, values *structure.RequestData) []byte {
// TODO: Finish this
if values.CurrentTemplate != 1 { // not post
return evaluateEscape(values.Blog.Description, helper.Unescaped)
}
// Nothing on post yet
return []byte{}
}
func bodyFunc(helper *Helper, values *structure.RequestData) []byte {
return helper.Block
}
func insertFunc(helper *Helper, values *structure.RequestData) []byte {
if len(helper.Arguments) != 0 {
if templateHelper, ok := compiledTemplates.m[helper.Arguments[0].Name]; ok {
return executeHelper(templateHelper, values, values.CurrentHelperContext)
}
}
return []byte{}
}
func encodeFunc(helper *Helper, values *structure.RequestData) []byte {
if len(helper.Arguments) != 0 {
return []byte(url.QueryEscape(string(helper.Arguments[0].Function(&helper.Arguments[0], values))))
}
return []byte{}
}
func authorFunc(helper *Helper, values *structure.RequestData) []byte {
// Check if helper is block helper
if len(helper.Block) != 0 {
return executeHelper(helper, values, 3) // context = author
}
// Else return author.name
var buffer bytes.Buffer
buffer.WriteString("<a href=\"")
buffer.WriteString("/author/")
// TODO: Error handling if there i no Posts[values.CurrentPostIndex]
buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
buffer.WriteString("/\">")
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.Write(evaluateEscape(values.Posts[values.CurrentPostIndex].Author.Name, helper.Unescaped))
buffer.WriteString("</a>")
return buffer.Bytes()
}
func authorDotNameFunc(helper *Helper, values *structure.RequestData) []byte {
var buffer bytes.Buffer
buffer.WriteString("<a href=\"")
buffer.WriteString("/author/")
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
buffer.WriteString("\">")
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.Write(evaluateEscape(values.Posts[values.CurrentPostIndex].Author.Name, helper.Unescaped))
buffer.WriteString("</a>")
return buffer.Bytes()
}
func bioFunc(helper *Helper, values *structure.RequestData) []byte {
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
return evaluateEscape(values.Posts[values.CurrentPostIndex].Author.Bio, helper.Unescaped)
}
func emailFunc(helper *Helper, values *structure.RequestData) []byte {
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
return evaluateEscape(values.Posts[values.CurrentPostIndex].Author.Email, helper.Unescaped)
}
func websiteFunc(helper *Helper, values *structure.RequestData) []byte {
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
return evaluateEscape(values.Posts[values.CurrentPostIndex].Author.Website, helper.Unescaped)
}
func imageFunc(helper *Helper, values *structure.RequestData) []byte {
if values.CurrentHelperContext == 1 { // post
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
return evaluateEscape(values.Posts[values.CurrentPostIndex].Image, helper.Unescaped)
} else if values.CurrentHelperContext == 3 { // author
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
return evaluateEscape(values.Posts[values.CurrentPostIndex].Author.Image, helper.Unescaped)
}
return []byte{}
}
func authorDotImageFunc(helper *Helper, values *structure.RequestData) []byte {
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
return evaluateEscape(values.Posts[values.CurrentPostIndex].Author.Image, helper.Unescaped)
}
func coverFunc(helper *Helper, values *structure.RequestData) []byte {
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
return evaluateEscape(values.Posts[values.CurrentPostIndex].Author.Cover, helper.Unescaped)
}
func locationFunc(helper *Helper, values *structure.RequestData) []byte {
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
return evaluateEscape(values.Posts[values.CurrentPostIndex].Author.Location, helper.Unescaped)
}
func postFunc(helper *Helper, values *structure.RequestData) []byte {
return executeHelper(helper, values, 1) // context = post
}
func postsFunc(helper *Helper, values *structure.RequestData) []byte {
if len(values.Posts) > 0 {
return []byte{1}
}
return []byte{}
}
func tagsFunc(helper *Helper, values *structure.RequestData) []byte {
if len(values.Posts[values.CurrentPostIndex].Tags) > 0 {
separator := ", "
suffix := ""
prefix := ""
makeLink := true
if len(helper.Arguments) != 0 {
for _, argument := range helper.Arguments {
if strings.HasPrefix(argument.Name, "separator") {
separator = argument.Name[len("separator"):]
} else if strings.HasPrefix(argument.Name, "suffix") {
suffix = argument.Name[len("suffix"):]
} else if strings.HasPrefix(argument.Name, "prefix") {
prefix = argument.Name[len("prefix"):]
} else if strings.HasPrefix(argument.Name, "autolink") {
if argument.Name[len("autolink"):] == "false" {
makeLink = false
}
}
}
}
var buffer bytes.Buffer
if prefix != "" {
buffer.WriteString(prefix)
buffer.WriteString(" ")
}
for index, tag := range values.Posts[values.CurrentPostIndex].Tags {
if index != 0 {
buffer.WriteString(separator)
}
if makeLink {
buffer.WriteString("<a href=\"")
buffer.WriteString("/tag/")
buffer.WriteString(tag.Slug)
buffer.WriteString("/\">")
}
buffer.Write(evaluateEscape(tag.Name, helper.Unescaped))
if makeLink {
buffer.WriteString("</a>")
}
}
if suffix != "" {
buffer.WriteString(" ")
buffer.WriteString(suffix)
}
return buffer.Bytes()
}
return []byte{}
}
func post_classFunc(helper *Helper, values *structure.RequestData) []byte {
var buffer bytes.Buffer
buffer.WriteString("post")
if values.Posts[values.CurrentPostIndex].IsFeatured {
buffer.WriteString(" featured")
}
if values.Posts[values.CurrentPostIndex].IsPage {
buffer.WriteString(" page")
}
for _, tag := range values.Posts[values.CurrentPostIndex].Tags {
buffer.WriteString(" tag-")
buffer.WriteString(tag.Slug)
}
return evaluateEscape(buffer.Bytes(), helper.Unescaped)
}
func urlFunc(helper *Helper, values *structure.RequestData) []byte {
var buffer bytes.Buffer
if len(helper.Arguments) != 0 {
for _, argument := range helper.Arguments {
// If the "absolute argument was used, prepend the blog url
if strings.HasPrefix(argument.Name, "absolute") {
if argument.Name[len("absolute"):] == "true" {
buffer.Write(values.Blog.Url)
}
}
}
}
if values.CurrentHelperContext == 1 { // post
buffer.WriteString("/")
buffer.WriteString(values.Posts[values.CurrentPostIndex].Slug)
buffer.WriteString("/")
return evaluateEscape(buffer.Bytes(), helper.Unescaped)
} else if values.CurrentHelperContext == 3 { // author
buffer.WriteString("/author/")
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
buffer.WriteString("/")
return evaluateEscape(buffer.Bytes(), helper.Unescaped)
}
return []byte{}
}
func titleFunc(helper *Helper, values *structure.RequestData) []byte {
return evaluateEscape(values.Posts[values.CurrentPostIndex].Title, helper.Unescaped)
}
func contentFunc(helper *Helper, values *structure.RequestData) []byte {
// TODO: is content always unescaped? seems like it...
return values.Posts[values.CurrentPostIndex].Html
}
func excerptFunc(helper *Helper, values *structure.RequestData) []byte {
if values.CurrentHelperContext == 1 { // post
if len(helper.Arguments) != 0 {
if strings.HasPrefix(helper.Arguments[0].Name, "words") {
number, err := strconv.Atoi(helper.Arguments[0].Name[len("words"):])
if err == nil {
excerpt := conversion.StripTagsFromHtml(values.Posts[values.CurrentPostIndex].Html)
words := bytes.Fields(excerpt)
if len(words) < number {
return excerpt
}
return bytes.Join(words[:number], []byte(" "))
}
} else if strings.HasPrefix(helper.Arguments[0].Name, "characters") {
number, err := strconv.Atoi(helper.Arguments[0].Name[len("characters"):])
if err == nil {
// Use runes for UTF-8 support
runes := []rune(string(conversion.StripTagsFromHtml(values.Posts[values.CurrentPostIndex].Html)))
if len(runes) < number {
return []byte(string(runes))
}
return []byte(string(runes[:number]))
}
}
}
// Default to 50 words excerpt
excerpt := conversion.StripTagsFromHtml(values.Posts[values.CurrentPostIndex].Html)
words := bytes.Fields(excerpt)
if len(words) < 50 {
return excerpt
}
return bytes.Join(words[:50], []byte(" "))
}
return []byte{}
}
func dateFunc(helper *Helper, values *structure.RequestData) []byte {
showPublicationDate := false
timeFormat := "MMM Do, YYYY" // Default time format
// If in scope of a post, change default to published date
if values.CurrentHelperContext == 1 { // post
showPublicationDate = true
}
// Get the date
if len(helper.Arguments) != 0 {
for index, _ := range helper.Arguments {
if helper.Arguments[index].Name == "published_at" {
showPublicationDate = true
} else if strings.HasPrefix(helper.Arguments[index].Name, "timeago") {
if helper.Arguments[index].Name[len("timeago"):] == "true" {
// Compute time ago
return evaluateEscape(generateTimeAgo(&values.Posts[values.CurrentPostIndex].Date), helper.Unescaped)
}
} else if strings.HasPrefix(helper.Arguments[index].Name, "format") {
timeFormat = helper.Arguments[index].Name[len("format"):]
}
}
}
if showPublicationDate {
return evaluateEscape(formatDate(timeFormat, &values.Posts[values.CurrentPostIndex].Date), helper.Unescaped)
}
date := time.Now()
return evaluateEscape(formatDate(timeFormat, &date), helper.Unescaped)
}
func atFirstFunc(helper *Helper, values *structure.RequestData) []byte {
if values.CurrentHelperContext == 1 { // post
if values.CurrentPostIndex == 0 {
return []byte{1}
}
return []byte{}
}
if values.CurrentHelperContext == 2 { // tag
if values.CurrentTagIndex == 0 {
return []byte{1}
}
return []byte{}
}
return []byte{}
}
func atLastFunc(helper *Helper, values *structure.RequestData) []byte {
if values.CurrentHelperContext == 1 { // post
if values.CurrentPostIndex == (len(values.Posts) - 1) {
return []byte{1}
}
return []byte{}
}
if values.CurrentHelperContext == 2 { // tag
if values.CurrentTagIndex == (len(values.Posts[values.CurrentPostIndex].Tags) - 1) {
return []byte{1}
}
return []byte{}
}
return []byte{}
}
func atEvenFunc(helper *Helper, values *structure.RequestData) []byte {
if values.CurrentHelperContext == 1 { // post
// First post (index 0) needs to be odd
if values.CurrentPostIndex%2 == 1 {
return []byte{1}
}
return []byte{}
}
if values.CurrentHelperContext == 2 { // tag
// First tag (index 0) needs to be odd
if values.CurrentTagIndex%2 == 1 {
return []byte{1}
}
return []byte{}
}
return []byte{}
}
func atOddFunc(helper *Helper, values *structure.RequestData) []byte {
if values.CurrentHelperContext == 1 { // post
// First post (index 0) needs to be odd
if values.CurrentPostIndex%2 == 0 {
return []byte{1}
}
return []byte{}
}
if values.CurrentHelperContext == 2 { // tag
// First tag (index 0) needs to be odd
if values.CurrentTagIndex%2 == 0 {
return []byte{1}
}
return []byte{}
}
return []byte{}
}
func nameFunc(helper *Helper, values *structure.RequestData) []byte {
// If tag (commented out the code for generating a link. Ghost doesn't seem to do that.
if values.CurrentHelperContext == 2 { // tag
//var buffer bytes.Buffer
//buffer.WriteString("<a href=\"")
//buffer.WriteString("/tag/")
//buffer.WriteString(values.Posts[values.CurrentPostIndex].Tags[values.CurrentTagIndex].Slug)
//buffer.WriteString("/\">")
//buffer.Write(evaluateEscape([]byte(values.Posts[values.CurrentPostIndex].Tags[values.CurrentTagIndex].Name), helper.Unescaped))
//buffer.WriteString("</a>")
//return buffer.Bytes()
return evaluateEscape(values.Posts[values.CurrentPostIndex].Tags[values.CurrentTagIndex].Name, helper.Unescaped)
}
// If author (commented out the code for generating a link. Ghost doesn't seem to do that.
//var buffer bytes.Buffer
//buffer.WriteString("<a href=\"")
//buffer.WriteString("/author/")
//buffer.WriteString(values.Author.Slug)
//buffer.WriteString("\">")
//buffer.Write(evaluateEscape([]byte(values.Author.Name), helper.Unescaped))
//buffer.WriteString("</a>")
//return buffer.Bytes()
//TODO: Error handling if there is no Posts[values.CurrentPostIndex]
return evaluateEscape(values.Posts[values.CurrentPostIndex].Author.Name, helper.Unescaped)
}
func tagDotNameFunc(helper *Helper, values *structure.RequestData) []byte {
if len(values.CurrentTag.Name) != 0 {
return evaluateEscape(values.CurrentTag.Name, helper.Unescaped)
} else {
return evaluateEscape(values.Posts[values.CurrentPostIndex].Tags[values.CurrentTagIndex].Name, helper.Unescaped)
}
}
func tagDotSlugFunc(helper *Helper, values *structure.RequestData) []byte {
if values.CurrentTag.Slug != "" {
return evaluateEscape([]byte(values.CurrentTag.Slug), helper.Unescaped)
} else {
return evaluateEscape([]byte(values.Posts[values.CurrentPostIndex].Tags[values.CurrentTagIndex].Slug), helper.Unescaped)
}
}
func paginationFunc(helper *Helper, values *structure.RequestData) []byte {
if template, ok := compiledTemplates.m["pagination"]; ok { // If the theme has a pagination.hbs
return executeHelper(template, values, values.CurrentHelperContext)
}
var count int64
var err error
if values.CurrentTemplate == 0 { // index
count = values.Blog.PostCount
} else if values.CurrentTemplate == 2 { // tag
count, err = database.RetrieveNumberOfPostsByTag(values.CurrentTag.Id)
if err != nil {
log.Println("couldn't get number of posts for tag", err.Error())
return []byte{}
}
} else if values.CurrentTemplate == 3 { // author
count, err = database.RetrieveNumberOfPostsByUser(values.Posts[values.CurrentPostIndex].Author.Id)
if err != nil {
log.Println("couldn't get number of posts for author", err.Error())
return []byte{}
}
}
if count > values.Blog.PostsPerPage {
maxPages := int64((float64(count) / float64(values.Blog.PostsPerPage)) + 0.5)
var buffer bytes.Buffer
buffer.WriteString("<nav class=\"pagination\" role=\"navigation\">")
// If this is not the first index page, display a back link
if values.CurrentIndexPage > 1 {
buffer.WriteString("\n\t\t<a class=\"newer-posts\" href=\"")
if values.CurrentIndexPage == 2 {
if values.CurrentTemplate == 3 { // author
buffer.WriteString("/author/")
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
} else if values.CurrentTemplate == 2 { // tag
buffer.WriteString("/tag/")
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.CurrentTag.Slug)
}
buffer.WriteString("/")
} else {
if values.CurrentTemplate == 3 { // author
buffer.WriteString("/author/")
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
} else if values.CurrentTemplate == 2 { // tag
buffer.WriteString("/tag/")
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.CurrentTag.Slug)
}
buffer.WriteString("/page/")
buffer.WriteString(strconv.Itoa(values.CurrentIndexPage - 1))
buffer.WriteString("/")
}
buffer.WriteString("\">← Newer Posts</a>")
}
buffer.WriteString("\n\t<span class=\"page-number\">Page ")
buffer.WriteString(strconv.Itoa(values.CurrentIndexPage))
buffer.WriteString(" of ")
buffer.WriteString(strconv.FormatInt(maxPages, 10))
buffer.WriteString("</span>")
if int64(values.CurrentIndexPage) < maxPages {
buffer.WriteString("\n\t\t<a class=\"older-posts\" href=\"")
if values.CurrentTemplate == 3 { // author
buffer.WriteString("/author/")
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.Posts[values.CurrentPostIndex].Author.Slug)
} else if values.CurrentTemplate == 2 { // tag
buffer.WriteString("/tag/")
// TODO: Error handling if there is no Posts[values.CurrentPostIndex]
buffer.WriteString(values.CurrentTag.Slug)
}
buffer.WriteString("/page/")
buffer.WriteString(strconv.Itoa(values.CurrentIndexPage + 1))
buffer.WriteString("/\">Older Posts →</a>")
}
buffer.WriteString("\n</nav>")
return buffer.Bytes()
} else {
return []byte("<nav class=\"pagination\" role=\"navigation\">\n\t<span class=\"page-number\">Page 1 of 1</span>\n</nav>")
}
}
func idFunc(helper *Helper, values *structure.RequestData) []byte {
return []byte(strconv.FormatInt(values.Posts[values.CurrentPostIndex].Id, 10))
}
func assetFunc(helper *Helper, values *structure.RequestData) []byte {
if len(helper.Arguments) != 0 {
var buffer bytes.Buffer
buffer.Write(values.Blog.AssetPath)
buffer.WriteString(helper.Arguments[0].Name)
return buffer.Bytes()
}
return []byte{}
}
func foreachFunc(helper *Helper, values *structure.RequestData) []byte {
if len(helper.Arguments) != 0 {
switch helper.Arguments[0].Name {
case "posts":
var buffer bytes.Buffer
for index, _ := range values.Posts {
//if values.Posts[index].Id != 0 { // If post is not empty (Commented out for now. This was only neccessary in previous versions, when the array length was always the postsPerPage length)
values.CurrentPostIndex = index
buffer.Write(executeHelper(helper, values, 1)) // context = post
//}
}
return buffer.Bytes()
case "tags":
var buffer bytes.Buffer
for index, _ := range values.Posts[values.CurrentPostIndex].Tags {
//if values.Posts[values.CurrentPostIndex].Tags[index].Id != 0 { // If tag is not empty (Commented out for now. Not neccessary.)
values.CurrentTagIndex = index
buffer.Write(executeHelper(helper, values, 2)) // context = tag
//}
}
return buffer.Bytes()
default:
return []byte{}
}
}
return []byte{}
}
func ifFunc(helper *Helper, values *structure.RequestData) []byte {
if len(helper.Arguments) != 0 {
if len(helper.Arguments[0].Function(&helper.Arguments[0], values)) != 0 {
return executeHelper(helper, values, values.CurrentHelperContext)
} else {
// Execute the else helper which is always at the last index of the if helper Arguments
if helper.Arguments[len(helper.Arguments)-1].Name == "else" {
if len(helper.Arguments[len(helper.Arguments)-1].Children) != 0 {
}
return executeHelper(&helper.Arguments[len(helper.Arguments)-1], values, values.CurrentHelperContext)
}
}
}
return []byte{}
}
func unlessFunc(helper *Helper, values *structure.RequestData) []byte {
if len(helper.Arguments) != 0 {
if len(helper.Arguments[0].Function(&helper.Arguments[0], values)) == 0 {
return executeHelper(helper, values, values.CurrentHelperContext)
} else {
// Execute the else helper which is always at the last index of the if helper Arguments
if helper.Arguments[len(helper.Arguments)-1].Name == "else" {
if len(helper.Arguments[len(helper.Arguments)-1].Children) != 0 {
}
return executeHelper(&helper.Arguments[len(helper.Arguments)-1], values, values.CurrentHelperContext)
}
}
}
return []byte{}
}
func atBlogDotTitleFunc(helper *Helper, values *structure.RequestData) []byte {
return evaluateEscape(values.Blog.Title, helper.Unescaped)
}
func atBlogDotUrlFunc(helper *Helper, values *structure.RequestData) []byte {
var buffer bytes.Buffer
// Write // in front of url to be protocol agnostic
buffer.WriteString("//")
buffer.Write(values.Blog.Url)
return evaluateEscape(buffer.Bytes(), helper.Unescaped)
}
func atBlogDotLogoFunc(helper *Helper, values *structure.RequestData) []byte {
return evaluateEscape(values.Blog.Logo, helper.Unescaped)
}
func atBlogDotCoverFunc(helper *Helper, values *structure.RequestData) []byte {
return evaluateEscape(values.Blog.Cover, helper.Unescaped)
}
func atBlogDotDescriptionFunc(helper *Helper, values *structure.RequestData) []byte {
return evaluateEscape(values.Blog.Description, helper.Unescaped)
}
func evaluateEscape(value []byte, unescaped bool) []byte {
if unescaped {
return value
}
return []byte(html.EscapeString(string(value)))
}