154 lines
3.4 KiB
Go
154 lines
3.4 KiB
Go
// Copyright (C) 2017 Marius Schellenberger
|
|
|
|
package main
|
|
|
|
import (
|
|
"html/template"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/blevesearch/bleve/analysis"
|
|
htmlFilter "github.com/blevesearch/bleve/analysis/char_filters/html_char_filter"
|
|
"github.com/russross/blackfriday"
|
|
)
|
|
|
|
var filter = makeFilter()
|
|
|
|
func makeFilter() analysis.CharFilter {
|
|
f, _ := htmlFilter.CharFilterConstructor(nil, nil)
|
|
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]>")
|
|
titleRe = regexp.MustCompile("[^a-zA-Z0-9 -]+")
|
|
)
|
|
|
|
type rendered struct {
|
|
Index template.HTML
|
|
Text template.HTML
|
|
MD string
|
|
Raw []byte
|
|
}
|
|
|
|
func (r *rendered) Article() *Article {
|
|
return &Article{
|
|
Index: r.Index,
|
|
Text: r.Text,
|
|
MD: r.MD,
|
|
Search: whiteSpace.ReplaceAllString(string(filter.Filter(r.Raw)), " "),
|
|
}
|
|
}
|
|
|
|
func (r *rendered) Snippet() *Snippet {
|
|
return &Snippet{
|
|
Index: r.Index,
|
|
Text: r.Text,
|
|
MD: r.MD,
|
|
}
|
|
}
|
|
|
|
func render(text string) *rendered {
|
|
rend := blackfriday.MarkdownCommon([]byte(text))
|
|
index, html := buildIndex(string(rend))
|
|
html = strings.Replace(html, "<table>", `<table class="table">`, -1)
|
|
html = strings.Replace(html, "</h1>", `</h1><hr>`, -1)
|
|
html = strings.Replace(html, "<a", `<a target="_blank"`, -1)
|
|
return &rendered{
|
|
Index: template.HTML(index),
|
|
Text: template.HTML(html),
|
|
MD: text,
|
|
Raw: rend,
|
|
}
|
|
}
|
|
|
|
func spaceReplace(s string) string {
|
|
return whiteSpace.ReplaceAllString(s, "+")
|
|
//TODO remove
|
|
//return strings.Replace(s, " ", "+", -1)
|
|
}
|
|
|
|
func makeLinkTitle(t string) (newTitle string, title string) {
|
|
title = titleRe.ReplaceAllString(t, "")
|
|
newTitle = spaceReplace(title)
|
|
return
|
|
}
|
|
|
|
func buildIndex(html string) (string, string) {
|
|
var ret string
|
|
m := make(map[string]int)
|
|
hn := reIndex.FindAllStringSubmatch(html, -1)
|
|
html = reHeader.ReplaceAllString(html, `${1} id="${2}">${2}${3}`)
|
|
html = reLink.ReplaceAllStringFunc(html, spaceReplace)
|
|
for k, v := range hn {
|
|
link, _ := makeLinkTitle(v[2])
|
|
i := m[link]
|
|
m[link] = i + 1
|
|
newLink := link + strconv.Itoa(i)
|
|
html = strings.Replace(html, link+`"`, newLink+`"`, 1)
|
|
ret += before(hn, k)
|
|
ret += `<li><a href="#` + newLink + `">` + v[2] + "</a>"
|
|
ret += after(hn, k)
|
|
}
|
|
return ret, html
|
|
}
|
|
|
|
func before(arr [][]string, i int) string {
|
|
c, _ := strconv.Atoi(arr[i][1])
|
|
if c == 1 {
|
|
return `<ul type="circle">`
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func after(arr [][]string, i int) string {
|
|
c, _ := strconv.Atoi(arr[i][1])
|
|
if len(arr) <= i+1 {
|
|
return closeTag(c-1) + "</ul>"
|
|
}
|
|
n, _ := strconv.Atoi(arr[i+1][1])
|
|
if n == 1 {
|
|
return closeTag(c-n) + "</ul>"
|
|
} else if n < c {
|
|
return closeTag(c - n)
|
|
} else if n > c {
|
|
return openTag(n - c)
|
|
} else if n == c {
|
|
return "</li>"
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func openTag(j int) (ret string) {
|
|
for i := 0; i < j; i++ {
|
|
ret += `<ul type="circle"><li>`
|
|
}
|
|
if j > 1 {
|
|
ret = strings.Replace(ret, "circle", "none", j-1)
|
|
}
|
|
ret = ret[0 : len(ret)-4]
|
|
return
|
|
}
|
|
|
|
func closeTag(j int) (ret string) {
|
|
for i := 0; i < j; i++ {
|
|
ret += "</li></ul></li>"
|
|
}
|
|
if j > 1 {
|
|
ret = strings.Replace(ret, "</li></li>", "</li>", -1)
|
|
}
|
|
return
|
|
}
|
|
|
|
func renderResult(res []Result) (html string) {
|
|
//TODO fmt.Sprintf ?
|
|
for r := range res {
|
|
html += `<a href="/` + r.LinkTitle + `"><b>` + r.Title + "</b></a>" +
|
|
`<pre style="white-space: pre-wrap">` + r.Text + "</pre><br>"
|
|
|
|
}
|
|
}
|