major changes

This commit is contained in:
ston1th 2018-02-07 21:41:54 +01:00
commit 2b56832843
83 changed files with 7760 additions and 1244 deletions

View file

@ -25,38 +25,74 @@ var (
reLink = regexp.MustCompile(`id="(.+)">`)
whiteSpace = regexp.MustCompile(`\s+`)
reIndex = regexp.MustCompile("<h([1-6])>(.+)</h[1-6]>")
titleRe = regexp.MustCompile("[^a-zA-Z0-9 -]+")
)
func render(text string) Article {
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))
html := string(rend)
index := buildIndex(html)
index, html := buildIndex(string(rend))
html = strings.Replace(html, "<table>", `<table class="table">`, -1)
html = reHeader.ReplaceAllString(html, `${1} id="${2}">${2}${3}`)
html = reLink.ReplaceAllStringFunc(html, makeLinkTitle)
html = strings.Replace(html, "</h1>", `</h1><hr>`, -1)
html = strings.Replace(html, "<a", `<a target="_blank"`, -1)
return Article{
Search: whiteSpace.ReplaceAllString(string(filter.Filter(rend)), " "),
Index: template.HTML(index),
Text: template.HTML(html),
MD: text,
return &rendered{
Index: template.HTML(index),
Text: template.HTML(html),
MD: text,
Raw: rend,
}
}
func makeLinkTitle(title string) string {
return strings.Replace(title, " ", "+", -1)
func spaceReplace(s string) string {
return strings.Replace(s, " ", "+", -1)
}
func buildIndex(html string) (ret string) {
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])
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="#` + link + `">` + v[2] + "</a>"
ret += `<li><a href="#` + newLink + `">` + v[2] + "</a>"
ret += after(hn, k)
}
return
return ret, html
}
func before(arr [][]string, i int) string {