87 lines
2.2 KiB
Go
87 lines
2.2 KiB
Go
// Copyright (C) 2021 Marius Schellenberger
|
|
|
|
package render
|
|
|
|
import (
|
|
"git.giftfish.de/ston1th/gowiki/pkg/core"
|
|
"github.com/blevesearch/bleve/analysis"
|
|
htmlchar "github.com/blevesearch/bleve/analysis/char/html"
|
|
"github.com/russross/blackfriday"
|
|
"html/template"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
const delim = "+"
|
|
|
|
var filter = makeFilter()
|
|
|
|
func makeFilter() analysis.CharFilter {
|
|
f, _ := htmlchar.CharFilterConstructor(nil, nil)
|
|
return f
|
|
}
|
|
|
|
var (
|
|
titleRe = regexp.MustCompile("[^a-zA-Z0-9 -]+")
|
|
whiteSpace = regexp.MustCompile(`\s+`)
|
|
)
|
|
|
|
const (
|
|
commonHtmlFlags = 0 |
|
|
blackfriday.HTML_USE_XHTML |
|
|
blackfriday.HTML_USE_SMARTYPANTS |
|
|
blackfriday.HTML_SMARTYPANTS_FRACTIONS |
|
|
blackfriday.HTML_SMARTYPANTS_DASHES |
|
|
blackfriday.HTML_SMARTYPANTS_LATEX_DASHES |
|
|
blackfriday.HTML_TOC |
|
|
blackfriday.HTML_HREF_TARGET_BLANK |
|
|
blackfriday.HTML_NOREFERRER_LINKS |
|
|
blackfriday.HTML_NOFOLLOW_LINKS |
|
|
blackfriday.HTML_NOOPENER_LINKS
|
|
|
|
commonExtensions = 0 |
|
|
blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
|
|
blackfriday.EXTENSION_TABLES |
|
|
blackfriday.EXTENSION_FENCED_CODE |
|
|
blackfriday.EXTENSION_AUTOLINK |
|
|
blackfriday.EXTENSION_STRIKETHROUGH |
|
|
blackfriday.EXTENSION_SPACE_HEADERS |
|
|
blackfriday.EXTENSION_HEADER_IDS |
|
|
blackfriday.EXTENSION_BACKSLASH_LINE_BREAK |
|
|
blackfriday.EXTENSION_DEFINITION_LISTS |
|
|
blackfriday.EXTENSION_AUTO_HEADER_IDS
|
|
)
|
|
|
|
func Render(p *core.Page) string {
|
|
renderer := blackfriday.HtmlRenderer(commonHtmlFlags, "", "")
|
|
rend := blackfriday.MarkdownOptions([]byte(p.Markdown), renderer,
|
|
blackfriday.Options{Extensions: commonExtensions},
|
|
)
|
|
html := string(rend)
|
|
html = strings.ReplaceAll(html, "<table>", `<table class="table">`)
|
|
p.HTML = template.HTML(html)
|
|
return whiteSpace.ReplaceAllString(string(filter.Filter(rend)), " ")
|
|
}
|
|
|
|
func Title(title string) string {
|
|
return whiteSpace.ReplaceAllString(titleRe.ReplaceAllString(title, ""), delim)
|
|
}
|
|
|
|
func Untitle(title string) string {
|
|
return strings.ReplaceAll(title, delim, " ")
|
|
}
|
|
|
|
func StoreTitle(section, title string) string {
|
|
return section + "/" + title
|
|
}
|
|
|
|
func SplitStoreTitle(st string) (section, title string) {
|
|
a := strings.Split(st, "/")
|
|
switch len(a) {
|
|
case 1:
|
|
return a[0], ""
|
|
case 2:
|
|
return a[0], a[1]
|
|
}
|
|
return
|
|
}
|