Skip to content

Commit

Permalink
fix the base dir function's bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yosssi committed May 7, 2014
1 parent 4b59a56 commit 1375147
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
12 changes: 6 additions & 6 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,15 @@ func (e *Element) Html(bf *bytes.Buffer, stringTemplates map[string]string) erro
g := tpl.Generator
var incTpl *Template
var err error
addBaseDir := true
if stringTemplates == nil {
if CurrentDirectoryBasedPath(incTplPath) {
incTplPath = tpl.Dir() + incTplPath + goldExtension
} else {
incTplPath = Path(g.baseDir, incTplPath+goldExtension)
if g.baseDir != "" && CurrentDirectoryBasedPath(incTplPath) {
incTplPath = tpl.Dir() + incTplPath
addBaseDir = false
}
incTpl, err = g.parse(incTplPath, nil)
incTpl, err = g.parse(incTplPath+goldExtension, nil, addBaseDir)
} else {
incTpl, err = g.parse(incTplPath, stringTemplates)
incTpl, err = g.parse(incTplPath, stringTemplates, addBaseDir)
}
if err != nil {
return err
Expand Down
36 changes: 21 additions & 15 deletions generator.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package gold

import (
"errors"
"fmt"
"html/template"
"io"
"io/ioutil"
"os"
"strings"

"github.com/yosssi/gohtml"
Expand All @@ -20,7 +20,7 @@ const (
goldExtension = ".gold"
)

// A generator represents an HTML generator.
// Generator represents an HTML generator.
type Generator struct {
cache bool
templates map[string]*template.Template
Expand Down Expand Up @@ -86,7 +86,7 @@ func (g *Generator) generateTemplate(path string, stringTemplates map[string]str
return tpl, html, nil
}
}
gtpl, err := g.parse(path, stringTemplates)
gtpl, err := g.parse(path, stringTemplates, true)
if err != nil {
return nil, "", err
}
Expand Down Expand Up @@ -115,8 +115,10 @@ func (g *Generator) generateTemplate(path string, stringTemplates map[string]str
}

// parse parses a Gold template file and returns a Gold template.
func (g *Generator) parse(path string, stringTemplates map[string]string) (*Template, error) {
path = Path(g.baseDir, path)
func (g *Generator) parse(path string, stringTemplates map[string]string, addBaseDir bool) (*Template, error) {
if addBaseDir {
path = Path(g.baseDir, path)
}
if g.cache {
if tpl, prs := g.gtemplates[path]; prs {
return tpl, nil
Expand Down Expand Up @@ -146,20 +148,20 @@ func (g *Generator) parse(path string, stringTemplates map[string]string) (*Temp
case isExtends(line):
tokens := strings.Split(strings.TrimSpace(line), " ")
if l := len(tokens); l != extendsBlockTokensLen {
return nil, errors.New(fmt.Sprintf("The line tokens length is invalid. (expected: %d, actual: %d, line no: %d, template: %s, line: %s)", extendsBlockTokensLen, l, i, tpl.Path, strings.TrimSpace(line)))
return nil, fmt.Errorf("the line tokens length is invalid. (expected: %d, actual: %d, line no: %d, template: %s, line: %s)", extendsBlockTokensLen, l, i, tpl.Path, strings.TrimSpace(line))
}
superTplPath := tokens[1]
var superTpl *Template
var err error
addBaseDir := true
if stringTemplates == nil {
if CurrentDirectoryBasedPath(superTplPath) {
superTplPath = tpl.Dir() + superTplPath + goldExtension
} else {
superTplPath = Path(g.baseDir, superTplPath+goldExtension)
if g.baseDir != "" && CurrentDirectoryBasedPath(superTplPath) {
superTplPath = tpl.Dir() + superTplPath
addBaseDir = false
}
superTpl, err = g.parse(superTplPath, nil)
superTpl, err = g.parse(superTplPath+goldExtension, nil, addBaseDir)
} else {
superTpl, err = g.parse(superTplPath, stringTemplates)
superTpl, err = g.parse(superTplPath, stringTemplates, addBaseDir)
}
if err != nil {
return nil, err
Expand All @@ -169,7 +171,7 @@ func (g *Generator) parse(path string, stringTemplates map[string]string) (*Temp
case tpl.Super != nil && isBlock(line):
tokens := strings.Split(strings.TrimSpace(line), " ")
if l := len(tokens); l != extendsBlockTokensLen {
return nil, errors.New(fmt.Sprintf("The line tokens length is invalid. (expected: %d, actual: %d, line no: %d, template: %s, line: %s)", extendsBlockTokensLen, l, i, tpl.Path, strings.TrimSpace(line)))
return nil, fmt.Errorf("the line tokens length is invalid. (expected: %d, actual: %d, line no: %d, template: %s, line: %s)", extendsBlockTokensLen, l, i, tpl.Path, strings.TrimSpace(line))
}
block := &Block{Name: tokens[1], Template: tpl}
tpl.AddBlock(block.Name, block)
Expand All @@ -196,7 +198,11 @@ func (g *Generator) parse(path string, stringTemplates map[string]string) (*Temp

// NewGenerator generages a generator and returns it.
func NewGenerator(cache bool) *Generator {
return &Generator{cache: cache, templates: make(map[string]*template.Template), gtemplates: make(map[string]*Template), htmls: make(map[string]string)}
baseDir, err := os.Getwd()
if err != nil {
baseDir = ""
}
return &Generator{cache: cache, templates: make(map[string]*template.Template), gtemplates: make(map[string]*Template), htmls: make(map[string]string), baseDir: baseDir}
}

// formatLf returns a string whose line feed codes are replaced with LF.
Expand Down Expand Up @@ -265,7 +271,7 @@ func appendChildren(parent Container, lines []string, i *int, l *int, parentInde
return err
}
case indent > parentIndent+1:
return errors.New(fmt.Sprintf("The indent of the line %d is invalid. [template: %s][lineno: %d][line: %s]", *i+1, tpl.Path, *i+1, strings.TrimSpace(line)))
return fmt.Errorf("the indent of the line %d is invalid. [template: %s][lineno: %d][line: %s]", *i+1, tpl.Path, *i+1, strings.TrimSpace(line))
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestGeneratorParseFile(t *testing.T) {
// When g.Parse returns an error.
g = &Generator{}
_, err = g.ParseFile("./test/TestGeneratorParseFile/001.gold")
expectedErrMsg := "The indent of the line 2 is invalid. [template: ./test/TestGeneratorParseFile/001.gold][lineno: 2][line: head]"
expectedErrMsg := "the indent of the line 2 is invalid. [template: ./test/TestGeneratorParseFile/001.gold][lineno: 2][line: head]"
if err == nil || err.Error() != expectedErrMsg {
t.Errorf("Error(%s) should be returned.", expectedErrMsg)
}
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestParse(t *testing.T) {
gtmplt := &Template{}
g := NewGenerator(true)
g.gtemplates = map[string]*Template{"path": gtmplt}
gtpl, err := g.parse("path", nil)
gtpl, err := g.parse("path", nil, false)
if err != nil {
t.Errorf("An error(%s) occurred.", err.Error())
}
Expand All @@ -99,7 +99,7 @@ func TestParse(t *testing.T) {
// When ioutil.ReadFile returns an error.
gtmplt = &Template{}
g = NewGenerator(false)
gtpl, err = g.parse("./somepath/somefile", nil)
gtpl, err = g.parse("./somepath/somefile", nil, true)
expectedErrMsg := "open ./somepath/somefile: no such file or directory"
if err == nil || err.Error() != expectedErrMsg {
t.Errorf("Error(%s) should be returned.", expectedErrMsg)
Expand All @@ -115,7 +115,7 @@ func TestParse(t *testing.T) {
// When a template includes a "extends" and returns an error.
g = NewGenerator(false)
_, err = g.ParseFile("./test/TestGeneratorParseFile/006.gold")
expectedErrMsg = "The line tokens length is invalid. (expected: 2, actual: 1, line no: 1, template: ./test/TestGeneratorParseFile/006.gold, line: extends)"
expectedErrMsg = "the line tokens length is invalid. (expected: 2, actual: 1, line no: 1, template: ./test/TestGeneratorParseFile/006.gold, line: extends)"
if err == nil || err.Error() != expectedErrMsg {
t.Errorf("Error(%s) should be returned.", expectedErrMsg)
}
Expand All @@ -131,15 +131,15 @@ func TestParse(t *testing.T) {
// When a template includes a "extends" and returns an error while parsing a block line.
g = NewGenerator(false)
_, err = g.ParseFile("./test/TestGeneratorParseFile/008.gold")
expectedErrMsg = "The line tokens length is invalid. (expected: 2, actual: 1, line no: 3, template: ./test/TestGeneratorParseFile/008.gold, line: block)"
expectedErrMsg = "the line tokens length is invalid. (expected: 2, actual: 1, line no: 3, template: ./test/TestGeneratorParseFile/008.gold, line: block)"
if err == nil || err.Error() != expectedErrMsg {
t.Errorf("Error(%s) should be returned.", expectedErrMsg)
}

// When a template includes a "extends" and returns an error while appending a child.
g = NewGenerator(false)
_, err = g.ParseFile("./test/TestGeneratorParseFile/009.gold")
expectedErrMsg = `The indent of the line 4 is invalid. [template: ./test/TestGeneratorParseFile/009.gold][lineno: 4][line: div#content.content style="font-size: 1rem; font-weight: bold;"]`
expectedErrMsg = `the indent of the line 4 is invalid. [template: ./test/TestGeneratorParseFile/009.gold][lineno: 4][line: div#content.content style="font-size: 1rem; font-weight: bold;"]`
if err == nil || err.Error() != expectedErrMsg {
t.Errorf("Error(%s) should be returned.", expectedErrMsg)
}
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestAppendChildren(t *testing.T) {
g := NewGenerator(false)
tpl := NewTemplate("/tmp/tmp.gold", g)
err = appendChildren(e, []string{" div"}, &i, &l, 0, false, TypeTag, tpl)
expectedErrMsg = "The indent of the line 1 is invalid. [template: /tmp/tmp.gold][lineno: 1][line: div]"
expectedErrMsg = "the indent of the line 1 is invalid. [template: /tmp/tmp.gold][lineno: 1][line: div]"
if err == nil || err.Error() != expectedErrMsg {
t.Errorf("Error(%s) should be returned.", expectedErrMsg)
}
Expand Down

3 comments on commit 1375147

@ahall
Copy link

@ahall ahall commented on 1375147 May 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yosssi What was this bug? I didn't see it in the issue tracker.

@yosssi
Copy link
Owner Author

@yosssi yosssi commented on 1375147 May 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ahall

Thanks for your comment.

I forgot to create an issue about this bug. I'm so sorry for the inconvenience.

I fixed the following bug:

[bug]
When I set "foo" as a base directory and a Gold template has a line of extends bar, "foo/foo/bar.gold" is parsed instead of "foo/bar.gold".

By the way, I created RenderGold which is a Martini middleware/handler for parsing Gold templates and rendering HTML. On the following issue, I'm asking the martini-contrib team if I can add this middleware on the martini-contrib.

martini-contrib/render#8

Please check it out.

Thanks.

@ahall
Copy link

@ahall ahall commented on 1375147 May 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant, thanks for this!

Please sign in to comment.