diff options
| author | Bobby <[email protected]> | 2026-03-29 22:52:46 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2026-03-29 22:52:46 +0530 |
| commit | 9eb9b7f4bd552a641235764f66483e1f940fcfd9 (patch) | |
| tree | da520b923b5e6758d5457b6233dd6671fc640914 /nexus/tags | |
| parent | 65a143a0871c35989b7c7ea6723d39a0585c089e (diff) | |
| download | echoes-of-vaelun-main.tar.xz echoes-of-vaelun-main.zip | |
Diffstat (limited to 'nexus/tags')
| -rw-r--r-- | nexus/tags/active.go | 20 | ||||
| -rw-r--r-- | nexus/tags/defaults.go | 6 | ||||
| -rw-r--r-- | nexus/tags/messages.go | 12 | ||||
| -rw-r--r-- | nexus/tags/static.go | 32 | ||||
| -rw-r--r-- | nexus/tags/tags.go | 40 | ||||
| -rw-r--r-- | nexus/tags/url.go | 97 |
6 files changed, 207 insertions, 0 deletions
diff --git a/nexus/tags/active.go b/nexus/tags/active.go new file mode 100644 index 0000000..c762222 --- /dev/null +++ b/nexus/tags/active.go @@ -0,0 +1,20 @@ +package tags
+
+import (
+ "nexus/utils/urls"
+
+ "github.com/flosch/pongo2/v6"
+)
+
+func active(value *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
+ routeName := value.String()
+
+ routePath, exists := urls.GetFullPath(routeName)
+ if !exists {
+ return pongo2.AsValue(false), nil
+ }
+
+ requestPath := param.String()
+
+ return pongo2.AsValue(requestPath == routePath), nil
+}
diff --git a/nexus/tags/defaults.go b/nexus/tags/defaults.go new file mode 100644 index 0000000..b040c18 --- /dev/null +++ b/nexus/tags/defaults.go @@ -0,0 +1,6 @@ +package tags
+
+const (
+ LogPrefix = "Tags"
+ StaticPrefix = "/static"
+)
diff --git a/nexus/tags/messages.go b/nexus/tags/messages.go new file mode 100644 index 0000000..87644ae --- /dev/null +++ b/nexus/tags/messages.go @@ -0,0 +1,12 @@ +package tags
+
+const (
+ ExpectedEquals = "Expected '=' after parameter key."
+ ExpectedParamKey = "Expected parameter key identifier."
+ ExpectedRouteName = "Expected route name string."
+ ExpectedStaticPath = "Expected static file path string."
+ ExpectedVariableName = "Expected variable name after 'as'."
+ RegistrationFailed = "Failed to register tag: %s."
+ RouteNotFound = "Route not found: %s."
+ TemplateWriteFailed = "Failed to write template output."
+)
diff --git a/nexus/tags/static.go b/nexus/tags/static.go new file mode 100644 index 0000000..ba29807 --- /dev/null +++ b/nexus/tags/static.go @@ -0,0 +1,32 @@ +package tags
+
+import (
+ "fmt"
+
+ "github.com/flosch/pongo2/v6"
+)
+
+type StaticNode struct {
+ Path string
+}
+
+func static(document *pongo2.Parser, start *pongo2.Token, arguments *pongo2.Parser) (pongo2.INodeTag, *pongo2.Error) {
+ pathToken := arguments.MatchType(pongo2.TokenString)
+ if pathToken == nil {
+ return nil, arguments.Error(ExpectedStaticPath, nil)
+ }
+
+ return &StaticNode{Path: pathToken.Val}, nil
+}
+
+func (self *StaticNode) Execute(executionContext *pongo2.ExecutionContext, writer pongo2.TemplateWriter) *pongo2.Error {
+ _, writeError := writer.WriteString(fmt.Sprintf("%s/%s", StaticPrefix, self.Path))
+ if writeError != nil {
+ return &pongo2.Error{
+ Sender: "tag:static",
+ OrigError: fmt.Errorf(TemplateWriteFailed),
+ }
+ }
+
+ return nil
+}
diff --git a/nexus/tags/tags.go b/nexus/tags/tags.go new file mode 100644 index 0000000..165172d --- /dev/null +++ b/nexus/tags/tags.go @@ -0,0 +1,40 @@ +package tags
+
+import (
+ "nexus/utils/logger"
+
+ "github.com/flosch/pongo2/v6"
+)
+
+type TemplateTag struct {
+ Name string
+ Parser pongo2.TagParser
+}
+
+type TemplateFilter struct {
+ Name string
+ Filter pongo2.FilterFunction
+}
+
+func Initialize() {
+ tags := []TemplateTag{
+ {Name: "static", Parser: static},
+ {Name: "url", Parser: url},
+ }
+
+ filters := []TemplateFilter{
+ {Name: "active", Filter: active},
+ }
+
+ for _, tag := range tags {
+ if registrationError := pongo2.RegisterTag(tag.Name, tag.Parser); registrationError != nil {
+ logger.Errorf(LogPrefix, RegistrationFailed, tag.Name)
+ }
+ }
+
+ for _, filter := range filters {
+ if registrationError := pongo2.RegisterFilter(filter.Name, filter.Filter); registrationError != nil {
+ logger.Errorf(LogPrefix, RegistrationFailed, filter.Name)
+ }
+ }
+}
diff --git a/nexus/tags/url.go b/nexus/tags/url.go new file mode 100644 index 0000000..85f7923 --- /dev/null +++ b/nexus/tags/url.go @@ -0,0 +1,97 @@ +package tags
+
+import (
+ "fmt"
+ "strings"
+
+ "nexus/utils/collections"
+ "nexus/utils/urls"
+
+ "github.com/flosch/pongo2/v6"
+)
+
+type UrlNode struct {
+ RouteName string
+ Params collections.Record[string, pongo2.IEvaluator]
+ VariableName string
+}
+
+func url(document *pongo2.Parser, start *pongo2.Token, arguments *pongo2.Parser) (pongo2.INodeTag, *pongo2.Error) {
+ routeNameToken := arguments.MatchType(pongo2.TokenString)
+ if routeNameToken == nil {
+ return nil, arguments.Error(ExpectedRouteName, nil)
+ }
+
+ params := make(collections.Record[string, pongo2.IEvaluator])
+
+ var variableName string
+
+ for arguments.Remaining() > 0 {
+ if arguments.Match(pongo2.TokenKeyword, "as") != nil {
+ nameToken := arguments.MatchType(pongo2.TokenIdentifier)
+ if nameToken == nil {
+ return nil, arguments.Error(ExpectedVariableName, nil)
+ }
+ variableName = nameToken.Val
+ break
+ }
+
+ keyToken := arguments.MatchType(pongo2.TokenIdentifier)
+ if keyToken == nil {
+ return nil, arguments.Error(ExpectedParamKey, nil)
+ }
+
+ if arguments.Match(pongo2.TokenSymbol, "=") == nil {
+ return nil, arguments.Error(ExpectedEquals, nil)
+ }
+
+ valueExpression, parseError := arguments.ParseExpression()
+ if parseError != nil {
+ return nil, parseError
+ }
+
+ params[keyToken.Val] = valueExpression
+ }
+
+ return &UrlNode{
+ RouteName: routeNameToken.Val,
+ Params: params,
+ VariableName: variableName,
+ }, nil
+}
+
+func (self *UrlNode) Execute(executionContext *pongo2.ExecutionContext, writer pongo2.TemplateWriter) *pongo2.Error {
+ path, exists := urls.GetFullPath(self.RouteName)
+ if !exists {
+ return &pongo2.Error{
+ Sender: "tag:url",
+ OrigError: fmt.Errorf(RouteNotFound, self.RouteName),
+ }
+ }
+
+ for key, expression := range self.Params {
+ evaluatedValue, evaluationError := expression.Evaluate(executionContext)
+ if evaluationError != nil {
+ return evaluationError
+ }
+
+ placeholder := fmt.Sprintf(":%s", key)
+ replacement := fmt.Sprintf("%v", evaluatedValue.Interface())
+ path = strings.ReplaceAll(path, placeholder, replacement)
+ }
+
+ if self.VariableName != "" {
+ executionContext.Public[self.VariableName] = path
+ return nil
+ }
+
+ _, writeError := writer.WriteString(path)
+ if writeError != nil {
+ return &pongo2.Error{
+ Sender: "tag:url",
+ OrigError: fmt.Errorf(TemplateWriteFailed),
+ }
+ }
+
+ return nil
+}
|
