diff options
author | clsr <clsr@clsr.net> | 2017-08-25 16:02:01 +0200 |
---|---|---|
committer | clsr <clsr@clsr.net> | 2017-08-25 16:02:06 +0200 |
commit | c626ecdc562f99fbaf2130f7136d7f9b0ea7b58a (patch) | |
tree | bb1c5ee6b1fcc19d195f6ebca05e226be00cd1a6 | |
parent | ed64d2fb11b4833a8dcbe29225f90bf6462dc8ad (diff) | |
download | cn-http-0.1.5.tar.gz cn-http-0.1.5.zip |
Implement draft/cnm-selector hash fragment anchorsv0.1.5
-rw-r--r-- | cnhttp.go | 39 | ||||
-rw-r--r-- | templates/content.html | 14 | ||||
-rw-r--r-- | templates/page.html | 7 |
3 files changed, 49 insertions, 11 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] { diff --git a/templates/content.html b/templates/content.html index 4616a10..9b6d508 100644 --- a/templates/content.html +++ b/templates/content.html @@ -4,11 +4,15 @@ {{- inc .Title -}} <summary> {{- with .Title -}} - {{- if $l := lanchor -}} - <h{{depth}}>{{.}}<a class="sec-link" id="/{{$l}}" href="#/{{$l}}">¶</a> - {{- if $s := sanchor}}{{if ne $s $l}}<a id="#{{$s}}"></a>{{end}}{{end -}} - </h{{depth}}> - {{- end -}} + <h{{depth}}>{{.}} + {{- if $l := lanchor -}} + <a class="sec-link" id="/{{$l}}" href="#/{{$l}}">¶</a> + {{- if $s := sanchor}}{{if ne $s $l}}<a id="#{{$s}}"></a>{{end}}{{end -}} + <a id="${{ianchor}}"></a> + {{- else -}} + <a class="sec-link" id="${{ianchor}}" href="#${{ianchor}}">¶</a> + {{- end -}} + </h{{depth}}> {{- end -}} </summary> {{- range .Children -}} diff --git a/templates/page.html b/templates/page.html index 0f89e2d..4a356cb 100644 --- a/templates/page.html +++ b/templates/page.html @@ -60,7 +60,11 @@ {{- block "toc" . -}} {{- inc .Title -}} <li> - <a href="#/{{anchor}}">{{.Title}}</a> + {{- if $l := lanchor -}} + <a href="#/{{$l}}">{{.Title}}</a> + {{- else -}} + <a href="#${{ianchor}}">{{.Title}}</a> + {{- end -}} {{- with .Children -}} <ul> {{- range . -}} @@ -72,6 +76,7 @@ {{- dec -}} {{- end -}} {{- end -}} + {{- rst -}} </ul> </details> </nav> |