From 859a78e1bda04bcaad11aa022d792d419e2ad769 Mon Sep 17 00:00:00 2001 From: spf13 Date: Thu, 1 May 2014 13:21:37 -0400 Subject: [PATCH] Providing the ability to save (write) a content's source file. --- hugolib/page.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/hugolib/page.go b/hugolib/page.go index 052e4d9e8..f771ef48c 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -567,6 +567,27 @@ func (page *Page) SetSourceMetaData(in interface{}, mark rune) (err error) { return nil } +func (page *Page) SaveSourceAs(path string) { + b := new(bytes.Buffer) + b.Write(page.sourceFrontmatter) + b.Write(page.sourceContent) + + page.saveSource(b.Bytes(), path) +} + +func (page *Page) saveSource(by []byte, inpath string) (err error) { + if !path.IsAbs(inpath) { + inpath = helpers.AbsPathify(inpath) + } + jww.INFO.Println("creating", inpath) + helpers.WriteToDisk(inpath, bytes.NewReader(by)) + return nil +} + +func (page *Page) SaveSource() { + page.SaveSourceAs(page.FullFilePath()) +} + func (p *Page) ProcessShortcodes(t Template) { p.rawContent = []byte(ShortcodesHandle(string(p.rawContent), p, t)) p.Summary = template.HTML(ShortcodesHandle(string(p.Summary), p, t)) @@ -669,6 +690,10 @@ func ReaderToBytes(lines io.Reader) []byte { return b.Bytes() } +func (p *Page) FullFilePath() string { + return path.Join(p.Dir, p.FileName) +} + func (p *Page) TargetPath() (outfile string) { // Always use Url if it's specified