Skip to content

Commit 2ccf768

Browse files
committed
internal/godoc/dochmtl: remove unused Render things
- Remove old Render method - Remove unit.tmpl - Re-organize template loading - Rename RenderParts to Render For golang/go#40850 Change-Id: Ie27a54b3553ef100b39d472f52b400f8d8d8392e Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/312649 Trust: Jonathan Amsterdam <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Jamal Carvalho <[email protected]>
1 parent 802d4e6 commit 2ccf768

File tree

5 files changed

+20
-109
lines changed

5 files changed

+20
-109
lines changed

content/static/html/doc/unit.tmpl

Lines changed: 0 additions & 11 deletions
This file was deleted.

internal/godoc/dochtml/dochtml.go

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,6 @@ type templateData struct {
7070
NoteHeaders map[string]noteHeader
7171
}
7272

73-
// Render renders package documentation HTML for the
74-
// provided file set and package.
75-
//
76-
// If the rendered documentation HTML size exceeds the specified limit,
77-
// an error with ErrTooLarge in its chain will be returned.
78-
func Render(ctx context.Context, fset *token.FileSet, p *doc.Package, opt RenderOptions) (_ safehtml.HTML, err error) {
79-
defer derrors.Wrap(&err, "dochtml.Render")
80-
if opt.Limit == 0 {
81-
const megabyte = 1000 * 1000
82-
opt.Limit = 10 * megabyte
83-
}
84-
85-
funcs, data, _ := renderInfo(ctx, fset, p, opt)
86-
p = data.Package
87-
if docIsEmpty(p) {
88-
return safehtml.HTML{}, nil
89-
}
90-
tmpl := template.Must(unitTemplate.Clone()).Funcs(funcs)
91-
return executeToHTMLWithLimit(tmpl, data, opt.Limit)
92-
}
93-
9473
// Parts contains HTML for each part of the documentation.
9574
type Parts struct {
9675
Body safehtml.HTML // main body of doc
@@ -104,7 +83,7 @@ type Parts struct {
10483
//
10584
// If any of the rendered documentation part HTML sizes exceeds the specified limit,
10685
// an error with ErrTooLarge in its chain will be returned.
107-
func RenderParts(ctx context.Context, fset *token.FileSet, p *doc.Package, opt RenderOptions) (_ *Parts, err error) {
86+
func Render(ctx context.Context, fset *token.FileSet, p *doc.Package, opt RenderOptions) (_ *Parts, err error) {
10887
defer derrors.Wrap(&err, "dochtml.RenderParts")
10988

11089
if opt.Limit == 0 {
@@ -117,26 +96,21 @@ func RenderParts(ctx context.Context, fset *token.FileSet, p *doc.Package, opt R
11796
if docIsEmpty(p) {
11897
return &Parts{}, nil
11998
}
120-
tmpl := template.Must(unitTemplate.Clone()).Funcs(funcs)
12199

122-
exec := func(name string) safehtml.HTML {
100+
exec := func(tmpl *template.Template) safehtml.HTML {
123101
if err != nil {
124102
return safehtml.HTML{}
125103
}
126-
t := tmpl.Lookup(name)
127-
if t == nil {
128-
err = fmt.Errorf("missing %s", name)
129-
return safehtml.HTML{}
130-
}
104+
t := template.Must(tmpl.Clone()).Funcs(funcs)
131105
var html safehtml.HTML
132106
html, err = executeToHTMLWithLimit(t, data, opt.Limit)
133107
return html
134108
}
135109

136110
parts := &Parts{
137-
Body: exec("body.tmpl"),
138-
Outline: exec("outline.tmpl"),
139-
MobileOutline: exec("sidenav-mobile.tmpl"),
111+
Body: exec(bodyTemplate),
112+
Outline: exec(outlineTemplate),
113+
MobileOutline: exec(sidenavTemplate),
140114
// links must be called after body, because the call to
141115
// render_doc_extract_links in body.tmpl creates the links.
142116
Links: links(),

internal/godoc/dochtml/dochtml_test.go

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -34,64 +34,12 @@ var (
3434
hasExactText = htmlcheck.HasExactText
3535
)
3636

37-
func TestRender(t *testing.T) {
38-
LoadTemplates(templateSource)
39-
fset, d := mustLoadPackage("everydecl")
40-
41-
rawDoc, err := Render(context.Background(), fset, d, RenderOptions{
42-
FileLinkFunc: func(string) string { return "file" },
43-
SourceLinkFunc: func(ast.Node) string { return "src" },
44-
SinceVersionFunc: func(string) string { return "" },
45-
})
46-
if err != nil {
47-
t.Fatal(err)
48-
}
49-
50-
htmlDoc, err := html.Parse(strings.NewReader(rawDoc.String()))
51-
if err != nil {
52-
t.Fatal(err)
53-
}
54-
// Check that there are no duplicate id attributes.
55-
t.Run("duplicate ids", func(t *testing.T) {
56-
testDuplicateIDs(t, htmlDoc)
57-
})
58-
t.Run("ids-and-kinds", func(t *testing.T) {
59-
// Check that the id and data-kind labels are right.
60-
testIDsAndKinds(t, htmlDoc)
61-
})
62-
63-
checker := in(".Documentation-note",
64-
in("h3", hasAttr("id", "pkg-note-BUG"), hasExactText("Bugs ¶")),
65-
in("a", hasHref("#pkg-note-BUG")))
66-
if err := checker(htmlDoc); err != nil {
67-
t.Errorf("note check: %v", err)
68-
}
69-
70-
checker = in(".Documentation-index",
71-
in(".Documentation-indexNote", in("a", hasHref("#pkg-note-BUG"), hasExactText("Bugs"))))
72-
if err := checker(htmlDoc); err != nil {
73-
t.Errorf("note check: %v", err)
74-
}
75-
76-
checker = in(".DocNav-notes",
77-
in("#nav-group-notes", in("li", in("a", hasHref("#pkg-note-BUG"), hasText("Bugs")))))
78-
if err := checker(htmlDoc); err != nil {
79-
t.Errorf("note check: %v", err)
80-
}
81-
82-
checker = in("",
83-
in("optgroup[label=Notes]", in("option", hasAttr("value", "pkg-note-BUG"), hasExactText("Bugs"))))
84-
if err := checker(htmlDoc); err != nil {
85-
t.Errorf("note check: %v", err)
86-
}
87-
}
88-
8937
func TestRenderParts(t *testing.T) {
9038
LoadTemplates(templateSource)
9139
fset, d := mustLoadPackage("everydecl")
9240

9341
ctx := context.Background()
94-
parts, err := RenderParts(ctx, fset, d, RenderOptions{
42+
parts, err := Render(ctx, fset, d, RenderOptions{
9543
FileLinkFunc: func(string) string { return "file" },
9644
SourceLinkFunc: func(ast.Node) string { return "src" },
9745
SinceVersionFunc: func(string) string { return "" },
@@ -160,15 +108,15 @@ func TestExampleRender(t *testing.T) {
160108
ctx := context.Background()
161109
fset, d := mustLoadPackage("example_test")
162110

163-
rawDoc, err := Render(ctx, fset, d, RenderOptions{
111+
parts, err := Render(ctx, fset, d, RenderOptions{
164112
FileLinkFunc: func(string) string { return "file" },
165113
SourceLinkFunc: func(ast.Node) string { return "src" },
166114
})
167115
if err != nil {
168116
t.Fatal(err)
169117
}
170118

171-
htmlDoc, err := html.Parse(strings.NewReader(rawDoc.String()))
119+
htmlDoc, err := html.Parse(strings.NewReader(parts.Body.String()))
172120
if err != nil {
173121
t.Fatal(err)
174122
}

internal/godoc/dochtml/template.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@ var (
1818

1919
// TODO(golang.org/issue/5060): finalize URL scheme and design for notes,
2020
// then it becomes more viable to factor out inline CSS style.
21-
unitTemplate *template.Template
21+
bodyTemplate, outlineTemplate, sidenavTemplate *template.Template
2222
)
2323

2424
// LoadTemplates reads and parses the templates used to generate documentation.
2525
func LoadTemplates(dir template.TrustedSource) {
2626
loadOnce.Do(func() {
2727
join := template.TrustedSourceJoin
2828
tc := template.TrustedSourceFromConstant
29-
30-
example := join(dir, tc("example.tmpl"))
31-
declaration := join(dir, tc("declaration.tmpl"))
32-
unitTemplate = template.Must(template.New("unit.tmpl").
29+
bodyTemplate = template.Must(template.New("body.tmpl").
3330
Funcs(tmpl).
3431
ParseFilesFromTrustedSources(
35-
join(dir, tc("unit.tmpl")),
36-
join(dir, tc("outline.tmpl")),
37-
join(dir, tc("sidenav-mobile.tmpl")),
3832
join(dir, tc("body.tmpl")),
39-
example,
40-
declaration))
33+
join(dir, tc("declaration.tmpl")),
34+
join(dir, tc("example.tmpl"))))
35+
outlineTemplate = template.Must(template.New("outline.tmpl").
36+
Funcs(tmpl).
37+
ParseFilesFromTrustedSources(join(dir, tc("outline.tmpl"))))
38+
sidenavTemplate = template.Must(template.New("sidenav-mobile.tmpl").
39+
Funcs(tmpl).
40+
ParseFilesFromTrustedSources(join(dir, tc("sidenav-mobile.tmpl"))))
4141
})
4242
}
4343

internal/godoc/render.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func (p *Package) Render(ctx context.Context, innerPath string,
193193
nameToVersion = map[string]string{}
194194
}
195195
opts := p.renderOptions(innerPath, sourceInfo, modInfo, nameToVersion)
196-
parts, err := dochtml.RenderParts(ctx, p.Fset, d, opts)
196+
parts, err := dochtml.Render(ctx, p.Fset, d, opts)
197197
if errors.Is(err, ErrTooLarge) {
198198
return &dochtml.Parts{Body: template.MustParseAndExecuteToHTML(DocTooLargeReplacement)}, nil
199199
}

0 commit comments

Comments
 (0)