From d4d9da9f3a6358e8325d0c3f973a5842ef3be039 Mon Sep 17 00:00:00 2001 From: Noah Campbell Date: Wed, 4 Sep 2013 16:57:17 -0700 Subject: [PATCH] Remove page module dependence on opening files The site is responsible for reading files, page only operates on buffers. --- docs/content/extras/indexes/series.md | 0 hugolib/benchmark_test.go | 6 ----- hugolib/page.go | 32 +++------------------------ hugolib/page_test.go | 2 +- hugolib/site.go | 17 ++++++++++---- 5 files changed, 17 insertions(+), 40 deletions(-) delete mode 100644 docs/content/extras/indexes/series.md diff --git a/docs/content/extras/indexes/series.md b/docs/content/extras/indexes/series.md deleted file mode 100644 index e69de29bb..000000000 diff --git a/hugolib/benchmark_test.go b/hugolib/benchmark_test.go index a3408896d..c9cbcf024 100644 --- a/hugolib/benchmark_test.go +++ b/hugolib/benchmark_test.go @@ -16,9 +16,3 @@ func BenchmarkParsePage(b *testing.B) { p = p } } - -func BenchmarkNewPage(b *testing.B) { - for i := 0; i < b.N; i++ { - NewPage("redis.cn.md") - } -} diff --git a/hugolib/page.go b/hugolib/page.go index 5152578d0..6289fc61c 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -25,7 +25,6 @@ import ( "github.com/theplant/blackfriday" "html/template" "io" - "io/ioutil" "launchpad.net/goyaml" "os" "path/filepath" @@ -91,7 +90,7 @@ func getSummaryString(content []byte) ([]byte, bool) { // TODO abstract further to support loading from more // than just files on disk. Should load reader (file, []byte) -func NewPage(filename string) *Page { +func newPage(filename string) *Page { page := Page{contentType: "", File: File{FileName: filename, Extension: "html"}, Node: Node{Keywords: make([]string, 10, 30)}, @@ -134,15 +133,6 @@ func StripHTML(s string) string { return output } -func (page *Page) Initalize() error { - err := page.buildPageFromFile() - if err != nil { - return err - } - page.analyzePage() - return nil -} - func (p *Page) guessSection() { if p.Section == "" { x := strings.Split(p.FileName, string(os.PathSeparator)) @@ -186,7 +176,7 @@ func ReadFrom(buf io.Reader, name string) (page *Page, err error) { return nil, errors.New("Zero length page name") } - p := NewPage(name) + p := newPage(name) if err = p.parse(buf); err != nil { return @@ -360,7 +350,7 @@ var ErrMatchingEndingFrontMatterDelimiter = errors.New("unable to match ending f func (page *Page) parseFrontMatter(data *bufio.Reader) (err error) { if err = checkEmpty(data); err != nil { - return + return fmt.Errorf("%s: %s", page.FileName, err) } var mark rune @@ -487,22 +477,6 @@ func (p *Page) ExecuteTemplate(layout string) *bytes.Buffer { return buffer } -func (page *Page) readFile() (data []byte, err error) { - data, err = ioutil.ReadFile(page.FileName) - if err != nil { - return nil, err - } - return data, nil -} - -func (page *Page) buildPageFromFile() error { - f, err := os.Open(page.FileName) - if err != nil { - return err - } - return page.parse(bufio.NewReader(f)) -} - func (page *Page) parse(reader io.Reader) error { data := bufio.NewReader(reader) diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 4c8968ba9..7acbcedab 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -133,7 +133,7 @@ func TestDegenerateEmptyPage(t *testing.T) { t.Fatalf("Expected ReadFrom to return an error when an empty buffer is passed.") } - checkError(t, err, "unable to locate front matter") + checkError(t, err, "test: unable to locate front matter") } func checkPageTitle(t *testing.T, page *Page, title string) { diff --git a/hugolib/site.go b/hugolib/site.go index d7016762c..0eac054b0 100644 --- a/hugolib/site.go +++ b/hugolib/site.go @@ -131,7 +131,9 @@ func (s *Site) Process() (err error) { s.initialize() s.prepTemplates() s.timerStep("initialize & template prep") - s.CreatePages() + if err = s.CreatePages(); err != nil { + return err + } s.setupPrevNext() s.timerStep("import pages") if err = s.BuildSiteMeta(); err != nil { @@ -272,13 +274,19 @@ func (s *Site) AbsUrlify() { } } -func (s *Site) CreatePages() { +func (s *Site) CreatePages() (err error) { for _, fileName := range s.Files { - page := NewPage(fileName) + f, err := os.Open(fileName) + if err != nil { + return err + } + page, err := ReadFrom(f, fileName) + if err != nil { + return err + } page.Site = s.Info page.Tmpl = s.Tmpl _ = s.setUrlPath(page) - page.Initalize() s.setOutFile(page) if s.Config.BuildDrafts || !page.Draft { s.Pages = append(s.Pages, page) @@ -286,6 +294,7 @@ func (s *Site) CreatePages() { } s.Pages.Sort() + return } func (s *Site) setupPrevNext() {