shithub: hugo

Download patch

ref: 859a78e1bda04bcaad11aa022d792d419e2ad769
parent: 1302ef9f636911296c737f0844036c9d5d604936
author: spf13 <[email protected]>
date: Thu May 1 09:21:37 EDT 2014

Providing the ability to save (write) a content's source file.

--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -567,6 +567,27 @@
 	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))
@@ -667,6 +688,10 @@
 	b := new(bytes.Buffer)
 	b.ReadFrom(lines)
 	return b.Bytes()
+}
+
+func (p *Page) FullFilePath() string {
+	return path.Join(p.Dir, p.FileName)
 }
 
 func (p *Page) TargetPath() (outfile string) {