landed enough rewriters and filters to begin building configurations for the HTML checker
This commit is contained in:
+163
-6
@@ -10,9 +10,12 @@
|
||||
package htmlcheck
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/mail"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"git.erbosoft.com/amy/amsterdam/database"
|
||||
)
|
||||
|
||||
// markupData holds the return from rewriters.
|
||||
@@ -37,12 +40,24 @@ type rewriter interface {
|
||||
Rewrite(string, rewriterServices) *markupData
|
||||
}
|
||||
|
||||
// rewriterRegistry contains a list of all rewriters.
|
||||
var rewriterRegistry = make(map[string]rewriter)
|
||||
|
||||
// init registers our rewriters with the registry.
|
||||
func init() {
|
||||
r1 := emailRewriter{}
|
||||
rewriterRegistry[r1.Name()] = &r1
|
||||
r2 := urlRewriter{}
|
||||
rewriterRegistry[r2.Name()] = &r2
|
||||
r3 := postLinkRewriter{}
|
||||
rewriterRegistry[r3.Name()] = &r3
|
||||
r4 := userLinkRewriter{}
|
||||
rewriterRegistry[r4.Name()] = &r4
|
||||
}
|
||||
|
||||
// emailRewriter is an implementation of Rewriter that recognizes E-mail addresses.
|
||||
type emailRewriter struct{}
|
||||
|
||||
// EmailRewriter is a singleton implementration of rewriter for E-mail addresses.
|
||||
var EmailRewriter = emailRewriter{}
|
||||
|
||||
// Name returns the rewriter's name.
|
||||
func (rw *emailRewriter) Name() string {
|
||||
return "email"
|
||||
@@ -81,9 +96,6 @@ func (rw *emailRewriter) Rewrite(data string, svc rewriterServices) *markupData
|
||||
// urlRewriter is an implementation of Rewriter that recognizes URLs.
|
||||
type urlRewriter struct{}
|
||||
|
||||
// URLRewriter is a singleton implementration of rewriter for URLs.
|
||||
var URLRewriter = urlRewriter{}
|
||||
|
||||
// Name returns the rewriter's name.
|
||||
func (rw *urlRewriter) Name() string {
|
||||
return "url"
|
||||
@@ -137,3 +149,148 @@ func (rw *urlRewriter) Rewrite(data string, svc rewriterServices) *markupData {
|
||||
rescan: false,
|
||||
}
|
||||
}
|
||||
|
||||
// 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:"
|
||||
|
||||
// Name returns the rewriter's name.
|
||||
func (rw *postLinkRewriter) Name() string {
|
||||
return "postlink"
|
||||
}
|
||||
|
||||
// buildPostLink constructs a full post link from decoded data and context.
|
||||
func buildPostLink(decoded, context *database.PostLinkData) string {
|
||||
var b strings.Builder
|
||||
started := false
|
||||
if decoded.Community == "" {
|
||||
b.WriteString(context.Community)
|
||||
} else {
|
||||
b.WriteString(decoded.Community)
|
||||
started = true
|
||||
}
|
||||
b.WriteString("!")
|
||||
if decoded.Conference == "" {
|
||||
if started {
|
||||
return b.String()
|
||||
}
|
||||
b.WriteString(context.Conference)
|
||||
} else {
|
||||
b.WriteString(decoded.Conference)
|
||||
}
|
||||
b.WriteString(".")
|
||||
if decoded.Topic == -1 {
|
||||
if started {
|
||||
return b.String()
|
||||
}
|
||||
b.WriteString(fmt.Sprintf("%d", context.Topic))
|
||||
} else {
|
||||
b.WriteString(fmt.Sprintf("%d", decoded.Topic))
|
||||
}
|
||||
b.WriteString(".")
|
||||
if decoded.FirstPost != -1 {
|
||||
b.WriteString(fmt.Sprintf("%d", decoded.FirstPost))
|
||||
if decoded.FirstPost != decoded.LastPost {
|
||||
b.WriteString("-")
|
||||
if decoded.LastPost != -1 {
|
||||
b.WriteString(fmt.Sprintf("%d", decoded.LastPost))
|
||||
}
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
/* Rewrite rewrites the given string data and adds markup before and after if needed.
|
||||
* Parameters:
|
||||
* data - The data to be rewritten.
|
||||
* svc - Services interface we can use.
|
||||
* Returns:
|
||||
* Pointer to markup data, or nil.
|
||||
*/
|
||||
func (rw *postLinkRewriter) Rewrite(data string, svc rewriterServices) *markupData {
|
||||
q := svc.rewriterContextValue("PostLinkDecoderContext")
|
||||
if q == nil {
|
||||
return nil
|
||||
}
|
||||
ctxt := q.(*database.PostLinkData)
|
||||
|
||||
mydata, err := database.AmDecodePostLink(data)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
err = mydata.VerifyNames()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
// build post link, add it as an internal reference
|
||||
link := buildPostLink(mydata, ctxt)
|
||||
svc.addInternalRef(link)
|
||||
// build the necessary markup and return it
|
||||
var openA strings.Builder
|
||||
openA.WriteString("<a href=\"")
|
||||
openA.WriteString(postLinkURLPrefix)
|
||||
openA.WriteString(link)
|
||||
openA.WriteString("\"")
|
||||
catenate := svc.rewriterAttrValue("ANCHORTAIL")
|
||||
if catenate != "" {
|
||||
openA.WriteString(" ")
|
||||
openA.WriteString(catenate)
|
||||
}
|
||||
openA.WriteString(">")
|
||||
return &markupData{
|
||||
beginMarkup: openA.String(),
|
||||
text: data,
|
||||
endMarkup: "</a>",
|
||||
rescan: false,
|
||||
}
|
||||
}
|
||||
|
||||
// userLinkRewriter is the rewriter that handles links to user names.
|
||||
type userLinkRewriter struct{}
|
||||
|
||||
// userLinkURIPrefix is the default URL prefix for user links.
|
||||
const userLinkURIPRefix = "x-userlink:"
|
||||
|
||||
// Name returns the rewriter's name.
|
||||
func (rw *userLinkRewriter) Name() string {
|
||||
return "userlink"
|
||||
}
|
||||
|
||||
/* Rewrite rewrites the given string data and adds markup before and after if needed.
|
||||
* Parameters:
|
||||
* data - The data to be rewritten.
|
||||
* svc - Services interface we can use.
|
||||
* Returns:
|
||||
* Pointer to markup data, or nil.
|
||||
*/
|
||||
func (rw *userLinkRewriter) Rewrite(data string, svc rewriterServices) *markupData {
|
||||
if data == "" || len(data) > 64 || !database.AmIsValidAmsterdamID(data) {
|
||||
return nil
|
||||
}
|
||||
|
||||
user, err := database.AmGetUserByName(data)
|
||||
if err != nil || user == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// build the necessary markup and return it
|
||||
var openA strings.Builder
|
||||
openA.WriteString("<a href=\"")
|
||||
openA.WriteString(userLinkURIPRefix)
|
||||
openA.WriteString(data)
|
||||
openA.WriteString("\"")
|
||||
catenate := svc.rewriterAttrValue("ANCHORTAIL")
|
||||
if catenate != "" {
|
||||
openA.WriteString(" ")
|
||||
openA.WriteString(catenate)
|
||||
}
|
||||
openA.WriteString(">")
|
||||
return &markupData{
|
||||
beginMarkup: openA.String(),
|
||||
text: data,
|
||||
endMarkup: "</a>",
|
||||
rescan: false,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user