Skip to content

Commit

Permalink
update:refine the logic of render anchor id
Browse files Browse the repository at this point in the history
  • Loading branch information
luoliwoshang committed Apr 7, 2024
1 parent bdf1365 commit 0bf3991
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 65 deletions.
51 changes: 25 additions & 26 deletions internal/godoc/dochtml/dochtml.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (
"go/doc"
"go/printer"
"go/token"
"regexp"
"sort"
"strconv"
"strings"

"github.com/google/safehtml"
Expand All @@ -38,6 +40,9 @@ var (
// size exceeded the specified limit. See the RenderOptions.Limit field.
ErrTooLarge = errors.New("rendered documentation HTML size exceeded the specified limit")
)
var (
overloadFuncIndexPattern = regexp.MustCompile(`^overload_func_index:(\d+)\n`)
)

// ModuleInfo contains all the information a package needs about the module it
// belongs to in order to render its documentation.
Expand Down Expand Up @@ -141,6 +146,17 @@ type item struct {
// HTML-specific values, for types and functions
Kind string // for data-kind attribute
HeaderClass string // class for header
FuncId string // for functions and methods
}

func overloadFuncIndex(fn *doc.Func) (int, int) {
if match := overloadFuncIndexPattern.FindStringSubmatch(fn.Doc); len(match) == 2 {
valueStr := match[1]
if intValue, err := strconv.Atoi(valueStr); err == nil {
return intValue, len(match[0])
}
}
return -1, -1
}

func packageToItems(p *doc.Package, exmap map[string][]*example) (consts, vars, funcs, types []*item) {
Expand Down Expand Up @@ -178,15 +194,24 @@ func funcsToItems(fs []*doc.Func, hclass, typeName string, exmap map[string][]*e
}
kind := "function"
headerStart := "func"
funcId := fullName
if f.Recv != "" {
kind = "method"
headerStart += " (" + f.Recv + ")"
}
if index, match := overloadFuncIndex(f); index >= 0 {
f.Doc = f.Doc[match:]
if index > 0 {
funcId = fmt.Sprintf("%s__%d", fullName, index)
}
}

i := &item{
Doc: f.Doc,
Decl: f.Decl,
Name: f.Name,
FullName: fullName,
FuncId: funcId,
HeaderStart: headerStart,
IsDeprecated: funcIsDeprecated(f),
Examples: exmap[fullName],
Expand Down Expand Up @@ -286,32 +311,6 @@ func renderInfo(ctx context.Context, fset *token.FileSet, p *doc.Package, opt Re
"file_link": fileLink,
"source_link": sourceLink,
"since_version": sinceVersion,
"render_function_id": func(item *item) string {
fullName := item.Name
overloadIndex := overloadFuncIndex(item)
if item.FullName != "" {
fullName = item.FullName
}
if overloadIndex != -1 {
// The first overloaded function does not require an index to render id
if overloadIndex == 0 {
return fullName
}
return fmt.Sprintf("%s__%d", fullName, overloadIndex)
} else {
return fullName
}
},
"render_gop_decl": func(item *item) (out struct {
Doc safehtml.HTML
Decl safehtml.HTML
}) {
doc := item.Doc
if item.Kind == "method" || item.Kind == "function" {
doc = overloadFuncIndexPattern.ReplaceAllString(doc, "")
}
return r.DeclHTML(doc, item.Decl)
},
}
examples := collectExamples(p)
data := TemplateData{
Expand Down
29 changes: 0 additions & 29 deletions internal/godoc/dochtml/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"go/doc"
"path"
"reflect"
"regexp"
"strconv"
"sync"

"github.com/google/safehtml"
Expand All @@ -24,9 +22,6 @@ var (
// then it becomes more viable to factor out inline CSS style.
bodyTemplate, outlineTemplate, sidenavTemplate *template.Template
)
var (
overloadFuncIndexPattern = regexp.MustCompile(`^overload_func_index:(\d+)\n`)
)

func Templates() []*template.Template {
return []*template.Template{bodyTemplate, outlineTemplate, sidenavTemplate}
Expand All @@ -51,20 +46,6 @@ func LoadTemplates(fsys template.TrustedFS) {
})
}

func overloadFuncIndex(item *item) int {
if item.Kind != "method" && item.Kind != "function" {
return -1
}
match := overloadFuncIndexPattern.FindStringSubmatch(item.Doc)
if len(match) == 2 {
valueStr := match[1]
if intValue, err := strconv.Atoi(valueStr); err == nil {
return intValue
}
}
return -1
}

var tmpl = map[string]any{
"ternary": func(q, a, b any) any {
v := reflect.ValueOf(q)
Expand All @@ -87,14 +68,4 @@ var tmpl = map[string]any{
"since_version": func(string) safehtml.HTML { return safehtml.HTML{} },
"play_url": func(*doc.Example) string { return "" },
"safe_id": render.SafeGoID,
"render_function_id": func(item *item) string { return "" },
"render_gop_decl": func(item *item) (out struct {
Doc safehtml.HTML
Decl safehtml.HTML
}) {
return struct {
Doc safehtml.HTML
Decl safehtml.HTML
}{}
},
}
6 changes: 3 additions & 3 deletions static/doc/body.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

{{- range .Funcs -}}
<li class="Documentation-indexFunction">
<a {{if .IsDeprecated}}class="js-deprecatedTagLink" {{end}}href="#{{render_function_id .}}">{{render_synopsis .Decl}}</a>
<a {{if .IsDeprecated}}class="js-deprecatedTagLink" {{end}}href="#{{.FuncId}}">{{render_synopsis .Decl}}</a>
{{- if .IsDeprecated -}}
<span class="Documentation-indexDeprecated Documentation-deprecatedTag">deprecated</span>
{{- end -}}
Expand All @@ -47,7 +47,7 @@
{{- end -}}
{{- with .Methods -}}
<li><ul class="Documentation-indexTypeMethods">{{"\n" -}}{{range .}}<li>
<a {{if .IsDeprecated}}class="js-deprecatedTagLink" {{end}}href="#{{render_function_id .}}">{{render_synopsis .Decl}}</a>
<a {{if .IsDeprecated}}class="js-deprecatedTagLink" {{end}}href="#{{.FuncId}}">{{render_synopsis .Decl}}</a>
{{- if .IsDeprecated -}}
<span class="Documentation-indexDeprecated Documentation-deprecatedTag">deprecated</span>
{{- end -}}
Expand Down Expand Up @@ -142,7 +142,7 @@
{{define "item"}}
{{$id := safe_id .FullName}}
{{if and (or (eq .Kind "method") (eq .Kind "function"))}}
{{$id = safe_id (render_function_id .)}}
{{$id = safe_id (.FuncId)}}
{{end}}
{{if .IsDeprecated}}
<details class="Documentation-deprecatedDetails js-deprecatedDetails">
Expand Down
4 changes: 2 additions & 2 deletions static/doc/declaration.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{{/* . is internal/godoc/dochtml.item */}}
{{- define "declaration" -}}
{{- $out := render_gop_decl . -}}
{{- $out := render_decl .Doc .Decl -}}
{{if $out.Decl}}
<div class="Documentation-declaration">
<pre>{{- $out.Decl -}}</pre>
Expand All @@ -17,7 +17,7 @@
{{- end -}}

{{- define "declaration-view-source" -}}
{{- $out := render_gop_decl . -}}
{{- $out := render_decl .Doc .Decl -}}
{{if $out.Decl}}
<div class="Documentation-declaration">
<span class="Documentation-declarationLink">{{source_link "View Source" .Decl}}</span>
Expand Down
6 changes: 3 additions & 3 deletions static/doc/outline.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<ul>
{{range .Funcs}}
<li>
<a href="#{{render_function_id .}}" title="{{render_short_synopsis .Decl}}" data-gtmc="doc outline link">
<a href="#{{.FuncId}}" title="{{render_short_synopsis .Decl}}" data-gtmc="doc outline link">
{{render_short_synopsis .Decl}}
</a>
</li>
Expand All @@ -65,15 +65,15 @@
<ul>
{{range .Funcs}}
<li>
<a href="#{{render_function_id .}}" title="{{render_short_synopsis .Decl}}"
<a href="#{{.FuncId}}" title="{{render_short_synopsis .Decl}}"
data-gtmc="doc outline link">
{{render_short_synopsis .Decl}}
</a>
</li>
{{end}}
{{range .Methods}}
<li>
<a href="#{{render_function_id .}}" title="{{render_short_synopsis .Decl}}"
<a href="#{{.FuncId}}" title="{{render_short_synopsis .Decl}}"
data-gtmc="doc outline link">
{{render_short_synopsis .Decl}}
</a>
Expand Down
4 changes: 2 additions & 2 deletions static/doc/sidenav-mobile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
{{$tname := .Name}}
<option value="{{$tname}}">type {{$tname}}</option>
{{range .Funcs}}
<option value="{{render_function_id .}}">{{render_short_synopsis .Decl}}</option>
<option value="{{.FuncId}}">{{render_short_synopsis .Decl}}</option>
{{end}}
{{range .Methods}}
<option value="{{render_function_id .}}">{{render_short_synopsis .Decl}}</option>
<option value="{{.FuncId}}">{{render_short_synopsis .Decl}}</option>
{{end}}
{{end}} {{/* range .Types */}}
</optgroup>
Expand Down

0 comments on commit 0bf3991

Please sign in to comment.