Commit ff12f2ef authored by Rob Pike's avatar Rob Pike

add (stub) parser to template code, enabling rewrite.

update pretty to use it.
change stdout to stderr in pretty.

R=rsc
DELTA=173  (52 added, 24 deleted, 97 changed)
OCL=27405
CL=27409
parent c1ed7d7d
...@@ -18,7 +18,7 @@ func HtmlFormatter(w io.Write, value interface{}, format string) { ...@@ -18,7 +18,7 @@ func HtmlFormatter(w io.Write, value interface{}, format string) {
fmt.Fprint(w, value); fmt.Fprint(w, value);
} }
// StringFormatter formats returns the default string representation. // StringFormatter formats into the default string representation.
// It is stored under the name "str" and is the default formatter. // It is stored under the name "str" and is the default formatter.
// You can override the default formatter by storing your default // You can override the default formatter by storing your default
// under the name "" in your custom formatter map. // under the name "" in your custom formatter map.
......
This diff is collapsed.
...@@ -133,6 +133,11 @@ var tests = []*Test { ...@@ -133,6 +133,11 @@ var tests = []*Test {
"Header=77\n" "Header=77\n"
"Header=77\n" "Header=77\n"
}, },
&Test{
"{.section data}{.end} {header}\n",
" Header\n"
},
// Repeated // Repeated
&Test{ &Test{
...@@ -157,12 +162,6 @@ var tests = []*Test { ...@@ -157,12 +162,6 @@ var tests = []*Test {
"Header=77\n" "Header=77\n"
}, },
// Bugs
&Test{
"{.section data}{.end} {integer}\n",
" 77\n"
},
} }
func TestAll(t *testing.T) { func TestAll(t *testing.T) {
...@@ -178,9 +177,14 @@ func TestAll(t *testing.T) { ...@@ -178,9 +177,14 @@ func TestAll(t *testing.T) {
var buf io.ByteBuffer; var buf io.ByteBuffer;
for i, test := range tests { for i, test := range tests {
buf.Reset(); buf.Reset();
err := Execute(test.in, s, formatters, &buf); tmpl, err, line := Parse(test.in, formatters);
if err != nil {
t.Error("unexpected parse error:", err, "line", line);
continue;
}
err = tmpl.Execute(s, &buf);
if err != nil { if err != nil {
t.Error("unexpected error:", err) t.Error("unexpected execute error:", err)
} }
if string(buf.Data()) != test.out { if string(buf.Data()) != test.out {
t.Errorf("for %q: expected %q got %q", test.in, test.out, string(buf.Data())); t.Errorf("for %q: expected %q got %q", test.in, test.out, string(buf.Data()));
...@@ -189,9 +193,12 @@ func TestAll(t *testing.T) { ...@@ -189,9 +193,12 @@ func TestAll(t *testing.T) {
} }
func TestBadDriverType(t *testing.T) { func TestBadDriverType(t *testing.T) {
err := Execute("hi", "hello", nil, os.Stdout); tmpl, err, line := Parse("hi", nil);
if err != nil {
t.Error("unexpected parse error:", err)
}
err = tmpl.Execute("hi", nil);
if err == nil { if err == nil {
t.Error("failed to detect string as driver type") t.Error("failed to detect string as driver type")
} }
var s S;
} }
...@@ -174,7 +174,7 @@ func parse(path string, mode uint) (*ast.Program, errorList) { ...@@ -174,7 +174,7 @@ func parse(path string, mode uint) (*ast.Program, errorList) {
src, err := os.Open(path, os.O_RDONLY, 0); src, err := os.Open(path, os.O_RDONLY, 0);
defer src.Close(); defer src.Close();
if err != nil { if err != nil {
log.Stdoutf("open %s: %v", path, err); log.Stderrf("open %s: %v", path, err);
var noPos token.Position; var noPos token.Position;
return nil, errorList{parseError{noPos, err.String()}}; return nil, errorList{parseError{noPos, err.String()}};
} }
...@@ -242,7 +242,12 @@ func servePage(c *http.Conn, title, content interface{}) { ...@@ -242,7 +242,12 @@ func servePage(c *http.Conn, title, content interface{}) {
d.header = title.(string); d.header = title.(string);
d.timestamp = time.UTC().String(); d.timestamp = time.UTC().String();
d.content = content.(string); d.content = content.(string);
template.Execute(godoc_html, &d, nil, c); templ, err, line := template.Parse(godoc_html, nil);
if err != nil {
log.Stderrf("template error %s:%d: %s\n", title, line, err);
} else {
templ.Execute(&d, c);
}
} }
...@@ -350,7 +355,7 @@ func serveParseErrors(c *http.Conn, filename string, errors errorList) { ...@@ -350,7 +355,7 @@ func serveParseErrors(c *http.Conn, filename string, errors errorList) {
fmt.Fprintf(&b, "<b><font color=red>%s >>></font></b>", e.msg); fmt.Fprintf(&b, "<b><font color=red>%s >>></font></b>", e.msg);
offs = e.pos.Offset; offs = e.pos.Offset;
} else { } else {
log.Stdoutf("error position %d out of bounds (len = %d)", e.pos.Offset, len(src)); log.Stderrf("error position %d out of bounds (len = %d)", e.pos.Offset, len(src));
} }
} }
// TODO handle Write errors // TODO handle Write errors
...@@ -471,13 +476,13 @@ func addDirectory(pmap map[string]*pakDesc, dirname string) { ...@@ -471,13 +476,13 @@ func addDirectory(pmap map[string]*pakDesc, dirname string) {
path := dirname; path := dirname;
fd, err1 := os.Open(path, os.O_RDONLY, 0); fd, err1 := os.Open(path, os.O_RDONLY, 0);
if err1 != nil { if err1 != nil {
log.Stdoutf("open %s: %v", path, err1); log.Stderrf("open %s: %v", path, err1);
return; return;
} }
list, err2 := fd.Readdir(-1); list, err2 := fd.Readdir(-1);
if err2 != nil { if err2 != nil {
log.Stdoutf("readdir %s: %v", path, err2); log.Stderrf("readdir %s: %v", path, err2);
return; return;
} }
...@@ -626,7 +631,7 @@ func installHandler(prefix string, handler func(c *http.Conn, path string)) { ...@@ -626,7 +631,7 @@ func installHandler(prefix string, handler func(c *http.Conn, path string)) {
f := func(c *http.Conn, req *http.Request) { f := func(c *http.Conn, req *http.Request) {
path := req.Url.Path; path := req.Url.Path;
if *verbose { if *verbose {
log.Stdoutf("%s\t%s", req.Host, path); log.Stderrf("%s\t%s", req.Host, path);
} }
handler(c, path[len(prefix) : len(path)]); handler(c, path[len(prefix) : len(path)]);
}; };
...@@ -663,9 +668,9 @@ func main() { ...@@ -663,9 +668,9 @@ func main() {
if *httpaddr != "" { if *httpaddr != "" {
if *verbose { if *verbose {
log.Stdoutf("Go Documentation Server\n"); log.Stderrf("Go Documentation Server\n");
log.Stdoutf("address = %s\n", *httpaddr); log.Stderrf("address = %s\n", *httpaddr);
log.Stdoutf("goroot = %s\n", goroot); log.Stderrf("goroot = %s\n", goroot);
} }
installHandler("/mem", makeFixedFileServer("doc/go_mem.html")); installHandler("/mem", makeFixedFileServer("doc/go_mem.html"));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment