Browse Source

added explicit yaml separator, fixed amber compilation sequence

master
Serge A. Zaitsev 6 years ago
parent
commit
6b71f501a7
  1. 17
      testdata/blog/.test/index.html
  2. 1
      testdata/blog/about.md
  3. 1
      testdata/blog/posts/hello.md
  4. 1
      testdata/blog/posts/update.md
  5. 28
      zs.go
  6. 7
      zs_test.go

17
testdata/blog/.test/index.html

@ -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>

1
testdata/blog/about.md

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

1
testdata/blog/posts/hello.md

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

1
testdata/blog/posts/update.md

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

28
zs.go

@ -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

7
zs_test.go

@ -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",

Loading…
Cancel
Save