From c830c2b4dd1103de5b3cebab788ef2549d702fb5 Mon Sep 17 00:00:00 2001 From: mappu Date: Sat, 18 Nov 2017 15:04:19 +1300 Subject: [PATCH] add new ContentedBBCodeTag option for contented 1.2.0++ thumbnails --- ServerOptions.go | 2 ++ WikiServer.go | 4 ++++ bbcode.go | 16 +++++++++++++++- rArchive.go | 2 +- rFormatting.go | 15 ++++++++++++++- rView.go | 2 +- 6 files changed, 37 insertions(+), 4 deletions(-) diff --git a/ServerOptions.go b/ServerOptions.go index ea5261a..0c322a8 100644 --- a/ServerOptions.go +++ b/ServerOptions.go @@ -22,6 +22,7 @@ type ServerOptions struct { DeclareRSSLanguage string DeclareRSSEmail string ContentedServer string + ContentedBBCodeTag string } func DefaultOptions() *ServerOptions { @@ -43,5 +44,6 @@ func DefaultOptions() *ServerOptions { DeclareRSSLanguage: "en-GB", DeclareRSSEmail: `nobody@example.com`, ContentedServer: "", + ContentedBBCodeTag: "", } } diff --git a/WikiServer.go b/WikiServer.go index c36d35f..b58df7f 100644 --- a/WikiServer.go +++ b/WikiServer.go @@ -76,6 +76,10 @@ func NewWikiServer(opts *ServerOptions) (*WikiServer, error) { return &ws, nil } +func (this *WikiServer) GetBBCodeRenderer() *BBCodeRenderer { + return NewBBCodeRenderer(this.opts.ExpectBaseURL, this.opts.ContentedServer, this.opts.ContentedBBCodeTag) +} + func (this *WikiServer) Close() { this.db.Close() } diff --git a/bbcode.go b/bbcode.go index e7f1dc8..cb1fea1 100644 --- a/bbcode.go +++ b/bbcode.go @@ -2,6 +2,7 @@ package yatwiki import ( "encoding/json" + "html" "html/template" "net/url" "regexp" @@ -13,13 +14,17 @@ type BBCodeRenderer struct { baseUrl string CodePresent bool DynamicContentWarning string + ContentedURL string + ContentedTag string } -func NewBBCodeRenderer(baseUrl string) *BBCodeRenderer { +func NewBBCodeRenderer(baseUrl, ContentedURL, ContentedTag string) *BBCodeRenderer { return &BBCodeRenderer{ baseUrl: baseUrl, CodePresent: false, DynamicContentWarning: `⚠ run dynamic content`, + ContentedURL: ContentedURL, + ContentedTag: ContentedTag, } } @@ -61,6 +66,15 @@ func (this *BBCodeRenderer) bbcode(data string) string { }}, } + if len(this.ContentedTag) > 0 { + s_to_r = append(s_to_r, + pregReplaceRule{regexp.MustCompile(`(?si)\[` + regexp.QuoteMeta(this.ContentedTag) + `\](.*?)\[/` + regexp.QuoteMeta(this.ContentedTag) + `\]`), + ``, + nil, + }, + ) + } + for _, prr := range s_to_r { for prr.match.MatchString(data) { // repeat until all recursive replacements are consumed diff --git a/rArchive.go b/rArchive.go index c3a26e6..783575b 100644 --- a/rArchive.go +++ b/rArchive.go @@ -25,7 +25,7 @@ func (this *WikiServer) routeArchive(w http.ResponseWriter, r *http.Request, rev pto.CurrentPageName = a.Title pto.CurrentPageIsArticle = true - bcr := NewBBCodeRenderer(this.opts.ExpectBaseURL) + bcr := this.GetBBCodeRenderer() pto.Content = template.HTML( `
`+ `You are viewing specific revision of this page, last modified `+ diff --git a/rFormatting.go b/rFormatting.go index ece3da3..569fca2 100644 --- a/rFormatting.go +++ b/rFormatting.go @@ -1,13 +1,15 @@ package yatwiki import ( + "html/template" "net/http" ) func (this *WikiServer) routeFormatting(w http.ResponseWriter, r *http.Request) { pto := DefaultPageTemplateOptions(this.opts) pto.CurrentPageName = "Formatting help" - pto.Content = ` + + content := `

Formatting help



` + + pto.Content = template.HTML(content) + this.servePageResponse(w, r, pto) } diff --git a/rView.go b/rView.go index 9e36fce..fb265e9 100644 --- a/rView.go +++ b/rView.go @@ -30,7 +30,7 @@ func (this *WikiServer) routeView(w http.ResponseWriter, r *http.Request, articl pto.CurrentPageName = articleTitle pto.CurrentPageIsArticle = true - bcr := NewBBCodeRenderer(this.opts.ExpectBaseURL) + bcr := this.GetBBCodeRenderer() pto.Content = bcr.RenderHTML(string(a.Body)) pto.LoadCodeResources = bcr.CodePresent