add new ContentedBBCodeTag option for contented 1.2.0++ thumbnails

This commit is contained in:
mappu 2017-11-18 15:04:19 +13:00
parent fdb854e6c7
commit c830c2b4dd
6 changed files with 37 additions and 4 deletions

View File

@ -22,6 +22,7 @@ type ServerOptions struct {
DeclareRSSLanguage string DeclareRSSLanguage string
DeclareRSSEmail string DeclareRSSEmail string
ContentedServer string ContentedServer string
ContentedBBCodeTag string
} }
func DefaultOptions() *ServerOptions { func DefaultOptions() *ServerOptions {
@ -43,5 +44,6 @@ func DefaultOptions() *ServerOptions {
DeclareRSSLanguage: "en-GB", DeclareRSSLanguage: "en-GB",
DeclareRSSEmail: `nobody@example.com`, DeclareRSSEmail: `nobody@example.com`,
ContentedServer: "", ContentedServer: "",
ContentedBBCodeTag: "",
} }
} }

View File

@ -76,6 +76,10 @@ func NewWikiServer(opts *ServerOptions) (*WikiServer, error) {
return &ws, nil return &ws, nil
} }
func (this *WikiServer) GetBBCodeRenderer() *BBCodeRenderer {
return NewBBCodeRenderer(this.opts.ExpectBaseURL, this.opts.ContentedServer, this.opts.ContentedBBCodeTag)
}
func (this *WikiServer) Close() { func (this *WikiServer) Close() {
this.db.Close() this.db.Close()
} }

View File

@ -2,6 +2,7 @@ package yatwiki
import ( import (
"encoding/json" "encoding/json"
"html"
"html/template" "html/template"
"net/url" "net/url"
"regexp" "regexp"
@ -13,13 +14,17 @@ type BBCodeRenderer struct {
baseUrl string baseUrl string
CodePresent bool CodePresent bool
DynamicContentWarning string DynamicContentWarning string
ContentedURL string
ContentedTag string
} }
func NewBBCodeRenderer(baseUrl string) *BBCodeRenderer { func NewBBCodeRenderer(baseUrl, ContentedURL, ContentedTag string) *BBCodeRenderer {
return &BBCodeRenderer{ return &BBCodeRenderer{
baseUrl: baseUrl, baseUrl: baseUrl,
CodePresent: false, CodePresent: false,
DynamicContentWarning: `⚠ run dynamic content`, 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) + `\]`),
`<a href="` + html.EscapeString(this.ContentedURL) + `p/${1}"><img class="imgur" alt="" src="` + html.EscapeString(this.ContentedURL) + `thumb/s/${1}" ></a>`,
nil,
},
)
}
for _, prr := range s_to_r { for _, prr := range s_to_r {
for prr.match.MatchString(data) { // repeat until all recursive replacements are consumed for prr.match.MatchString(data) { // repeat until all recursive replacements are consumed

View File

@ -25,7 +25,7 @@ func (this *WikiServer) routeArchive(w http.ResponseWriter, r *http.Request, rev
pto.CurrentPageName = a.Title pto.CurrentPageName = a.Title
pto.CurrentPageIsArticle = true pto.CurrentPageIsArticle = true
bcr := NewBBCodeRenderer(this.opts.ExpectBaseURL) bcr := this.GetBBCodeRenderer()
pto.Content = template.HTML( pto.Content = template.HTML(
`<div class="info">`+ `<div class="info">`+
`You are viewing specific revision of this page, last modified `+ `You are viewing specific revision of this page, last modified `+

View File

@ -1,13 +1,15 @@
package yatwiki package yatwiki
import ( import (
"html/template"
"net/http" "net/http"
) )
func (this *WikiServer) routeFormatting(w http.ResponseWriter, r *http.Request) { func (this *WikiServer) routeFormatting(w http.ResponseWriter, r *http.Request) {
pto := DefaultPageTemplateOptions(this.opts) pto := DefaultPageTemplateOptions(this.opts)
pto.CurrentPageName = "Formatting help" pto.CurrentPageName = "Formatting help"
pto.Content = `
content := `
<h2>Formatting help</h2><br><br> <h2>Formatting help</h2><br><br>
<ul> <ul>
<li>[h]header[/h]</li> <li>[h]header[/h]</li>
@ -21,9 +23,20 @@ func (this *WikiServer) routeFormatting(w http.ResponseWriter, r *http.Request)
<li>[article=page name]title[/article] or [rev=id]title[/rev]</li> <li>[article=page name]title[/article] or [rev=id]title[/rev]</li>
<li>[img]image-url[/img]</li> <li>[img]image-url[/img]</li>
<li>[imgur]asdf.jpg[/imgur]</li> <li>[imgur]asdf.jpg[/imgur]</li>
`
if len(this.opts.ContentedBBCodeTag) > 0 {
content += `
<li>[` + this.opts.ContentedBBCodeTag + `]abc[/` + this.opts.ContentedBBCodeTag + `]</li>
`
}
content += `
<li>[code]fixed width[/code]</li> <li>[code]fixed width[/code]</li>
<li>[section=header]content[/section]</li> <li>[section=header]content[/section]</li>
<li>[html]raw html[/html]</li> <li>[html]raw html[/html]</li>
</ul>` </ul>`
pto.Content = template.HTML(content)
this.servePageResponse(w, r, pto) this.servePageResponse(w, r, pto)
} }

View File

@ -30,7 +30,7 @@ func (this *WikiServer) routeView(w http.ResponseWriter, r *http.Request, articl
pto.CurrentPageName = articleTitle pto.CurrentPageName = articleTitle
pto.CurrentPageIsArticle = true pto.CurrentPageIsArticle = true
bcr := NewBBCodeRenderer(this.opts.ExpectBaseURL) bcr := this.GetBBCodeRenderer()
pto.Content = bcr.RenderHTML(string(a.Body)) pto.Content = bcr.RenderHTML(string(a.Body))
pto.LoadCodeResources = bcr.CodePresent pto.LoadCodeResources = bcr.CodePresent