added explicit yaml separator, fixed amber compilation sequence

This commit is contained in:
Serge A. Zaitsev 2015-09-02 19:35:26 +02:00
parent 2ce4b993b0
commit 6b71f501a7
6 changed files with 44 additions and 11 deletions

View File

@ -0,0 +1,17 @@
<html>
<head>
<title>My blog</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<p>Here goes list of posts</p>
<ul>
<li>
<a href="/posts/hello.html">First post</a>
</li>
<li>
<a href="/posts/update.html">Second post</a>
</li>
</ul>
</body>
</html>

View File

@ -1,5 +1,6 @@
title: About myself
date: 28-08-2015
---
# {{title}}

View File

@ -1,5 +1,6 @@
title: First post
date: 28-08-2015
---
# {{title}}

View File

@ -1,5 +1,6 @@
title: Second post
date: 29-08-2015
---
# {{title}}

28
zs.go
View File

@ -16,7 +16,7 @@ import (
"github.com/eknkc/amber"
"github.com/russross/blackfriday"
"github.com/yosssi/gcss"
"gopkg.in/yaml.v1"
"gopkg.in/yaml.v2"
)
const (
@ -115,17 +115,21 @@ func getVars(path string, globals Vars) (Vars, string, error) {
v["url"] = path[:len(path)-len(filepath.Ext(path))] + ".html"
v["output"] = filepath.Join(PUBDIR, v["url"])
if sep := strings.Index(s, "\n\n"); sep == -1 {
delim := "\n---\n"
if sep := strings.Index(s, delim); sep == -1 {
return v, s, nil
} else {
header := s[:sep]
body := s[sep+len("\n\n"):]
body := s[sep+len(delim):]
vars := Vars{}
if err := yaml.Unmarshal([]byte(header), &vars); err != nil {
fmt.Println("ERROR: failed to parse header", err)
return nil, "", err
} else {
for key, value := range vars {
v[key] = value
log.Println(key, value)
}
}
if strings.HasPrefix(v["url"], "./") {
@ -226,12 +230,9 @@ func buildAmber(path string, w io.Writer, vars Vars) error {
if err != nil {
return err
}
if body, err = render(body, v); err != nil {
return err
}
a := amber.New()
if err := a.Parse(body); err != nil {
fmt.Println(body)
return err
}
@ -239,6 +240,16 @@ func buildAmber(path string, w io.Writer, vars Vars) error {
if err != nil {
return err
}
htmlBuf := &bytes.Buffer{}
if err := t.Execute(htmlBuf, v); err != nil {
return err
}
if body, err = render(string(htmlBuf.Bytes()), v); err != nil {
return err
}
if w == nil {
f, err := os.Create(filepath.Join(PUBDIR, renameExt(path, ".amber", ".html")))
if err != nil {
@ -247,7 +258,8 @@ func buildAmber(path string, w io.Writer, vars Vars) error {
defer f.Close()
w = f
}
return t.Execute(w, vars)
_, err = io.WriteString(w, body)
return err
}
// Compiles .gcss into .css

View File

@ -53,7 +53,7 @@ func TestVars(t *testing.T) {
`
foo: bar
title: Hello, world!
---
Some content in markdown
`: Vars{
"foo": "bar",
@ -63,8 +63,9 @@ Some content in markdown
"output": filepath.Join(PUBDIR, "test.html"),
"__content": "Some content in markdown\n",
},
`url: "example.com/foo.html"
`
url: "example.com/foo.html"
---
Hello
`: Vars{
"url": "example.com/foo.html",