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 title: About myself
date: 28-08-2015 date: 28-08-2015
---
# {{title}} # {{title}}

View File

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

View File

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

28
zs.go
View File

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

View File

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