From 2e394c51d9453c2f94c19f444e2f056ba50a22af Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Thu, 11 Aug 2011 13:24:13 -0700 Subject: [PATCH] godoc: moving to new template package - first step; rough conversion of all template files - there is plenty of opportunity for cleanups/simplifications (next CLs) - html and text output as before R=r, dsymonds CC=golang-dev https://golang.org/cl/4852048 --- lib/godoc/codewalk.html | 24 +++--- lib/godoc/codewalkdir.html | 8 +- lib/godoc/dirlist.html | 11 +-- lib/godoc/error.html | 2 +- lib/godoc/godoc.html | 48 +++++------ lib/godoc/package.html | 169 +++++++++++++++++++------------------ lib/godoc/package.txt | 145 +++++++++++++++---------------- lib/godoc/search.html | 116 +++++++++++++------------ lib/godoc/search.txt | 82 +++++++----------- src/cmd/godoc/codewalk.go | 2 +- src/cmd/godoc/format.go | 2 +- src/cmd/godoc/godoc.go | 54 +++++++----- 12 files changed, 323 insertions(+), 340 deletions(-) diff --git a/lib/godoc/codewalk.html b/lib/godoc/codewalk.html index 47f8a22e2b..64d2d32253 100644 --- a/lib/godoc/codewalk.html +++ b/lib/godoc/codewalk.html @@ -18,9 +18,9 @@ Pop Out Code
@@ -35,19 +35,19 @@
- {.repeated section Step} + {{range .Step}}
- -
{Title|html-esc}
+ +
{{.Title|html_esc}}
- {.section Err} - ERROR LOADING FILE: {Err|html-esc}

- {.end} - {XML} + {{with .Err}} + ERROR LOADING FILE: {{.|html_esc}}

+ {{end}} + {{.XML}}
-
{@|html-esc}
+
{{.|html_esc}}
- {.end} + {{end}}
previous step diff --git a/lib/godoc/codewalkdir.html b/lib/godoc/codewalkdir.html index 61a9a8b3b4..ebfc19aa6b 100644 --- a/lib/godoc/codewalkdir.html +++ b/lib/godoc/codewalkdir.html @@ -5,11 +5,11 @@ --> -{.repeated section @} +{{range .}} - + - + -{.end} +{{end}}
{Name|html-esc}{{.Name|html_esc}}  {Title|html-esc}{{.Title|html_esc}}
diff --git a/lib/godoc/dirlist.html b/lib/godoc/dirlist.html index 29b4b24357..7e7d4e7f29 100644 --- a/lib/godoc/dirlist.html +++ b/lib/godoc/dirlist.html @@ -16,14 +16,15 @@ .. -{.repeated section @} +{{range .}} - {@|fileInfoName} + {{.|fileInfoName}} - {@|fileInfoSize} + {{.|fileInfoSize}} - {@|fileInfoTime} + {{.|fileInfoTime}} -{.end} +{{end}} +

diff --git a/lib/godoc/error.html b/lib/godoc/error.html index c14c574057..d349f754fd 100644 --- a/lib/godoc/error.html +++ b/lib/godoc/error.html @@ -5,5 +5,5 @@ -->

-{@|html-esc} +{{.|html_esc}}

diff --git a/lib/godoc/godoc.html b/lib/godoc/godoc.html index bd53f2615e..37db860b20 100644 --- a/lib/godoc/godoc.html +++ b/lib/godoc/godoc.html @@ -2,11 +2,11 @@ -{.section Title} - {@|html-esc} - The Go Programming Language -{.or} +{{with .Title}} + {{.|html_esc}} - The Go Programming Language +{{else}} The Go Programming Language -{.end} +{{end}} - {.section Menu} + {{with .Menu}} - {.end} + {{end}} - {.section Title} -

{@|html-esc}

- {.end} - {.section Subtitle} - {@|html-esc} - {.end} + {{with .Title}} +

{{.|html_esc}}

+ {{end}} + {{with .Subtitle}} + {{.|html_esc}} + {{end}} - {Content} + {{printf "%s" .Content}}
-

Build version {Version|html-esc}. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.

+

Build version {{.Version|html_esc}}. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.

diff --git a/lib/godoc/package.html b/lib/godoc/package.html index cb3ffa2a75..5a7438b017 100644 --- a/lib/godoc/package.html +++ b/lib/godoc/package.html @@ -3,119 +3,120 @@ Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. --> - -{.section PAst} -
{@ FSet|html-esc}
-{.end} -{.section PDoc} +{{$FSet := .FSet}} +{{$Info := .}} +{{with .PAst}} +
{{html_esc . $FSet}}
+{{end}} +{{with .PDoc}} - {.section IsPkg} - {# ImportPath is a string - no need for FSet} -

import "{ImportPath|html-esc}"

- {.end} - {Doc|html-comment} - {.section IsPkg} - {.section Filenames} - {# Filenames are strings - no need for FSet} + {{if $Info.IsPkg}} + {{/* ImportPath is a string - no need for FSet */}} +

import "{{.ImportPath|html_esc}}"

+ {{end}} + {{.Doc|html_comment}} + {{if $Info.IsPkg}} + {{with .Filenames}} + {{/* Filenames are strings - no need for FSet */}}

Package files

- {.repeated section @} - {@|localname} - {.end} + {{range .}} + {{.|localname}} + {{end}}

- {.end} - {.end} - {.section Consts} + {{end}} + {{end}} + {{with .Consts}}

Constants

- {.repeated section @} - {Doc|html-comment} -
{Decl FSet|html-esc}
- {.end} - {.end} - {.section Vars} + {{range .}} + {{html_comment .Doc}} +
{{html_esc .Decl $FSet}}
+ {{end}} + {{end}} + {{with .Vars}}

Variables

- {.repeated section @} - {Doc|html-comment} -
{Decl FSet|html-esc}
- {.end} - {.end} - {.section Funcs} - {.repeated section @} - {# Name is a string - no need for FSet} -

func {Name|html-esc}

-

{Decl FSet|html-esc}

- {Doc|html-comment} - {.end} - {.end} - {.section Types} - {.repeated section @} -

type {Type.Name FSet|html-esc}

- {Doc|html-comment} -

{Decl FSet|html-esc}

- {.repeated section Consts} - {Doc|html-comment} -
{Decl FSet|html-esc}
- {.end} - {.repeated section Vars} - {Doc|html-comment} -
{Decl FSet|html-esc}
- {.end} - {.repeated section Factories} -

func {Name|html-esc}

-

{Decl FSet|html-esc}

- {Doc|html-comment} - {.end} - {.repeated section Methods} -

func ({Recv FSet|html-esc}) {Name|html-esc}

-

{Decl FSet|html-esc}

- {Doc|html-comment} - {.end} - {.end} - {.end} - {.section Bugs} + {{range .}} + {{html_comment .Doc}} +
{{html_esc .Decl $FSet}}
+ {{end}} + {{end}} + {{with .Funcs}} + {{range .}} + {{/* Name is a string - no need for FSet */}} +

func {{.Name|html_esc}}

+

{{html_esc .Decl $FSet}}

+ {{.Doc|html_comment}} + {{end}} + {{end}} + {{with .Types}} + {{range $type := .}} +

type {{html_esc .Type.Name $FSet}}

+ {{.Doc|html_comment}} +

{{html_esc .Decl $FSet}}

+ {{range .Consts}} + {{.Doc|html_comment}} +
{{html_esc .Decl $FSet}}
+ {{end}} + {{range .Vars}} + {{.Doc|html_comment}} +
{{html_esc .Decl $FSet}}
+ {{end}} + {{range .Factories}} +

func {{.Name|html_esc}}

+

{{html_esc .Decl $FSet}}

+ {{.Doc|html_comment}} + {{end}} + {{range .Methods}} +

func ({{html_esc .Recv $FSet}}) {{.Name|html_esc}}

+

{{html_esc .Decl $FSet}}

+ {{.Doc|html_comment}} + {{end}} + {{end}} + {{end}} + {{with .Bugs}}

Bugs

- {.repeated section @} - {@|html-comment} - {.end} - {.end} -{.end} -{.section PList} + {{range .}} + {{.|html_comment}} + {{end}} + {{end}} +{{end}} +{{with .PList}}

Other packages

- {# PLIst entries are strings - no need for FSet} - {.repeated section @} - {@|html-esc}
- {.end} + {{/* PList entries are strings - no need for FSet */}} + {{range .}} + {{html .}}
+ {{end}}

-{.end} -{.section Dirs} +{{end}} +{{with .Dirs}}

Need more packages? The Package Dashboard provides a list of goinstallable packages.

- {# DirList entries are numbers and strings - no need for FSet} + {{/* DirList entries are numbers and strings - no need for FSet */}}

Subdirectories

- + - {.repeated section List} + {{range .List}} - {Depth|padding} - + {{.Depth|padding}} + - + - {.end} + {{end}}
NameName   Synopsis
..
{Name|html-esc}{{html .Name}} {Synopsis|html-esc}{{html .Synopsis}}

-{.end} +{{end}} diff --git a/lib/godoc/package.txt b/lib/godoc/package.txt index 6fe992dbe4..24806661ac 100644 --- a/lib/godoc/package.txt +++ b/lib/godoc/package.txt @@ -1,95 +1,90 @@ -{.section PAst} -{@ FSet} -{.end} -{.section PDoc} -{.section IsPkg} -PACKAGE - -package {PackageName} -import "{ImportPath}" -{.or} -COMMAND DOCUMENTATION -{.end} -{.section Doc} - -{@ FSet} -{.end} -{.section Consts} +{{$FSet := .FSet}}{{$Info := .}}{{/* +--------------------------------------- + +*/}}{{with .PAst}}{{text . $FSet}}{{end}}{{/* + +--------------------------------------- + +*/}}{{with .PDoc}}{{if $Info.IsPkg}}PACKAGE + +package {{.PackageName}} +import "{{.ImportPath}}" + +{{else}}COMMAND DOCUMENTATION +{{end}}{{/* + +--------------------------------------- + +*/}}{{with .Doc}}{{text . $FSet}} +{{end}}{{/* + +--------------------------------------- + +*/}}{{with .Consts}} CONSTANTS -{.repeated section @} -{Decl FSet} -{Doc} -{.end} -{.end} -{.section Vars} +{{range .}}{{text .Decl $FSet}} +{{.Doc}}{{end}} +{{end}}{{/* + +--------------------------------------- +*/}}{{with .Vars}} VARIABLES -{.repeated section @} -{Decl FSet} -{Doc} -{.end} -{.end} -{.section Funcs} +{{range .}}{{.Decl $FSet}} +{{.Doc}}{{end}} +{{end}}{{/* +--------------------------------------- + +*/}}{{with .Funcs}} FUNCTIONS -{.repeated section @} -{Decl FSet} -{Doc} -{.end} -{.end} -{.section Types} +{{range .}}{{text .Decl $FSet}} +{{.Doc}} +{{end}}{{end}}{{/* -TYPES +--------------------------------------- -{.repeated section @} -{Decl FSet} -{Doc} -{.repeated section Consts} -{Decl FSet} -{Doc} -{.end} -{.repeated section Vars} -{Decl FSet} -{Doc} -{.end} -{.repeated section Factories} -{Decl FSet} -{Doc} -{.end} -{.repeated section Methods} -{Decl FSet} -{Doc} -{.end} -{.end} -{.end} -{.section Bugs} +*/}}{{with .Types}} +TYPES +{{range .}}{{text .Decl $FSet}} +{{.Doc}} +{{range .Consts}}{{text .Decl $FSet}} +{{.Doc}} +{{end}}{{range .Vars}}{{text .Decl $FSet}} +{{.Doc}} +{{end}}{{range .Factories}}{{text .Decl $FSet}} +{{.Doc}} +{{end}}{{range .Methods}}{{text .Decl $FSet}} +{{.Doc}} +{{end}}{{end}}{{end}}{{/* + +--------------------------------------- + +*/}}{{with .Bugs}} BUGS -{.repeated section @} -{@} -{.end} -{.end} -{.end} -{.section PList} +{{range .}}{{.}} +{{end}}{{end}}{{end}}{{/* + +--------------------------------------- +*/}}{{with .PList}} OTHER PACKAGES -{.repeated section @} -{@} -{.end} -{.end} -{.section Dirs} -{.section Dirs} +{{range .}} +{{.}}{{end}} +{{end}}{{/* + +--------------------------------------- +*/}}{{with .Dirs}} SUBDIRECTORIES -{.repeated section List} - {Name} -{.end} -{.end} -{.end} +{{range .List}} + {{.Name}}{{end}} +{{end}} diff --git a/lib/godoc/search.html b/lib/godoc/search.html index 58a933fef0..94acb837b2 100644 --- a/lib/godoc/search.html +++ b/lib/godoc/search.html @@ -3,93 +3,91 @@ Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. --> - -{.section Alert} +{{$SearchResult := .}} +{{with .Alert}}

- {@} + {{.}}

-{.end} -{.section Alt} +{{end}} +{{with .Alt}}

Did you mean: - {.repeated section Alts} - {@|html-esc} - {.end} + {{range .Alts}} + {{.|html_esc}} + {{end}}

-{.end} -{.section Hit} - {.section Decls} +{{end}} +{{with .Hit}} + {{with .Decls}}

Package-level declarations

- {.repeated section @} -

package {Pak.Name|html-esc}

- {.repeated section Files} - {.repeated section Groups} - {.repeated section Infos} - {File.Path|url-src}:{@|infoLine} - {@|infoSnippet} - {.end} - {.end} - {.end} - {.end} - {.end} - {.section Others} + {{range .}} +

package {{.Pak.Name|html_esc}}

+ {{range $File := .Files}} + {{range .Groups}} + {{range .Infos}} + {{$File.File.Path|url_src}}:{{.|infoLine}} + {{.|infoSnippet}} + {{end}} + {{end}} + {{end}} + {{end}} + {{end}} + {{with .Others}}

Local declarations and uses

- {.repeated section @} -

package {Pak.Name|html-esc}

- {.repeated section Files} - {File.Path|url-src} + {{range .}} +

package {{.Pak.Name|html_esc}}

+ {{range $File := .Files}} + {{.File.Path|url_src}} - {.repeated section Groups} + {{range .Groups}} - + - {.end} + {{end}}
{Kind|infoKind}{{.Kind|infoKind}} - {.repeated section Infos} - {@|infoLine} - {.end} + {{range .Infos}} + {{.|infoLine}} + {{end}}
- {.end} - {.end} - {.end} -{.end} -{.section Textual} - {.section Complete} -

{Found|html-esc} textual occurrences

- {.or} -

More than {Found|html-esc} textual occurrences

+ {{end}} + {{end}} + {{end}} +{{end}} +{{with .Textual}} + {{if $SearchResult.Complete}} +

{{$SearchResult.Found|html_esc}} textual occurrences

+ {{else}} +

More than {{$SearchResult.Found|html_esc}} textual occurrences

- Not all files or lines containing "{Query|html-esc}" are shown. + Not all files or lines containing "{{$SearchResult.Query|html_esc}}" are shown.

- {.end} + {{end}}

- {.repeated section @} + {{range $File := .}} - + - {.end} - {.section Complete} - {.or} + {{end}} + {{if not $SearchResult.Complete}} - {.end} + {{end}}
- {Filename|url-src}: + {{.Filename|url_src}}: {Lines|numlines}{{.Lines|numlines}} - {.repeated section Lines} - {@|html-esc} - {.end} - {.section Complete} - {.or} + {{range .Lines}} + {{.|html_esc}} + {{end}} + {{if not $SearchResult.Complete}} ... - {.end} + {{end}}
...

-{.end} +{{end}} diff --git a/lib/godoc/search.txt b/lib/godoc/search.txt index 967c1ac015..81318cb641 100644 --- a/lib/godoc/search.txt +++ b/lib/godoc/search.txt @@ -1,61 +1,39 @@ QUERY - {Query} + {{.Query}} +{{with .Alert}} +{{.}} +{{end}}{{/* .Alert */}}{{/* -{.section Alert} -{@} +--------------------------------------- -{.end} -{.section Alt} +*/}}{{with .Alt}} DID YOU MEAN -{.repeated section Alts} - {@} -{.end} +{{range .Alts}} {{.}} +{{end}}{{end}}{{/* .Alts */}}{{/* -{.end} -{.section Hit} -{.section Decls} +--------------------------------------- + +*/}}{{with .Hit}}{{with .Decls}} PACKAGE-LEVEL DECLARATIONS -{.repeated section @} -package {Pak.Name} -{.repeated section Files} -{.repeated section Groups} -{.repeated section Infos} - {File.Path|url-src}:{@|infoLine} -{.end} -{.end} -{.end} - -{.end} -{.end} -{.section Others} +{{range .}}package {{.Pak.Name}} +{{range $File := .Files}}{{range .Groups}}{{range .Infos}} {{$File.File.Path|url_src}}:{{.|infoLine}}{{end}} +{{end}}{{end}}{{/* .Files */}}{{end}}{{end}}{{/* .Decls */}}{{/* + +--------------------------------------- + +*/}}{{with .Others}} LOCAL DECLARATIONS AND USES -{.repeated section @} -package {Pak.Name} -{.repeated section Files} -{.repeated section Groups} -{.repeated section Infos} - {File.Path|url-src}:{@|infoLine} -{.end} -{.end} -{.end} - -{.end} -{.end} -{.end} -{.section Textual} -{.section Complete} -{Found} TEXTUAL OCCURRENCES -{.or} -MORE THAN {Found} TEXTUAL OCCURRENCES -{.end} - -{.repeated section @} -{Lines|numlines} {Filename|url-src} -{.end} -{.section Complete} -{.or} -... ... -{.end} -{.end} +{{range .}}package {{.Pak.Name}} +{{range $File := .Files}}{{range .Groups}}{{range .Infos}} {{$File.File.Path|url_src}}:{{.|infoLine}} +{{end}}{{end}}{{end}}{{/* .Files */}} +{{end}}{{end}}{{/* .Others */}}{{end}}{{/* .Hit */}}{{/* + +--------------------------------------- + +*/}}{{if .Textual}}{{if .Complete}}{{.Found}} TEXTUAL OCCURRENCES{{else}}MORE THAN {{.Found}} TEXTUAL OCCURRENCES{{end}} + +{{range .Textual}}{{.Lines|numlines}} {{.Filename|url_src}} +{{end}}{{if not .Complete}}... ... +{{end}}{{end}} diff --git a/src/cmd/godoc/codewalk.go b/src/cmd/godoc/codewalk.go index e2643e4662..2e14186b0b 100644 --- a/src/cmd/godoc/codewalk.go +++ b/src/cmd/godoc/codewalk.go @@ -14,6 +14,7 @@ package main import ( "container/vector" + "exp/template" "fmt" "http" "io" @@ -23,7 +24,6 @@ import ( "sort" "strconv" "strings" - "template" "utf8" "xml" ) diff --git a/src/cmd/godoc/format.go b/src/cmd/godoc/format.go index 78dde4166f..69fd6ba0c0 100644 --- a/src/cmd/godoc/format.go +++ b/src/cmd/godoc/format.go @@ -11,13 +11,13 @@ package main import ( + "exp/template" "fmt" "go/scanner" "go/token" "io" "regexp" "strconv" - "template" ) // ---------------------------------------------------------------------------- diff --git a/src/cmd/godoc/godoc.go b/src/cmd/godoc/godoc.go index 03ac1b98b7..87f8b61fae 100644 --- a/src/cmd/godoc/godoc.go +++ b/src/cmd/godoc/godoc.go @@ -6,6 +6,7 @@ package main import ( "bytes" + "exp/template" "flag" "fmt" "go/ast" @@ -23,7 +24,6 @@ import ( "runtime" "sort" "strings" - "template" "time" ) @@ -481,16 +481,16 @@ func urlFmt(w io.Writer, format string, x ...interface{}) { // and assume the url-pkg format instead log.Printf("INTERNAL ERROR: urlFmt(%s)", format) fallthrough - case "url-pkg": + case "url_pkg": // because of the irregular mapping under goroot // we need to correct certain relative paths if strings.HasPrefix(relpath, "src/pkg/") { relpath = relpath[len("src/pkg/"):] } template.HTMLEscape(w, []byte(pkgHandler.pattern[1:]+relpath)) // remove trailing '/' for relative URL - case "url-src": + case "url_src": template.HTMLEscape(w, []byte(relpath)) - case "url-pos": + case "url_pos": template.HTMLEscape(w, []byte(relpath)) // selection ranges are of form "s=low:high" if low < high { @@ -600,14 +600,32 @@ func numlinesFmt(w io.Writer, format string, x ...interface{}) { fmt.Fprintf(w, "%d", len(list)) } -var fmap = template.FormatterMap{ - "": textFmt, - "html-esc": htmlEscFmt, - "html-comment": htmlCommentFmt, - "urlquery-esc": urlQueryEscFmt, - "url-pkg": urlFmt, - "url-src": urlFmt, - "url-pos": urlFmt, +// TODO(gri): Remove this type once fmtMap2funcMap is gone. +type FormatterMap map[string]func(io.Writer, string, ...interface{}) + +// TODO(gri): Remove the need for this conversion function by rewriting +// the old template formatters into new template functions. +func fmtMap2funcMap(fmtMap FormatterMap) template.FuncMap { + funcMap := make(template.FuncMap) + for n, f := range fmtMap { + name, fmt := n, f // separate instance of name, fmt for each closure! + funcMap[name] = func(args ...interface{}) string { + var buf bytes.Buffer + fmt(&buf, name, args...) + return buf.String() + } + } + return funcMap +} + +var fmap = fmtMap2funcMap(FormatterMap{ + "text": textFmt, + "html_esc": htmlEscFmt, + "html_comment": htmlCommentFmt, + "urlquery_esc": urlQueryEscFmt, + "url_pkg": urlFmt, + "url_src": urlFmt, + "url_pos": urlFmt, "infoKind": infoKindFmt, "infoLine": infoLineFmt, "infoSnippet": infoSnippetFmt, @@ -617,7 +635,7 @@ var fmap = template.FormatterMap{ "fileInfoTime": fileInfoTimeFmt, "localname": localnameFmt, "numlines": numlinesFmt, -} +}) func readTemplate(name string) *template.Template { path := filepath.Join(*goroot, "lib", "godoc", name) @@ -629,15 +647,7 @@ func readTemplate(name string) *template.Template { path = defaultpath } } - data, err := fs.ReadFile(path) - if err != nil { - log.Fatalf("ReadFile %s: %v", path, err) - } - t, err := template.Parse(string(data), fmap) - if err != nil { - log.Fatalf("%s: %v", name, err) - } - return t + return template.Must(template.New(name).Funcs(fmap).ParseFile(path)) } var ( -- 2.50.0