updated dependencies and moved to new rendering

This commit is contained in:
ston1th 2020-11-12 22:54:50 +01:00
commit 0a55030ae5
467 changed files with 64111 additions and 111050 deletions

View file

@ -9,7 +9,6 @@ import (
"github.com/russross/blackfriday"
"html/template"
"regexp"
"strconv"
"strings"
)
@ -23,21 +22,43 @@ func makeFilter() analysis.CharFilter {
}
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 -]+")
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 {
rend := blackfriday.MarkdownCommon([]byte(p.Markdown))
toc, html := buildTOC(string(rend))
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">`)
html = strings.ReplaceAll(html, "</h1>", `</h1><hr>`)
html = strings.ReplaceAll(html, "<a", `<a target="_blank" rel="noopener noreferrer"`)
html = strings.ReplaceAll(html, "\n</code", "</code")
p.TOC = template.HTML(toc)
p.HTML = template.HTML(html)
return whiteSpace.ReplaceAllString(string(filter.Filter(rend)), " ")
}
@ -64,72 +85,3 @@ func SplitStoreTitle(st string) (section, title string) {
}
return
}
func buildTOC(html string) (string, string) {
var toc string
m := make(map[string]int)
hn := reIndex.FindAllStringSubmatch(html, -1)
html = reHeader.ReplaceAllString(html, `<a id="${2}" class="anchor"></a>${1}${2}${3}`)
html = reLink.ReplaceAllStringFunc(html, func(s string) string {
if len(s) >= 3 {
return `id="` + Title(s[2:]) + `"`
}
return s
})
for k, v := range hn {
link := Title(v[2])
i := m[link]
m[link] = i + 1
newLink := link + strconv.Itoa(i)
html = strings.Replace(html, link+`"`, newLink+`"`, 1)
toc += before(hn, k)
toc += `<li><a href="#` + newLink + `">` + v[2] + "</a>"
toc += after(hn, k)
}
return toc, html
}
func before(arr [][]string, i int) string {
c, _ := strconv.Atoi(arr[i][1])
if c == 1 {
return `<ul class="toc">`
}
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])
switch {
case n == 1:
return closeTag(c-n) + "</ul>"
case n < c:
return closeTag(c - n)
case n > c:
return openTag(n - c)
case n == c:
return "</li>"
}
return ""
}
func openTag(j int) (ret string) {
for i := 0; i < j; i++ {
ret += `<ul class="toc"><li>`
}
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.ReplaceAll(ret, "</li></li>", "</li>")
}
return
}