diff --git a/database/validate.go b/database/validate.go index 71073aa..a1cd867 100644 --- a/database/validate.go +++ b/database/validate.go @@ -25,3 +25,16 @@ func AmIsValidAmsterdamID(test string) bool { } return true } + +// AmIsValidAmsterdamIDChar returns true if the character is a valid character in an Amsterdam ID. +func AmIsValidAmsterdamIDChar(ch byte) bool { + return strings.ContainsRune(AMS_ID_CHARS, rune(ch)) +} + +// AmIsValidPostLinkChar returns true if the character is a valid character in a post link. +func AmIsValidPostLinkChar(ch byte) bool { + if strings.ContainsRune(AMS_ID_CHARS, rune(ch)) { + return true + } + return ch == '.' || ch == '!' +} diff --git a/htmlcheck/rewriter.go b/htmlcheck/rewriter.go index 6418e97..63cbffa 100644 --- a/htmlcheck/rewriter.go +++ b/htmlcheck/rewriter.go @@ -98,8 +98,8 @@ func (rw *emailRewriter) Rewrite(data string, svc rewriterServices) *markupData // postLinkRewriter is the rewriter that handles links to conference posts. type postLinkRewriter struct{} -// postLinkURLPrefix is the default URL prefix for post links. -const postLinkURLPrefix = "x-postlink:" +// PostLinkURLPrefix is the default URL prefix for post links. +const PostLinkURLPrefix = "x-postlink:" // Name returns the rewriter's name. func (rw *postLinkRewriter) Name() string { @@ -175,7 +175,7 @@ func (rw *postLinkRewriter) Rewrite(data string, svc rewriterServices) *markupDa // build the necessary markup and return it var openA strings.Builder openA.WriteString("= 0 { + var buf strings.Builder + t := data + for plIndex >= 0 { + if plIndex > 0 { + buf.WriteString(t[:plIndex]) + t = t[plIndex+len(htmlcheck.PostLinkURLPrefix):] + } + p := 0 + for database.AmIsValidPostLinkChar(t[p]) { + p++ + } + if p > 0 { + buf.WriteString("/go/") + buf.WriteString(t[:p]) + t = t[p:] + } else { + buf.WriteString(htmlcheck.PostLinkURLPrefix) + } + plIndex = strings.Index(t, htmlcheck.PostLinkURLPrefix) + } + buf.WriteString(t) + data = buf.String() + } + + ulIndex = strings.Index(data, htmlcheck.UserLinkURIPRefix) + if ulIndex >= 0 { + var buf strings.Builder + t := data + for ulIndex >= 0 { + if ulIndex > 0 { + buf.WriteString(t[:ulIndex]) + t = t[ulIndex+len(htmlcheck.UserLinkURIPRefix):] + } + p := 0 + for database.AmIsValidAmsterdamIDChar(t[p]) { + p++ + } + if p > 0 { + buf.WriteString("/user/") + buf.WriteString(t[:p]) + t = t[p:] + } else { + buf.WriteString(htmlcheck.UserLinkURIPRefix) + } + ulIndex = strings.Index(t, htmlcheck.UserLinkURIPRefix) + } + buf.WriteString(t) + data = buf.String() + } + return reflect.ValueOf(data) +} + // SetupTemplates is called to set up the template renderer after the configuration is loaded. func SetupTemplates() { views = jet.NewSet( @@ -199,6 +261,7 @@ func SetupTemplates() { views.AddGlobal("AmsterdamCopyright", config.AMSTERDAM_COPYRIGHT) views.AddGlobal("GlobalConfig", config.GlobalConfig) views.AddGlobalFunc("iif", immediateIf) + views.AddGlobalFunc("postRewrite", postRewrite) views.AddGlobalFunc("MakeIntRange", makeIntRange) views.AddGlobalFunc("MakeYearRange", makeYearRange) views.AddGlobalFunc("ExtractCommunityLogo", extractCommunityLogo) diff --git a/ui/views/new_topic.jet b/ui/views/new_topic.jet index 65e0b17..1626982 100644 --- a/ui/views/new_topic.jet +++ b/ui/views/new_topic.jet @@ -41,7 +41,7 @@
-
{{ previewPb | raw }}
+
{{ previewPb | postRewrite | raw }}