Remove page module dependence on opening files

The site is responsible for reading files, page only operates on
buffers.
This commit is contained in:
Noah Campbell 2013-09-04 16:57:17 -07:00
parent cb00917af6
commit d4d9da9f3a
5 changed files with 17 additions and 40 deletions

View file

@ -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")
}
}

View file

@ -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)

View file

@ -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) {

View file

@ -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() {