diff options
Diffstat (limited to 'cnhttp.go')
-rw-r--r-- | cnhttp.go | 39 |
1 files changed, 34 insertions, 5 deletions
@@ -32,10 +32,12 @@ var ( templates = template.Must(template.New("").Funcs(map[string]interface{}{ "inc": func(s string) string { return "" }, "dec": func() string { return "" }, + "rst": func() string { return "" }, "depth": func() int { return 0 }, - "sanchor": func() string { return "" }, - "lanchor": func() string { return "" }, - "anchor": func() string { return "" }, + "sanchor": func() template.URL { return "" }, + "lanchor": func() template.URL { return "" }, + "ianchor": func() template.URL { return "" }, + "anchor": func() template.URL { return "" }, "lang": func(s string) string { return strings.Map(func(r rune) rune { if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '_' || r == '-' { @@ -437,6 +439,7 @@ func (srv *server) cnpCNMToHTML(w http.ResponseWriter, r *http.Request, req *cnp func (srv *server) addTemplateFuncs(tmpl *template.Template, req *cnp.Request, resp *cnp.Response) *template.Template { sections := []string{} + indices := []int{1} sanchors := map[string]bool{} lanchors := map[string]bool{} anchor := func() template.URL { @@ -449,8 +452,24 @@ func (srv *server) addTemplateFuncs(tmpl *template.Template, req *cnp.Request, r return template.URL(strings.Join(secs, "/")) } return tmpl.Funcs(map[string]interface{}{ - "inc": func(s string) string { sections = append(sections, s); return "" }, - "dec": func() string { sections = sections[:len(sections)-1]; return "" }, + "inc": func(s string) string { + sections = append(sections, s) + indices = append(indices, 1) + return "" + }, + "dec": func() string { + sections = sections[:len(sections)-1] + indices = indices[:len(indices)-1] + indices[len(indices)-1]++ + return "" + }, + "rst": func() string { + sections = []string{} + indices = []int{1} + sanchors = map[string]bool{} + lanchors = map[string]bool{} + return "" + }, "depth": func() int { d := len(sections) + 1 if d > 6 { @@ -459,6 +478,16 @@ func (srv *server) addTemplateFuncs(tmpl *template.Template, req *cnp.Request, r return d }, "anchor": anchor, + "ianchor": func() template.URL { + var buf bytes.Buffer + for i, n := range indices[:len(indices)-1] { + if i != 0 { + buf.WriteByte('.') + } + buf.WriteString(strconv.Itoa(n)) + } + return template.URL(buf.String()) + }, "sanchor": func() template.URL { s := url.PathEscape(sections[len(sections)-1]) if sanchors[s] { |