From c4486f5a07003caf8b9c790557f5335abf22d731 Mon Sep 17 00:00:00 2001 From: facundoolano Date: Sun, 11 Feb 2024 13:30:10 -0300 Subject: [PATCH] add liquid template support --- go.mod | 4 ++++ go.sum | 8 ++++++++ templates/templates.go | 9 ++++++++- templates/templates_test.go | 40 ++++++++++++++++++++++++++++++++++++- 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index db121bc..2d63eb8 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,10 @@ go 1.22.0 require ( github.com/niklasfasching/go-org v1.7.0 // indirect + github.com/osteele/liquid v1.3.2 // indirect + github.com/osteele/tuesday v1.0.3 // indirect golang.org/x/net v0.0.0-20201224014010-6772e930b67b // indirect + gopkg.in/osteele/liquid.v1 v1.2.4 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index a4f7eac..cb642ae 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,9 @@ github.com/niklasfasching/go-org v1.7.0 h1:vyMdcMWWTe/XmANk19F4k8XGBYg0GQ/gJGMimOjGMek= github.com/niklasfasching/go-org v1.7.0/go.mod h1:WuVm4d45oePiE0eX25GqTDQIt/qPW1T9DGkRscqLW5o= +github.com/osteele/liquid v1.3.2 h1:G+MvVYt1HX2xuv99JgdrhV7zRVdlvFnNi8M5rN8gQmI= +github.com/osteele/liquid v1.3.2/go.mod h1:VmzQQHa5v4E0GvGzqccfAfLgMwRk2V+s1QbxYx9dGak= +github.com/osteele/tuesday v1.0.3 h1:SrCmo6sWwSgnvs1bivmXLvD7Ko9+aJvvkmDjB5G4FTU= +github.com/osteele/tuesday v1.0.3/go.mod h1:pREKpE+L03UFuR+hiznj3q7j3qB1rUZ4XfKejwWFF2M= golang.org/x/net v0.0.0-20201224014010-6772e930b67b h1:iFwSg7t5GZmB/Q5TjiEAsdoLDrdJRC1RiF2WhuV29Qw= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -7,5 +11,9 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/osteele/liquid.v1 v1.2.4 h1:OioNeCaVyWL1jRXzRqQ2vr4ISBbTgtnYsJeVlToLhBw= +gopkg.in/osteele/liquid.v1 v1.2.4/go.mod h1:9Bx5f04tf9SVwv3Tcx93dx3WH0EKWmE0Gjd6Dyoc5cs= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/templates/templates.go b/templates/templates.go index 4255b4d..6b8647a 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -11,6 +11,7 @@ import ( "strings" "github.com/niklasfasching/go-org/org" + "gopkg.in/osteele/liquid.v1" "gopkg.in/yaml.v3" ) @@ -96,7 +97,13 @@ func (templ Template) Render() ([]byte, error) { } } else { - // TODO for other file types, assume a liquid template + // for other file types, assume a liquid template + engine := liquid.NewEngine() + out, err := engine.ParseAndRenderString(string(contents), templ.Metadata) + if err != nil { + return nil, err + } + contents = []byte(out) } // TODO: if layout in metadata, pass the result to the rendered parent diff --git a/templates/templates_test.go b/templates/templates_test.go index 5a02ef2..fd0b554 100644 --- a/templates/templates_test.go +++ b/templates/templates_test.go @@ -89,13 +89,51 @@ tags: ["software", "web"] } func TestRenderLiquid(t *testing.T) { - // TODO + input := `--- +title: my new post +subtitle: a blog post +tags: ["software", "web"] +--- +

{{ title }}

+

{{ subtitle }}

+ +` + + file := newFile("test*.html", input) + defer os.Remove(file.Name()) + + templ, err := Parse(file.Name()) + assertEqual(t, err, nil) + content, err := templ.Render() + assertEqual(t, err, nil) + expected := `

my new post

+

a blog post

+ +` + assertEqual(t, string(content), expected) } func TestRenderOrg(t *testing.T) { // TODO } +func TestRenderLiquidLayout(t *testing.T) { + // TODO +} + +func TestRenderOrgLayout(t *testing.T) { + // TODO +} + +func TestRenderLayoutLayout(t *testing.T) { + // TODO +} + // ------ HELPERS -------- func newFile(name string, contents string) *os.File {