some tests and major cleanup
This commit is contained in:
parent
8ef2a2547c
commit
16671b3406
30 changed files with 192057 additions and 202431 deletions
|
|
@ -30,21 +30,17 @@ var (
|
|||
|
||||
func Render(p *core.Page) string {
|
||||
rend := blackfriday.MarkdownCommon([]byte(p.Markdown))
|
||||
index, html := buildTOC(string(rend))
|
||||
toc, html := buildTOC(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)
|
||||
p.PageIndex = template.HTML(index)
|
||||
p.TOC = template.HTML(toc)
|
||||
p.HTML = template.HTML(html)
|
||||
return whiteSpace.ReplaceAllString(string(filter.Filter(rend)), " ")
|
||||
}
|
||||
|
||||
func spaceReplace(s string) string {
|
||||
return whiteSpace.ReplaceAllString(s, "+")
|
||||
}
|
||||
|
||||
func Title(title string) string {
|
||||
return spaceReplace(titleRe.ReplaceAllString(title, ""))
|
||||
return whiteSpace.ReplaceAllString(titleRe.ReplaceAllString(title, ""), "+")
|
||||
}
|
||||
|
||||
func StoreTitle(section, title string) string {
|
||||
|
|
@ -59,7 +55,7 @@ func UnstoreTitle(storeTitle string) (section, title string) {
|
|||
}
|
||||
|
||||
func buildTOC(html string) (string, string) {
|
||||
var ret 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}`)
|
||||
|
|
@ -75,17 +71,17 @@ func buildTOC(html string) (string, string) {
|
|||
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)
|
||||
toc += before(hn, k)
|
||||
toc += `<li><a href="#` + newLink + `">` + v[2] + "</a>"
|
||||
toc += after(hn, k)
|
||||
}
|
||||
return ret, html
|
||||
return toc, html
|
||||
}
|
||||
|
||||
func before(arr [][]string, i int) string {
|
||||
c, _ := strconv.Atoi(arr[i][1])
|
||||
if c == 1 {
|
||||
return `<ul type="circle">`
|
||||
return `<ul class="toc">`
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
|
@ -96,13 +92,14 @@ func after(arr [][]string, i int) string {
|
|||
return closeTag(c-1) + "</ul>"
|
||||
}
|
||||
n, _ := strconv.Atoi(arr[i+1][1])
|
||||
if n == 1 {
|
||||
switch {
|
||||
case n == 1:
|
||||
return closeTag(c-n) + "</ul>"
|
||||
} else if n < c {
|
||||
case n < c:
|
||||
return closeTag(c - n)
|
||||
} else if n > c {
|
||||
case n > c:
|
||||
return openTag(n - c)
|
||||
} else if n == c {
|
||||
case n == c:
|
||||
return "</li>"
|
||||
}
|
||||
return ""
|
||||
|
|
@ -110,11 +107,11 @@ func after(arr [][]string, i int) string {
|
|||
|
||||
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 += `<ul class="toc"><li>`
|
||||
}
|
||||
//if j > 1 {
|
||||
// ret = strings.Replace(ret, "circle", "none", j-1)
|
||||
//}
|
||||
ret = ret[0 : len(ret)-4]
|
||||
return
|
||||
}
|
||||
|
|
|
|||
29
pkg/render/render_test.go
Normal file
29
pkg/render/render_test.go
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// Copyright (C) 2018 Marius Schellenberger
|
||||
|
||||
package render
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTitleFunctions(t *testing.T) {
|
||||
section := "section"
|
||||
title := "title"
|
||||
combined := "section/title"
|
||||
t.Run("StoreTitle", func(t *testing.T) {
|
||||
if StoreTitle(section, title) != combined {
|
||||
t.Fail()
|
||||
}
|
||||
})
|
||||
t.Run("UnstoreTitle", func(t *testing.T) {
|
||||
se, ti := UnstoreTitle(combined)
|
||||
if se != section || ti != title {
|
||||
t.Fail()
|
||||
}
|
||||
})
|
||||
t.Run("Title", func(t *testing.T) {
|
||||
if Title("9ßw$%4h4t-8v74 287(G /SV&3[45v}=94#+?)") != "9w4h4t-8v74+287G+SV345v94" {
|
||||
t.Fail()
|
||||
}
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue