From a106008bebe177bb99c943cb1a865cdb4d43d3a9 Mon Sep 17 00:00:00 2001 From: Izuru Yakumo Date: Mon, 11 Dec 2023 14:23:49 -0300 Subject: [PATCH] Separate the build functions into their own source files Signed-off-by: Izuru Yakumo --- cmd/aya/amber.go | 47 ++++++++++++++++ cmd/aya/gcss.go | 30 ++++++++++ cmd/aya/html.go | 32 +++++++++++ cmd/aya/main.go | 133 +------------------------------------------- cmd/aya/markdown.go | 33 +++++++++++ cmd/aya/raw.go | 26 +++++++++ version.go | 2 +- 7 files changed, 170 insertions(+), 133 deletions(-) create mode 100644 cmd/aya/amber.go create mode 100644 cmd/aya/gcss.go create mode 100644 cmd/aya/html.go create mode 100644 cmd/aya/markdown.go create mode 100644 cmd/aya/raw.go diff --git a/cmd/aya/amber.go b/cmd/aya/amber.go new file mode 100644 index 0000000..05f9fe1 --- /dev/null +++ b/cmd/aya/amber.go @@ -0,0 +1,47 @@ +// Render .amber files into .html +package main + +import ( + "bytes" + "fmt" + "io" + "os" + "path/filepath" + "github.com/eknkc/amber" +) + +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 +} diff --git a/cmd/aya/gcss.go b/cmd/aya/gcss.go new file mode 100644 index 0000000..2cb449b --- /dev/null +++ b/cmd/aya/gcss.go @@ -0,0 +1,30 @@ +// Render .gcss files into .css +package main + +import ( + "io" + "os" + "path/filepath" + "strings" + "github.com/yosssi/gcss" +) + +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 +} diff --git a/cmd/aya/html.go b/cmd/aya/html.go new file mode 100644 index 0000000..7d6b5d3 --- /dev/null +++ b/cmd/aya/html.go @@ -0,0 +1,32 @@ +// Renders hypertext files expanding all variable macros inside it +package main + +import ( + "io" + "os" + "path/filepath" + "text/template" +) + +func buildHTML(path string, w io.Writer, vars Vars) error { + v, body, err := getVars(path, vars) + if err != nil { + return err + } + if body, err = render(body, v); err != nil { + return err + } + tmpl, err := template.New("").Delims("<%", "%>").Parse(body) + if err != nil { + return err + } + if w == nil { + f, err := os.Create(filepath.Join(PUBDIR, path)) + if err != nil { + return err + } + defer f.Close() + w = f + } + return tmpl.Execute(w, vars) +} diff --git a/cmd/aya/main.go b/cmd/aya/main.go index ac22395..71009cf 100644 --- a/cmd/aya/main.go +++ b/cmd/aya/main.go @@ -1,4 +1,4 @@ -// $TheSupernovaDuo: marisa.chaotic.ninja/aya/cmd/aya, v0.6.9 2023-12-09 23:30:07+0000, yakumo_izuru Exp $ +// $TheSupernovaDuo: marisa.chaotic.ninja/aya/cmd/aya, v0.7.0 2023-12-11 17:22:51+0000, yakumo_izuru Exp $ package main import ( @@ -9,12 +9,8 @@ import ( "os/exec" "path/filepath" "strings" - "text/template" "time" - "github.com/eknkc/amber" - "github.com/russross/blackfriday/v2" - "github.com/yosssi/gcss" "gopkg.in/yaml.v3" "marisa.chaotic.ninja/aya" ) @@ -174,133 +170,6 @@ func render(s string, vars Vars) (string, error) { } -// Renders markdown with the given layout into html expanding all the macros -func buildMarkdown(path string, w io.Writer, vars Vars) error { - v, body, err := getVars(path, vars) - extensions := blackfriday.CommonExtensions|blackfriday.AutoHeadingIDs|blackfriday.Strikethrough|blackfriday.Footnotes - if err != nil { - return err - } - content, err := render(body, v) - if err != nil { - return err - } - v["content"] = string(blackfriday.Run([]byte(content), - blackfriday.WithExtensions(extensions), - )) - if w == nil { - out, err := os.Create(filepath.Join(PUBDIR, renameExt(path, "", ".html"))) - if err != nil { - return err - } - defer out.Close() - w = out - } - return buildHTML(filepath.Join(AYADIR, v["layout"]), w, v) -} - -// Renders hypertext file expanding all variable macros inside it -func buildHTML(path string, w io.Writer, vars Vars) error { - v, body, err := getVars(path, vars) - if err != nil { - return err - } - if body, err = render(body, v); err != nil { - return err - } - tmpl, err := template.New("").Delims("<%", "%>").Parse(body) - if err != nil { - return err - } - if w == nil { - f, err := os.Create(filepath.Join(PUBDIR, path)) - if err != nil { - return err - } - defer f.Close() - w = f - } - 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) - if err != nil { - return err - } - defer in.Close() - if w == nil { - if out, err := os.Create(filepath.Join(PUBDIR, path)); err != nil { - return err - } else { - defer out.Close() - w = out - } - } - _, err = io.Copy(w, in) - return err -} - // This function passes the files to build to their corresponding functions // As far as I'm aware, Markdown has three possible filename extensions, // but .md is the most common one known. diff --git a/cmd/aya/markdown.go b/cmd/aya/markdown.go new file mode 100644 index 0000000..23c7f9a --- /dev/null +++ b/cmd/aya/markdown.go @@ -0,0 +1,33 @@ +// Renders markdown with the given layout into html expanding all the macros +package main + +import ( + "io" + "os" + "path/filepath" + "github.com/russross/blackfriday/v2" +) + +func buildMarkdown(path string, w io.Writer, vars Vars) error { + v, body, err := getVars(path, vars) + extensions := blackfriday.CommonExtensions|blackfriday.AutoHeadingIDs|blackfriday.Strikethrough|blackfriday.Footnotes + if err != nil { + return err + } + content, err := render(body, v) + if err != nil { + return err + } + v["content"] = string(blackfriday.Run([]byte(content), + blackfriday.WithExtensions(extensions), + )) + if w == nil { + out, err := os.Create(filepath.Join(PUBDIR, renameExt(path, "", ".html"))) + if err != nil { + return err + } + defer out.Close() + w = out + } + return buildHTML(filepath.Join(AYADIR, v["layout"]), w, v) +} diff --git a/cmd/aya/raw.go b/cmd/aya/raw.go new file mode 100644 index 0000000..83e12f3 --- /dev/null +++ b/cmd/aya/raw.go @@ -0,0 +1,26 @@ +// Copy files as-is from source to destination +package main + +import ( + "io" + "os" + "path/filepath" +) + +func buildRaw(path string, w io.Writer) error { + in, err := os.Open(path) + if err != nil { + return err + } + defer in.Close() + if w == nil { + if out, err := os.Create(filepath.Join(PUBDIR, path)); err != nil { + return err + } else { + defer out.Close() + w = out + } + } + _, err = io.Copy(w, in) + return err +} diff --git a/version.go b/version.go index 30d5678..0210513 100644 --- a/version.go +++ b/version.go @@ -8,7 +8,7 @@ import ( var ( // Set to current tag - Version = "v0.6.9" + Version = "v0.7.0" Time = time.Now() ) -- 2.43.0