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("((.+)()") reLink = regexp.MustCompile(`id="(.+)">`) whiteSpace = regexp.MustCompile(`\s+`) reIndex = regexp.MustCompile("(.+)") ) func render(text string) Article { rend := blackfriday.MarkdownCommon([]byte(text)) html := string(rend) index := buildIndex(html) html = strings.Replace(html, "", `
`, -1) html = reHeader.ReplaceAllString(html, `${1} id="${2}">${2}${3}`) html = reLink.ReplaceAllStringFunc(html, makeLinkTitle) html = strings.Replace(html, "", `
`, -1) html = strings.Replace(html, "` + v[2] + "" ret += after(hn, k) } return } func before(arr [][]string, i int) string { c, _ := strconv.Atoi(arr[i][1]) if c == 1 { return `
    ` } return "" } func after(arr [][]string, i int) string { c, _ := strconv.Atoi(arr[i][1]) if len(arr) <= i+1 { return closeTag(c-1) + "
" } n, _ := strconv.Atoi(arr[i+1][1]) if n == 1 { return closeTag(c-n) + "" } else if n < c { return closeTag(c - n) } else if n > c { return openTag(n - c) } else if n == c { return "" } return "" } func openTag(j int) (ret string) { for i := 0; i < j; i++ { ret += `
  • ` } 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 += "
" } if j > 1 { ret = strings.Replace(ret, "", "", -1) } return }