summaryrefslogtreecommitdiff
path: root/nexus/tags/url.go
diff options
context:
space:
mode:
authorBobby <[email protected]>2026-03-29 22:52:46 +0530
committerBobby <[email protected]>2026-03-29 22:52:46 +0530
commit9eb9b7f4bd552a641235764f66483e1f940fcfd9 (patch)
treeda520b923b5e6758d5457b6233dd6671fc640914 /nexus/tags/url.go
parent65a143a0871c35989b7c7ea6723d39a0585c089e (diff)
downloadechoes-of-vaelun-main.tar.xz
echoes-of-vaelun-main.zip
feat: nexus account manager scaffold with auth, characters, realmsHEADmain
Diffstat (limited to 'nexus/tags/url.go')
-rw-r--r--nexus/tags/url.go97
1 files changed, 97 insertions, 0 deletions
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
+}