replaced amber with my own fork, fixed file paths for amber and html

This commit is contained in:
Serge A. Zaitsev 2015-08-29 23:43:59 +02:00
parent aafb2e9d82
commit 46f55e19bd

45
zs.go
View File

@ -14,9 +14,9 @@ import (
"text/template" "text/template"
"time" "time"
"github.com/eknkc/amber"
"github.com/russross/blackfriday" "github.com/russross/blackfriday"
"github.com/yosssi/gcss" "github.com/yosssi/gcss"
"github.com/zserge/amber"
) )
const ( const (
@ -49,8 +49,13 @@ func md(path string, globals Vars) (Vars, string, error) {
"file": path, "file": path,
"url": url, "url": url,
"output": filepath.Join(PUBDIR, url), "output": filepath.Join(PUBDIR, url),
"layout": "index.html",
} }
if _, err := os.Stat(filepath.Join(ZSDIR, "layout.amber")); err == nil {
v["layout"] = "layout.amber"
} else {
v["layout"] = "layout.html"
}
if info, err := os.Stat(path); err == nil { if info, err := os.Stat(path); err == nil {
v["date"] = info.ModTime().Format("02-01-2006") v["date"] = info.ModTime().Format("02-01-2006")
} }
@ -115,7 +120,7 @@ func run(cmd string, args []string, vars Vars, output io.Writer) error {
err := c.Run() err := c.Run()
if errbuf.Len() > 0 { if errbuf.Len() > 0 {
log.Println(errbuf.String()) log.Println("ERROR:", errbuf.String())
} }
if err != nil { if err != nil {
@ -155,15 +160,17 @@ func buildMarkdown(path string, funcs template.FuncMap, vars Vars) error {
} }
v["content"] = string(blackfriday.MarkdownBasic([]byte(content))) v["content"] = string(blackfriday.MarkdownBasic([]byte(content)))
if strings.HasSuffix(v["layout"], ".amber") { if strings.HasSuffix(v["layout"], ".amber") {
return buildAmber(filepath.Join(ZSDIR, v["layout"]), funcs, v) return buildAmber(filepath.Join(ZSDIR, v["layout"]),
renameExt(path, "", ".html"), funcs, v)
} else { } else {
return buildPlain(filepath.Join(ZSDIR, v["layout"]), funcs, v) return buildPlain(filepath.Join(ZSDIR, v["layout"]),
renameExt(path, "", ".html"), funcs, v)
} }
} }
// Renders text file expanding all variable macros inside it // Renders text file expanding all variable macros inside it
func buildPlain(path string, funcs template.FuncMap, vars Vars) error { func buildPlain(in, out string, funcs template.FuncMap, vars Vars) error {
b, err := ioutil.ReadFile(path) b, err := ioutil.ReadFile(in)
if err != nil { if err != nil {
return err return err
} }
@ -171,7 +178,7 @@ func buildPlain(path string, funcs template.FuncMap, vars Vars) error {
if err != nil { if err != nil {
return err return err
} }
output := filepath.Join(PUBDIR, path) output := filepath.Join(PUBDIR, out)
if s, ok := vars["output"]; ok { if s, ok := vars["output"]; ok {
output = s output = s
} }
@ -183,9 +190,9 @@ func buildPlain(path string, funcs template.FuncMap, vars Vars) error {
} }
// Renders .amber file into .html // Renders .amber file into .html
func buildAmber(path string, funcs template.FuncMap, vars Vars) error { func buildAmber(in, out string, funcs template.FuncMap, vars Vars) error {
a := amber.New() a := amber.New()
err := a.ParseFile(path) err := a.ParseFile(in)
if err != nil { if err != nil {
return err return err
} }
@ -194,8 +201,7 @@ func buildAmber(path string, funcs template.FuncMap, vars Vars) error {
return err return err
} }
//amber.FuncMap = amber.FuncMap //amber.FuncMap = amber.FuncMap
s := strings.TrimSuffix(path, ".amber") + ".html" f, err := os.Create(filepath.Join(PUBDIR, out))
f, err := os.Create(filepath.Join(PUBDIR, s))
if err != nil { if err != nil {
return err return err
} }
@ -272,6 +278,13 @@ func createFuncs() template.FuncMap {
return funcs return funcs
} }
func renameExt(path, from, to string) string {
if from == "" {
from = filepath.Ext(path)
}
return strings.TrimSuffix(path, from) + to
}
func globals() Vars { func globals() Vars {
vars := Vars{} vars := Vars{}
for _, e := range os.Environ() { for _, e := range os.Environ() {
@ -313,10 +326,10 @@ func buildAll(once bool) {
return buildMarkdown(path, funcs, vars) return buildMarkdown(path, funcs, vars)
} else if ext == ".html" || ext == ".xml" { } else if ext == ".html" || ext == ".xml" {
log.Println("html: ", path) log.Println("html: ", path)
return buildPlain(path, funcs, vars) return buildPlain(path, path, funcs, vars)
} else if ext == ".amber" { } else if ext == ".amber" {
log.Println("html: ", path) log.Println("html: ", path)
return buildAmber(path, funcs, vars) return buildAmber(path, renameExt(path, ".amber", ".html"), funcs, vars)
} else if ext == ".gcss" { } else if ext == ".gcss" {
log.Println("css: ", path) log.Println("css: ", path)
return buildGCSS(path) return buildGCSS(path)
@ -372,12 +385,12 @@ func main() {
} }
} }
} else { } else {
log.Println(err) log.Println("ERROR:", err)
} }
default: default:
err := run(path.Join(ZSDIR, cmd), args, Vars{}, os.Stdout) err := run(path.Join(ZSDIR, cmd), args, Vars{}, os.Stdout)
if err != nil { if err != nil {
log.Println(err) log.Println("ERROR:", err)
} }
} }
} }