better render performance and code cleanup

This commit is contained in:
ston1th 2016-11-17 21:46:12 +01:00
commit fb41dd75c4
3 changed files with 28 additions and 27 deletions

View file

@ -18,19 +18,23 @@ func makeFilter() analysis.CharFilter {
return f
}
var (
reHeader = regexp.MustCompile("(<h[1-6])>(.+)(</h[1-6]>)")
reLink = regexp.MustCompile(`id="(.+)">`)
whiteSpace = regexp.MustCompile(`\s+`)
reIndex = regexp.MustCompile("<h([1-6])>(.+)</h[1-6]>")
)
func render(text string) Article {
re := regexp.MustCompile("(<h[1-6])>(.+)(</h[1-6]>)")
link := regexp.MustCompile(`id="(.+)">`)
ws := regexp.MustCompile("\\s+")
rend := blackfriday.MarkdownCommon([]byte(text))
html := string(rend)
index := buildIndex(html)
html = strings.Replace(html, "<table>", `<table class="table">`, -1)
html = re.ReplaceAllString(html, `${1} id="${2}">${2}${3}`)
html = link.ReplaceAllStringFunc(html, makeLinkTitle)
html = reHeader.ReplaceAllString(html, `${1} id="${2}">${2}${3}`)
html = reLink.ReplaceAllStringFunc(html, makeLinkTitle)
html = strings.Replace(html, "</h1>", `</h1><hr>`, -1)
return Article{
Search: ws.ReplaceAllString(string(filter.Filter(rend)), " "),
Search: whiteSpace.ReplaceAllString(string(filter.Filter(rend)), " "),
Index: template.HTML(index),
Text: template.HTML(html),
MD: text,
@ -42,8 +46,7 @@ func makeLinkTitle(title string) string {
}
func buildIndex(html string) (ret string) {
re := regexp.MustCompile("<h([1-6])>(.+)</h[1-6]>")
hn := re.FindAllStringSubmatch(html, -1)
hn := reIndex.FindAllStringSubmatch(html, -1)
for k, v := range hn {
link := makeLinkTitle(v[2])
ret += before(hn, k)