File: tags_now.go

package info (click to toggle)
golang-github-flosch-pongo2.v4 4.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, trixie
  • size: 860 kB
  • sloc: makefile: 3
file content (50 lines) | stat: -rw-r--r-- 959 bytes parent folder | download | duplicates (2)
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
package pongo2

import (
	"time"
)

type tagNowNode struct {
	position *Token
	format   string
	fake     bool
}

func (node *tagNowNode) Execute(ctx *ExecutionContext, writer TemplateWriter) *Error {
	var t time.Time
	if node.fake {
		t = time.Date(2014, time.February, 05, 18, 31, 45, 00, time.UTC)
	} else {
		t = time.Now()
	}

	writer.WriteString(t.Format(node.format))

	return nil
}

func tagNowParser(doc *Parser, start *Token, arguments *Parser) (INodeTag, *Error) {
	nowNode := &tagNowNode{
		position: start,
	}

	formatToken := arguments.MatchType(TokenString)
	if formatToken == nil {
		return nil, arguments.Error("Expected a format string.", nil)
	}
	nowNode.format = formatToken.Val

	if arguments.MatchOne(TokenIdentifier, "fake") != nil {
		nowNode.fake = true
	}

	if arguments.Remaining() > 0 {
		return nil, arguments.Error("Malformed now-tag arguments.", nil)
	}

	return nowNode, nil
}

func init() {
	RegisterTag("now", tagNowParser)
}