mostly finished db pages
This commit is contained in:
parent
c5d41b0965
commit
38f7c31517
10 changed files with 278 additions and 125 deletions
127
pkg/render/render.go
Normal file
127
pkg/render/render.go
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
// Copyright (C) 2017 Marius Schellenberger
|
||||
|
||||
package render
|
||||
|
||||
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 -]+")
|
||||
)
|
||||
|
||||
func Render(p *Page) string {
|
||||
rend := blackfriday.MarkdownCommon([]byte(p.Markdown))
|
||||
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)
|
||||
p.Index = template.HTML(index)
|
||||
p.Text = template.HTML(html)
|
||||
return whiteSpace.ReplaceAllString(string(filter.Filter(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>"
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue