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 } func render(text string) Article { re := regexp.MustCompile("((.+)()") ws := regexp.MustCompile("\\s+") rend := blackfriday.MarkdownCommon([]byte(text)) html := string(rend) index := buildIndex(html) html = strings.Replace(html, "", `
`, -1) html = re.ReplaceAllString(html, `${1} id="${2}">${2}${3}`) html = strings.Replace(html, "", `
`, -1) return Article{ Search: ws.ReplaceAllString(string(filter.Filter(rend)), " "), Index: template.HTML(index), Text: template.HTML(html), MD: text, } } func buildIndex(html string) (ret string) { re := regexp.MustCompile("(.+)") hn := re.FindAllStringSubmatch(html, -1) for k, v := range hn { ret += before(hn, k) ret += `
  • ` + 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, "", "", -1) } return }