Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
ad58dc9d
Commit
ad58dc9d
authored
Jul 08, 2011
by
Andrew Gerrand
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
exp/template: the must-have MustParse functions
R=r CC=golang-dev
https://golang.org/cl/4641096
parent
a852981d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
109 additions
and
6 deletions
+109
-6
src/pkg/exp/template/Makefile
src/pkg/exp/template/Makefile
+1
-0
src/pkg/exp/template/helper.go
src/pkg/exp/template/helper.go
+100
-0
src/pkg/exp/template/parse.go
src/pkg/exp/template/parse.go
+7
-5
src/pkg/exp/template/set.go
src/pkg/exp/template/set.go
+1
-1
No files found.
src/pkg/exp/template/Makefile
View file @
ad58dc9d
...
...
@@ -8,6 +8,7 @@ TARG=exp/template
GOFILES
=
\
exec.go
\
funcs.go
\
helper.go
\
lex.go
\
parse.go
\
set.go
\
...
...
src/pkg/exp/template/helper.go
0 → 100644
View file @
ad58dc9d
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Helper functions to make constructing templates and sets easier.
package
template
import
(
"io/ioutil"
"os"
"path/filepath"
)
// MustParse parses the template definition string to construct an internal
// representation of the template for execution.
// It panics if the template cannot be parsed.
func
(
t
*
Template
)
MustParse
(
text
string
)
*
Template
{
if
err
:=
t
.
Parse
(
text
);
err
!=
nil
{
panic
(
err
)
}
return
t
}
// ParseFile reads the template definition from a file and parses it to
// construct an internal representation of the template for execution.
func
(
t
*
Template
)
ParseFile
(
filename
string
)
os
.
Error
{
b
,
err
:=
ioutil
.
ReadFile
(
filename
)
if
err
!=
nil
{
return
err
}
return
t
.
Parse
(
string
(
b
))
}
// MustParseFile reads the template definition from a file and parses it to
// construct an internal representation of the template for execution.
// It panics if the file cannot be read or the template cannot be parsed.
func
(
t
*
Template
)
MustParseFile
(
filename
string
)
*
Template
{
if
err
:=
t
.
ParseFile
(
filename
);
err
!=
nil
{
panic
(
err
)
}
return
t
}
// ParseFile is a helper function that creates a new *Template and parses
// the template definition from the named file.
// The template name is the base name of the file.
func
ParseFile
(
filename
string
)
(
*
Template
,
os
.
Error
)
{
t
:=
New
(
filepath
.
Base
(
filename
))
return
t
,
t
.
ParseFile
(
filename
)
}
// MustParseFile is a helper function that creates a new *Template and parses
// the template definition from the named file.
// The template name is the base name of the file.
// It panics if the file cannot be read or the template cannot be parsed.
func
MustParseFile
(
filename
string
)
*
Template
{
return
New
(
filepath
.
Base
(
filename
))
.
MustParseFile
(
filename
)
}
// MustParse parses a string into a set of named templates.
// It panics if the set cannot be parsed.
func
(
s
*
Set
)
MustParse
(
text
string
)
*
Set
{
if
err
:=
s
.
Parse
(
text
);
err
!=
nil
{
panic
(
err
)
}
return
s
}
// ParseFile parses the named file into a set of named templates.
func
(
s
*
Set
)
ParseFile
(
filename
string
)
os
.
Error
{
b
,
err
:=
ioutil
.
ReadFile
(
filename
)
if
err
!=
nil
{
return
err
}
return
s
.
Parse
(
string
(
b
))
}
// MustParseFile parses the named file into a set of named templates.
// It panics if the file cannot be read or the set cannot be parsed.
func
(
s
*
Set
)
MustParseFile
(
filename
string
)
*
Set
{
if
err
:=
s
.
ParseFile
(
filename
);
err
!=
nil
{
panic
(
err
)
}
return
s
}
// ParseSetFile is a helper function that creates a new *Set and parses
// the set definition from the named file.
func
ParseSetFile
(
filename
string
)
(
*
Set
,
os
.
Error
)
{
s
:=
NewSet
()
return
s
,
s
.
ParseFile
(
filename
)
}
// MustParseSetFile is a helper function that creates a new *Set and parses
// the set definition from the named file.
// It panics if the file cannot be read or the set cannot be parsed.
func
MustParseSetFile
(
filename
string
)
*
Set
{
return
NewSet
()
.
MustParseFile
(
filename
)
}
src/pkg/exp/template/parse.go
View file @
ad58dc9d
...
...
@@ -539,8 +539,8 @@ func (t *Template) atEOF() bool {
return
false
}
// Parse parses the template definition string to construct an internal
representation
// of the template for execution.
// Parse parses the template definition string to construct an internal
//
representation
of the template for execution.
func
(
t
*
Template
)
Parse
(
s
string
)
(
err
os
.
Error
)
{
t
.
startParse
(
nil
,
lex
(
t
.
name
,
s
))
defer
t
.
recover
(
&
err
)
...
...
@@ -549,8 +549,9 @@ func (t *Template) Parse(s string) (err os.Error) {
return
}
// ParseInSet parses the template definition string to construct an internal representation
// of the template for execution. Function bindings are checked against those in the set.
// ParseInSet parses the template definition string to construct an internal
// representation of the template for execution.
// Function bindings are checked against those in the set.
func
(
t
*
Template
)
ParseInSet
(
s
string
,
set
*
Set
)
(
err
os
.
Error
)
{
t
.
startParse
(
set
,
lex
(
t
.
name
,
s
))
defer
t
.
recover
(
&
err
)
...
...
@@ -559,7 +560,8 @@ func (t *Template) ParseInSet(s string, set *Set) (err os.Error) {
return
}
// parse is the helper for Parse. It triggers an error if we expect EOF but don't reach it.
// parse is the helper for Parse.
// It triggers an error if we expect EOF but don't reach it.
func
(
t
*
Template
)
parse
(
toEOF
bool
)
(
next
node
)
{
t
.
root
,
next
=
t
.
itemList
(
true
)
if
toEOF
&&
next
!=
nil
{
...
...
src/pkg/exp/template/set.go
View file @
ad58dc9d
...
...
@@ -81,7 +81,7 @@ func (s *Set) recover(errp *os.Error) {
return
}
// Parse parses
the file
into a set of named templates.
// Parse parses
a string
into a set of named templates.
func
(
s
*
Set
)
Parse
(
text
string
)
(
err
os
.
Error
)
{
defer
s
.
recover
(
&
err
)
lex
:=
lex
(
"set"
,
text
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment