summaryrefslogtreecommitdiffstats
path: root/cnhttp.go
diff options
context:
space:
mode:
authorclsr <clsr@clsr.net>2017-08-25 16:02:01 +0200
committerclsr <clsr@clsr.net>2017-08-25 16:02:06 +0200
commitc626ecdc562f99fbaf2130f7136d7f9b0ea7b58a (patch)
treebb1c5ee6b1fcc19d195f6ebca05e226be00cd1a6 /cnhttp.go
parented64d2fb11b4833a8dcbe29225f90bf6462dc8ad (diff)
downloadcn-http-0.1.5.tar.gz
cn-http-0.1.5.zip
Implement draft/cnm-selector hash fragment anchorsv0.1.5
Diffstat (limited to 'cnhttp.go')
-rw-r--r--cnhttp.go39
1 files changed, 34 insertions, 5 deletions
diff --git a/cnhttp.go b/cnhttp.go
index 24b3bb5..c9732d5 100644
--- a/cnhttp.go
+++ b/cnhttp.go
@@ -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] {