From 6367a8d0e02133b786dcc7109c43cce324c4bcf5 Mon Sep 17 00:00:00 2001 From: Oliver Steele Date: Thu, 22 Jun 2017 10:42:57 -0400 Subject: [PATCH] Move VariableMap -> new templates package --- cmd/gojekyll/commands.go | 21 ++++++++++--------- collection.go | 14 +++++++------ dynamic_page.go | 17 ++++++++------- layout.go | 3 ++- page.go | 19 +++++++++-------- permalinks_test.go | 7 ++++--- site.go | 7 ++++--- site_config.go | 15 +++++++------ site_variables.go | 11 +++++----- variable_map.go => templates/variablemap.go | 2 +- .../variablemap_test.go | 2 +- 11 files changed, 65 insertions(+), 53 deletions(-) rename variable_map.go => templates/variablemap.go (97%) rename variable_map_test.go => templates/variablemap_test.go (98%) diff --git a/cmd/gojekyll/commands.go b/cmd/gojekyll/commands.go index 4d67bbf..2235ed5 100644 --- a/cmd/gojekyll/commands.go +++ b/cmd/gojekyll/commands.go @@ -13,6 +13,7 @@ import ( "github.com/osteele/gojekyll" "github.com/osteele/gojekyll/helpers" + "github.com/osteele/gojekyll/templates" ) // main sets this @@ -75,16 +76,16 @@ func varsCommand(site *gojekyll.Site) error { // (Actually it's circular, which the yaml package can't handle.) // Neuter it. This destroys it as Liquid data, but that's okay in this context. for _, c := range site.Collections { - siteData[c.Name] = fmt.Sprintf("", len(siteData[c.Name].([]gojekyll.VariableMap))) + siteData[c.Name] = fmt.Sprintf("", len(siteData[c.Name].([]templates.VariableMap))) } - var data interface{} //gojekyll.VariableMap + var data interface{} //templates.VariableMap switch { case *siteVariable: data = siteData case *dataVariable: - data = siteData["data"].(gojekyll.VariableMap) + data = siteData["data"].(templates.VariableMap) if *variablePath != "" { - data = data.(gojekyll.VariableMap)[*variablePath] + data = data.(templates.VariableMap)[*variablePath] } default: page, err := cliPage(site, *variablePath) @@ -138,12 +139,12 @@ func cliPage(s *gojekyll.Site, path string) (gojekyll.Page, error) { path = "/" } switch { - case strings.HasPrefix(path, "/"): - page, found := s.URLPage(path) - if !found { - return nil, helpers.NewPathError("render", path, "the site does not include a file with this URL path") - } - return page, nil + case strings.HasPrefix(path, "/"): + page, found := s.URLPage(path) + if !found { + return nil, helpers.NewPathError("render", path, "the site does not include a file with this URL path") + } + return page, nil default: page, found := s.RelPathPage(path) if !found { diff --git a/collection.go b/collection.go index c890fd0..dd82842 100644 --- a/collection.go +++ b/collection.go @@ -5,18 +5,20 @@ import ( "os" "path/filepath" "strings" + + "github.com/osteele/gojekyll/templates" ) // Collection is a Jekyll collection. type Collection struct { Site *Site Name string - Data VariableMap + Data templates.VariableMap pages []Page } // NewCollection creates a new Collection with defaults d -func NewCollection(s *Site, name string, d VariableMap) *Collection { +func NewCollection(s *Site, name string, d templates.VariableMap) *Collection { return &Collection{ Site: s, Name: name, @@ -28,7 +30,7 @@ func NewCollection(s *Site, name string, d VariableMap) *Collection { func (c *Collection) IsPosts() bool { return c.Name == "posts" } // Output returns a bool indicating whether files in this collection should be written. -func (c *Collection) Output() bool { return c.Data.Bool("output", false) } +func (c *Collection) Output() bool { return c.Data.Bool("output", false) } // PathPrefix returns the collection's directory prefix, e.g. "_posts/" func (c *Collection) PathPrefix() string { return filepath.FromSlash("_" + c.Name + "/") } @@ -38,7 +40,7 @@ func (c *Collection) Source() string { return filepath.Join(c.Site.Source, "_"+c // TemplateVariable returns an array of page objects, for use as the template variable // value of the collection. -func (c *Collection) TemplateVariable() (d []VariableMap) { +func (c *Collection) TemplateVariable() (d []templates.VariableMap) { for _, page := range c.Pages() { d = append(d, page.Variables()) } @@ -47,7 +49,7 @@ func (c *Collection) TemplateVariable() (d []VariableMap) { // ReadPages scans the file system for collection pages, and adds them to c.Pages. func (c *Collection) ReadPages() error { - collectionDefaults := MergeVariableMaps(c.Data, VariableMap{ + collectionDefaults := templates.MergeVariableMaps(c.Data, templates.VariableMap{ "collection": c.Name, }) @@ -71,7 +73,7 @@ func (c *Collection) ReadPages() error { case info.IsDir(): return nil } - defaults := MergeVariableMaps(c.Site.GetFrontMatterDefaults(relname, ""), collectionDefaults) + defaults := templates.MergeVariableMaps(c.Site.GetFrontMatterDefaults(relname, ""), collectionDefaults) p, err := NewPageFromFile(c.Site, c, filename, relname, defaults) switch { case err != nil: diff --git a/dynamic_page.go b/dynamic_page.go index 74ea5b6..6ef79fe 100644 --- a/dynamic_page.go +++ b/dynamic_page.go @@ -9,6 +9,7 @@ import ( "github.com/osteele/gojekyll/helpers" "github.com/osteele/gojekyll/liquid" + "github.com/osteele/gojekyll/templates" "github.com/russross/blackfriday" yaml "gopkg.in/yaml.v2" @@ -34,14 +35,14 @@ func newDynamicPageFromFile(filename string, f pageFields) (*DynamicPage, error) if err != nil { return nil, err } - f.frontMatter = MergeVariableMaps(f.frontMatter, frontMatter) + f.frontMatter = templates.MergeVariableMaps(f.frontMatter, frontMatter) return &DynamicPage{ pageFields: f, raw: b, }, nil } -func readFrontMatter(sourcePtr *[]byte) (frontMatter VariableMap, err error) { +func readFrontMatter(sourcePtr *[]byte) (frontMatter templates.VariableMap, err error) { var ( source = *sourcePtr start = 0 @@ -65,7 +66,7 @@ func readFrontMatter(sourcePtr *[]byte) (frontMatter VariableMap, err error) { } // Variables returns the attributes of the template page object. -func (p *DynamicPage) Variables() VariableMap { +func (p *DynamicPage) Variables() templates.VariableMap { var ( relpath = p.relpath ext = filepath.Ext(relpath) @@ -78,7 +79,7 @@ func (p *DynamicPage) Variables() VariableMap { content = &[]byte{} } - data := VariableMap{ + data := templates.VariableMap{ "path": relpath, "url": p.Permalink(), "content": content, @@ -122,8 +123,8 @@ func (p *DynamicPage) Variables() VariableMap { } // TemplateContext returns the local variables for template evaluation -func (p *DynamicPage) TemplateContext(ctx Context) VariableMap { - return VariableMap{ +func (p *DynamicPage) TemplateContext(ctx Context) templates.VariableMap { + return templates.VariableMap{ "page": p.Variables(), "site": ctx.SiteVariables(), } @@ -173,7 +174,7 @@ func (p *DynamicPage) Write(ctx Context, w io.Writer) error { return err } -func (p *DynamicPage) applyLayout(ctx Context, frontMatter VariableMap, body []byte) ([]byte, error) { +func (p *DynamicPage) applyLayout(ctx Context, frontMatter templates.VariableMap, body []byte) ([]byte, error) { for { name := frontMatter.String("layout", "") if name == "" { @@ -183,7 +184,7 @@ func (p *DynamicPage) applyLayout(ctx Context, frontMatter VariableMap, body []b if err != nil { return nil, err } - vars := MergeVariableMaps(p.TemplateContext(ctx), VariableMap{ + vars := templates.MergeVariableMaps(p.TemplateContext(ctx), templates.VariableMap{ "content": string(body), "layout": frontMatter, }) diff --git a/layout.go b/layout.go index dcd584a..9f8c27e 100644 --- a/layout.go +++ b/layout.go @@ -8,10 +8,11 @@ import ( "strings" "github.com/osteele/gojekyll/liquid" + "github.com/osteele/gojekyll/templates" ) // FindLayout returns a template for the named layout. -func (s *Site) FindLayout(base string, fm *VariableMap) (t liquid.Template, err error) { +func (s *Site) FindLayout(base string, fm *templates.VariableMap) (t liquid.Template, err error) { exts := []string{"", ".html"} for _, ext := range strings.SplitN(s.config.MarkdownExt, `,`, -1) { exts = append(exts, "."+ext) diff --git a/page.go b/page.go index 969779b..8a8a2f6 100644 --- a/page.go +++ b/page.go @@ -13,6 +13,7 @@ import ( "github.com/osteele/gojekyll/helpers" "github.com/osteele/gojekyll/liquid" + "github.com/osteele/gojekyll/templates" ) var ( @@ -34,7 +35,7 @@ type Page interface { Write(Context, io.Writer) error // Variables - Variables() VariableMap + Variables() templates.VariableMap // internal initPermalink() error @@ -42,11 +43,11 @@ type Page interface { // Context provides context information to a Page. type Context interface { - FindLayout(relname string, frontMatter *VariableMap) (liquid.Template, error) + FindLayout(relname string, frontMatter *templates.VariableMap) (liquid.Template, error) IsMarkdown(filename string) bool IsSassPath(filename string) bool SassIncludePaths() []string - SiteVariables() VariableMap + SiteVariables() templates.VariableMap SourceDir() string TemplateEngine() liquid.Engine WriteSass(io.Writer, []byte) error @@ -66,7 +67,7 @@ type pageFields struct { outputExt string permalink string // cached permalink modTime time.Time - frontMatter VariableMap // page front matter, merged with defaults + frontMatter templates.VariableMap // page front matter, merged with defaults isMarkdown bool } @@ -82,7 +83,7 @@ func (p *pageFields) OutputExt() string { return p.outputExt } func (p *pageFields) SiteRelPath() string { return p.relpath } // NewPageFromFile reads a Page from a file, using defaults as the default front matter. -func NewPageFromFile(ctx Context, c Container, filename string, relpath string, defaults VariableMap) (Page, error) { +func NewPageFromFile(ctx Context, c Container, filename string, relpath string, defaults templates.VariableMap) (Page, error) { magic, err := helpers.ReadFileMagic(filename) if err != nil { return nil, err @@ -127,14 +128,14 @@ func NewPageFromFile(ctx Context, c Container, filename string, relpath string, // Variables returns the attributes of the template page object. // See https://jekyllrb.com/docs/variables/#page-variables -func (p *pageFields) Variables() VariableMap { +func (p *pageFields) Variables() templates.VariableMap { var ( relpath = "/" + filepath.ToSlash(p.relpath) base = path.Base(relpath) ext = path.Ext(relpath) ) - return VariableMap{ + return templates.VariableMap{ "path": relpath, "modified_time": p.modTime, "name": base, @@ -152,8 +153,8 @@ type StaticPage struct { func (p *StaticPage) Static() bool { return true } // Variables returns metadata for use in the representation of the page as a collection item -func (p *StaticPage) Variables() VariableMap { - return MergeVariableMaps(p.frontMatter, p.pageFields.Variables()) +func (p *StaticPage) Variables() templates.VariableMap { + return templates.MergeVariableMaps(p.frontMatter, p.pageFields.Variables()) } func (p *StaticPage) Write(_ Context, w io.Writer) error { diff --git a/permalinks_test.go b/permalinks_test.go index 1ba5a2b..d64df58 100644 --- a/permalinks_test.go +++ b/permalinks_test.go @@ -3,6 +3,7 @@ package gojekyll import ( "testing" + "github.com/osteele/gojekyll/templates" "github.com/stretchr/testify/require" ) @@ -14,12 +15,12 @@ func (c containerMock) PathPrefix() string { return c.pathPrefix } func TestExpandPermalinkPattern(t *testing.T) { var ( c = containerMock{} - d = VariableMap{} + d = templates.VariableMap{} path = "/a/b/base.html" ) - testPermalinkPattern := func(pattern, path string, data VariableMap) (string, error) { - vs := MergeVariableMaps(data, VariableMap{"permalink": pattern}) + testPermalinkPattern := func(pattern, path string, data templates.VariableMap) (string, error) { + vs := templates.MergeVariableMaps(data, templates.VariableMap{"permalink": pattern}) p := pageFields{container: c, relpath: path, frontMatter: vs} return p.expandPermalink() } diff --git a/site.go b/site.go index 0885838..c778dbb 100644 --- a/site.go +++ b/site.go @@ -9,6 +9,7 @@ import ( "github.com/osteele/gojekyll/helpers" "github.com/osteele/gojekyll/liquid" + "github.com/osteele/gojekyll/templates" ) // Site is a Jekyll site. @@ -19,7 +20,7 @@ type Site struct { UseRemoteLiquidEngine bool Collections []*Collection - Variables VariableMap + Variables templates.VariableMap Paths map[string]Page // URL path -> Page config SiteConfig @@ -274,13 +275,13 @@ func (s *Site) TemplateEngine() liquid.Engine { } // GetFrontMatterDefaults implements https://jekyllrb.com/docs/configuration/#front-matter-defaults -func (s *Site) GetFrontMatterDefaults(relpath, typename string) (m VariableMap) { +func (s *Site) GetFrontMatterDefaults(relpath, typename string) (m templates.VariableMap) { for _, entry := range s.config.Defaults { scope := &entry.Scope hasPrefix := strings.HasPrefix(relpath, scope.Path) hasType := scope.Type == "" || scope.Type == typename if hasPrefix && hasType { - m = MergeVariableMaps(m, entry.Values) + m = templates.MergeVariableMaps(m, entry.Values) } } return diff --git a/site_config.go b/site_config.go index 8be14c5..7074bbd 100644 --- a/site_config.go +++ b/site_config.go @@ -1,6 +1,9 @@ package gojekyll -import yaml "gopkg.in/yaml.v2" +import ( + "github.com/osteele/gojekyll/templates" + yaml "gopkg.in/yaml.v2" +) // SiteConfig is the Jekyll site configuration, typically read from _config.yml. // See https://jekyllrb.com/docs/configuration/#default-configuration @@ -9,9 +12,9 @@ type SiteConfig struct { Source string Destination string LayoutsDir string `yaml:"layouts_dir"` - DataDir string `yaml:"data_dir"` + DataDir string `yaml:"data_dir"` IncludesDir string `yaml:"includes_dir"` - Collections map[string]VariableMap + Collections map[string]templates.VariableMap // Handling Reading Include []string @@ -26,19 +29,19 @@ type SiteConfig struct { Path string Type string } - Values VariableMap + Values templates.VariableMap } } func (s *Site) readConfigBytes(bytes []byte) error { - configVariables := VariableMap{} + configVariables := templates.VariableMap{} if err := yaml.Unmarshal(bytes, &s.config); err != nil { return err } if err := yaml.Unmarshal(bytes, &configVariables); err != nil { return err } - s.Variables = MergeVariableMaps(s.Variables, configVariables) + s.Variables = templates.MergeVariableMaps(s.Variables, configVariables) return nil } diff --git a/site_variables.go b/site_variables.go index 3aa104f..e70c850 100644 --- a/site_variables.go +++ b/site_variables.go @@ -7,10 +7,11 @@ import ( "time" "github.com/osteele/gojekyll/helpers" + "github.com/osteele/gojekyll/templates" ) // SiteVariables returns the site variable for template evaluation. -func (s *Site) SiteVariables() VariableMap { +func (s *Site) SiteVariables() templates.VariableMap { return s.Variables } @@ -19,7 +20,7 @@ func (s *Site) initSiteVariables() error { if err != nil { return err } - s.Variables = MergeVariableMaps(s.Variables, VariableMap{ + s.Variables = templates.MergeVariableMaps(s.Variables, templates.VariableMap{ "data": data, // TODO read time from _config, if it's available "time": time.Now(), @@ -35,13 +36,13 @@ func (s *Site) updateCollectionVariables() { } } -func (s *Site) readDataFiles() (VariableMap, error) { - data := VariableMap{} +func (s *Site) readDataFiles() (templates.VariableMap, error) { + data := templates.VariableMap{} dataDir := filepath.Join(s.Source, s.config.DataDir) files, err := ioutil.ReadDir(dataDir) if err != nil { if os.IsNotExist(err) { - return VariableMap{}, nil + return templates.VariableMap{}, nil } return nil, err } diff --git a/variable_map.go b/templates/variablemap.go similarity index 97% rename from variable_map.go rename to templates/variablemap.go index 27ec95c..fd31e6a 100644 --- a/variable_map.go +++ b/templates/variablemap.go @@ -1,4 +1,4 @@ -package gojekyll +package templates // VariableMap is a map of strings to interface values, for use in template processing. type VariableMap map[string]interface{} diff --git a/variable_map_test.go b/templates/variablemap_test.go similarity index 98% rename from variable_map_test.go rename to templates/variablemap_test.go index 53aa1af..23721bc 100644 --- a/variable_map_test.go +++ b/templates/variablemap_test.go @@ -1,4 +1,4 @@ -package gojekyll +package templates import ( "testing"