From c626ecdc562f99fbaf2130f7136d7f9b0ea7b58a Mon Sep 17 00:00:00 2001 From: clsr Date: Fri, 25 Aug 2017 16:02:01 +0200 Subject: Implement draft/cnm-selector hash fragment anchors --- cnhttp.go | 39 ++++++++++++++++++++++++++++++++++----- templates/content.html | 14 +++++++++----- templates/page.html | 7 ++++++- 3 files changed, 49 insertions(+), 11 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] { 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 -}} {{- with .Title -}} - {{- if $l := lanchor -}} - {{.}} - {{- if $s := sanchor}}{{if ne $s $l}}{{end}}{{end -}} - - {{- end -}} + {{.}} + {{- if $l := lanchor -}} + + {{- if $s := sanchor}}{{if ne $s $l}}{{end}}{{end -}} + + {{- else -}} + + {{- end -}} + {{- end -}} {{- 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 -}}
  • - {{.Title}} + {{- if $l := lanchor -}} + {{.Title}} + {{- else -}} + {{.Title}} + {{- end -}} {{- with .Children -}} -- cgit