Add functions to build Amber and GCSS (taken from original project)

Signed-off-by: Izuru Yakumo <yakumo.izuru@chaotic.ninja>

git-svn-id: file:///srv/svn/repo/aya/trunk@60 cec141ff-132a-4243-88a5-ce187bd62f94
This commit is contained in:
yakumo.izuru
2023-04-21 20:25:31 +00:00
parent 02ed102707
commit 625906f6b5
3 changed files with 70 additions and 0 deletions

View File

@@ -16,6 +16,8 @@ import (
"gopkg.in/yaml.v2"
"marisa.chaotic.ninja/aya"
log "github.com/sirupsen/logrus"
"github.com/eknkc/amber"
"github.com/yosssi/gcss"
)
const (
@@ -221,6 +223,64 @@ func buildHTML(path string, w io.Writer, vars Vars) error {
return tmpl.Execute(w, vars)
}
// Renders .amber file into .html
func buildAmber(path string, w io.Writer, vars Vars) error {
v, body, err := getVars(path, vars)
if err != nil {
return err
}
a := amber.New()
if err := a.Parse(body); err != nil {
fmt.Println(body)
return err
}
t, err := a.Compile()
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 {
return err
}
defer f.Close()
w = f
}
_, err = io.WriteString(w, body)
return err
}
// Compiles .gcss into .css
func buildGCSS(path string, w io.Writer) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
if w == nil {
s := strings.TrimSuffix(path, ".gcss") + ".css"
css, err := os.Create(filepath.Join(PUBDIR, s))
if err != nil {
return err
}
defer css.Close()
w = css
}
_, err = gcss.Compile(w, f)
return err
}
// Copies file as is from path to writer
func buildRaw(path string, w io.Writer) error {
in, err := os.Open(path)
@@ -246,6 +306,10 @@ func build(path string, w io.Writer, vars Vars) error {
return buildMarkdown(path, w, vars)
} else if ext == ".html" || ext == ".xml" {
return buildHTML(path, w, vars)
} else if ext == ".amber" {
return buildAmber(path, w, vars)
} else if ext == ".gcss" {
return buildGCSS(path, w)
} else {
return buildRaw(path, w)
}