added post link rewriter template function
This commit is contained in:
@@ -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 == '!'
|
||||
}
|
||||
|
||||
@@ -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("<a href=\"")
|
||||
openA.WriteString(postLinkURLPrefix)
|
||||
openA.WriteString(PostLinkURLPrefix)
|
||||
openA.WriteString(link)
|
||||
openA.WriteString("\"")
|
||||
catenate := svc.rewriterAttrValue("ANCHORTAIL")
|
||||
@@ -196,7 +196,7 @@ func (rw *postLinkRewriter) Rewrite(data string, svc rewriterServices) *markupDa
|
||||
type userLinkRewriter struct{}
|
||||
|
||||
// userLinkURIPrefix is the default URL prefix for user links.
|
||||
const userLinkURIPRefix = "x-userlink:"
|
||||
const UserLinkURIPRefix = "x-userlink:"
|
||||
|
||||
// Name returns the rewriter's name.
|
||||
func (rw *userLinkRewriter) Name() string {
|
||||
@@ -223,7 +223,7 @@ func (rw *userLinkRewriter) Rewrite(data string, svc rewriterServices) *markupDa
|
||||
// build the necessary markup and return it
|
||||
var openA strings.Builder
|
||||
openA.WriteString("<a href=\"")
|
||||
openA.WriteString(userLinkURIPRefix)
|
||||
openA.WriteString(UserLinkURIPRefix)
|
||||
openA.WriteString(data)
|
||||
openA.WriteString("\"")
|
||||
catenate := svc.rewriterAttrValue("ANCHORTAIL")
|
||||
|
||||
@@ -50,6 +50,7 @@ func setupEcho() *echo.Echo {
|
||||
e.POST("/TODO/*", fn)
|
||||
e.GET("/img/*", ui.AmWrap(ui.AmServeImage))
|
||||
e.GET("/static/*", ui.AmStaticFileHandler())
|
||||
e.GET("/go/:postlink", fn)
|
||||
|
||||
e.GET("/", ui.AmWrap(TopPage))
|
||||
e.GET("/about", ui.AmWrap(AboutPage))
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
|
||||
"git.erbosoft.com/amy/amsterdam/config"
|
||||
"git.erbosoft.com/amy/amsterdam/database"
|
||||
"git.erbosoft.com/amy/amsterdam/htmlcheck"
|
||||
"git.erbosoft.com/amy/amsterdam/util"
|
||||
"github.com/CloudyKit/jet/v6"
|
||||
"github.com/CloudyKit/jet/v6/loaders/embedfs"
|
||||
@@ -186,6 +187,67 @@ func displayExpandCat(a jet.Arguments) reflect.Value {
|
||||
return reflect.ValueOf(rc.String())
|
||||
}
|
||||
|
||||
func postRewrite(a jet.Arguments) reflect.Value {
|
||||
data := a.Get(0).Convert(reflect.TypeFor[string]()).String()
|
||||
plIndex := strings.Index(data, htmlcheck.PostLinkURLPrefix)
|
||||
ulIndex := strings.Index(data, htmlcheck.UserLinkURIPRefix)
|
||||
if plIndex < 0 && ulIndex < 0 {
|
||||
return reflect.ValueOf(data)
|
||||
}
|
||||
|
||||
if plIndex >= 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)
|
||||
|
||||
@@ -41,7 +41,7 @@
|
||||
|
||||
<!-- Preview Display -->
|
||||
<div class="bg-white border-2 border-blue-300 rounded-lg p-6 mb-6">
|
||||
<pre class="amsPost font-mono text-sm whitespace-pre-wrap break-words">{{ previewPb | raw }}</pre>
|
||||
<pre class="amsPost font-mono text-sm whitespace-pre-wrap break-words">{{ previewPb | postRewrite | raw }}</pre>
|
||||
</div>
|
||||
|
||||
<hr class="border-gray-400 mb-6">
|
||||
|
||||
Reference in New Issue
Block a user