1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 15:27:47 +01:00

Remove more templates.VariableMap

This commit is contained in:
Oliver Steele 2017-06-30 23:56:29 -04:00
parent 887820d7bd
commit 9b1d1d7790
12 changed files with 51 additions and 51 deletions

View File

@ -16,13 +16,13 @@ import (
// Collection is a Jekyll collection https://jekyllrb.com/docs/collections/. // Collection is a Jekyll collection https://jekyllrb.com/docs/collections/.
type Collection struct { type Collection struct {
Name string Name string
Metadata templates.VariableMap Metadata map[string]interface{}
container pages.Container container pages.Container
pages []pages.Document pages []pages.Document
} }
// NewCollection creates a new Collection // NewCollection creates a new Collection
func NewCollection(name string, metadata templates.VariableMap, c pages.Container) *Collection { func NewCollection(name string, metadata map[string]interface{}, c pages.Container) *Collection {
return &Collection{ return &Collection{
Name: name, Name: name,
Metadata: metadata, Metadata: metadata,
@ -43,7 +43,7 @@ func (c *Collection) PathPrefix() string { return filepath.FromSlash("_" + c.Nam
func (c *Collection) IsPostsCollection() bool { return c.Name == "posts" } func (c *Collection) IsPostsCollection() bool { return c.Name == "posts" }
// Output returns a bool indicating whether files in this collection should be written. // Output returns a bool indicating whether files in this collection should be written.
func (c *Collection) Output() bool { return c.Metadata.Bool("output", false) } func (c *Collection) Output() bool { return templates.VariableMap(c.Metadata).Bool("output", false) }
// Pages is a list of pages. // Pages is a list of pages.
func (c *Collection) Pages() []pages.Document { func (c *Collection) Pages() []pages.Document {
@ -62,7 +62,7 @@ func (c *Collection) TemplateVariable(ctx pages.RenderingContext, includeContent
if err != nil { if err != nil {
return nil, err return nil, err
} }
v = templates.MergeVariableMaps(v, templates.VariableMap{ v = templates.MergeVariableMaps(v, map[string]interface{}{
"content": string(c), "content": string(c),
}) })
} }
@ -84,12 +84,12 @@ func (c *Collection) PermalinkPattern() string {
if c.IsPostsCollection() { if c.IsPostsCollection() {
defaultPattern = constants.DefaultPostsCollectionPermalinkPattern defaultPattern = constants.DefaultPostsCollectionPermalinkPattern
} }
return c.Metadata.String("permalink", defaultPattern) return templates.VariableMap(c.Metadata).String("permalink", defaultPattern)
} }
// ReadPages scans the file system for collection pages, and adds them to c.Pages. // ReadPages scans the file system for collection pages, and adds them to c.Pages.
func (c *Collection) ReadPages(sitePath string, frontMatterDefaults func(string, string) templates.VariableMap) error { func (c *Collection) ReadPages(sitePath string, frontMatterDefaults func(string, string) map[string]interface{}) error {
pageDefaults := templates.VariableMap{ pageDefaults := map[string]interface{}{
"collection": c.Name, "collection": c.Name,
"permalink": c.PermalinkPattern(), "permalink": c.PermalinkPattern(),
} }

View File

@ -4,7 +4,6 @@ import (
"path" "path"
"testing" "testing"
"github.com/osteele/gojekyll/templates"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -19,36 +18,36 @@ type MockContainer struct{}
func (c MockContainer) PathPrefix() string { return "" } func (c MockContainer) PathPrefix() string { return "" }
func (c MockContainer) OutputExt(filename string) string { return path.Ext(filename) } func (c MockContainer) OutputExt(filename string) string { return path.Ext(filename) }
// func (c MockPipeline) Render(_ io.Writer, _ []byte, _ string, _ templates.VariableMap) ([]byte, error) { // func (c MockPipeline) Render(_ io.Writer, _ []byte, _ string, _ map[string]interface{}) ([]byte, error) {
// return nil, fmt.Errorf("unimplemented") // return nil, fmt.Errorf("unimplemented")
// } // }
// func (c MockPipeline) ApplyLayout(_ string, _ []byte, _ templates.VariableMap) ([]byte, error) { // func (c MockPipeline) ApplyLayout(_ string, _ []byte, _ map[string]interface{}) ([]byte, error) {
// return nil, fmt.Errorf("unimplemented") // return nil, fmt.Errorf("unimplemented")
// } // }
// func (c MockPipeline) SiteVariables() templates.VariableMap { return templates.VariableMap{} } // func (c MockPipeline) SiteVariables() map[string]interface{} { return map[string]interface{}{} }
func TestNewCollection(t *testing.T) { func TestNewCollection(t *testing.T) {
ctx := MockContainer{} ctx := MockContainer{}
c1 := NewCollection("c", templates.VariableMap{"output": true}, ctx) c1 := NewCollection("c", map[string]interface{}{"output": true}, ctx)
require.Equal(t, true, c1.Output()) require.Equal(t, true, c1.Output())
require.Equal(t, "_c/", c1.PathPrefix()) require.Equal(t, "_c/", c1.PathPrefix())
c2 := NewCollection("c", templates.VariableMap{}, ctx) c2 := NewCollection("c", map[string]interface{}{}, ctx)
require.Equal(t, false, c2.Output()) require.Equal(t, false, c2.Output())
} }
func TestPermalinkPattern(t *testing.T) { func TestPermalinkPattern(t *testing.T) {
ctx := MockContainer{} ctx := MockContainer{}
c1 := NewCollection("c", templates.VariableMap{}, ctx) c1 := NewCollection("c", map[string]interface{}{}, ctx)
require.Contains(t, c1.PermalinkPattern(), ":collection") require.Contains(t, c1.PermalinkPattern(), ":collection")
c2 := NewCollection("c", templates.VariableMap{"permalink": "out"}, ctx) c2 := NewCollection("c", map[string]interface{}{"permalink": "out"}, ctx)
require.Equal(t, "out", c2.PermalinkPattern()) require.Equal(t, "out", c2.PermalinkPattern())
c3 := NewCollection("posts", templates.VariableMap{}, ctx) c3 := NewCollection("posts", map[string]interface{}{}, ctx)
require.Contains(t, c3.PermalinkPattern(), "/:year/:month/:day/:title") require.Contains(t, c3.PermalinkPattern(), "/:year/:month/:day/:title")
} }

View File

@ -16,7 +16,7 @@ type Config struct {
LayoutsDir string `yaml:"layouts_dir"` LayoutsDir string `yaml:"layouts_dir"`
DataDir string `yaml:"data_dir"` DataDir string `yaml:"data_dir"`
IncludesDir string `yaml:"includes_dir"` IncludesDir string `yaml:"includes_dir"`
Collections map[string]templates.VariableMap Collections map[string]map[string]interface{}
// Handling Reading // Handling Reading
Include []string Include []string
@ -38,10 +38,10 @@ type Config struct {
Path string Path string
Type string Type string
} }
Values templates.VariableMap Values map[string]interface{}
} }
Variables templates.VariableMap `yaml:"-"` Variables map[string]interface{} `yaml:"-"`
} }
// Default returns a default site configuration. // Default returns a default site configuration.
@ -68,7 +68,7 @@ func Unmarshal(bytes []byte, c *Config) error {
} }
// GetFrontMatterDefaults implements https://jekyllrb.com/docs/configuration/#front-matter-defaults // GetFrontMatterDefaults implements https://jekyllrb.com/docs/configuration/#front-matter-defaults
func (c *Config) GetFrontMatterDefaults(relpath, typename string) (m templates.VariableMap) { func (c *Config) GetFrontMatterDefaults(relpath, typename string) (m map[string]interface{}) {
for _, entry := range c.Defaults { for _, entry := range c.Defaults {
scope := &entry.Scope scope := &entry.Scope
hasPrefix := strings.HasPrefix(relpath, scope.Path) hasPrefix := strings.HasPrefix(relpath, scope.Path)

View File

@ -4,7 +4,6 @@ import (
"io" "io"
"github.com/osteele/gojekyll/pipelines" "github.com/osteele/gojekyll/pipelines"
"github.com/osteele/gojekyll/templates"
) )
// Document is a Jekyll page or file. // Document is a Jekyll page or file.
@ -20,7 +19,7 @@ type Document interface {
Write(RenderingContext, io.Writer) error Write(RenderingContext, io.Writer) error
// Variables // Variables
PageVariables() templates.VariableMap PageVariables() map[string]interface{}
// Document initialization uses this. // Document initialization uses this.
initPermalink() error initPermalink() error

View File

@ -21,7 +21,7 @@ type file struct {
outputExt string outputExt string
permalink string // cached permalink permalink string // cached permalink
fileModTime time.Time fileModTime time.Time
frontMatter templates.VariableMap frontMatter map[string]interface{}
} }
func (p *file) String() string { func (p *file) String() string {
@ -31,11 +31,11 @@ func (p *file) String() string {
func (p *file) OutputExt() string { return p.outputExt } func (p *file) OutputExt() string { return p.outputExt }
func (p *file) Path() string { return p.relpath } func (p *file) Path() string { return p.relpath }
func (p *file) Permalink() string { return p.permalink } func (p *file) Permalink() string { return p.permalink }
func (p *file) Published() bool { return p.frontMatter.Bool("published", true) } func (p *file) Published() bool { return templates.VariableMap(p.frontMatter).Bool("published", true) }
func (p *file) SiteRelPath() string { return p.relpath } func (p *file) SiteRelPath() string { return p.relpath }
// NewFile creates a Post or StaticFile. // NewFile creates a Post or StaticFile.
func NewFile(filename string, c Container, relpath string, defaults templates.VariableMap) (Document, error) { func NewFile(filename string, c Container, relpath string, defaults map[string]interface{}) (Document, error) {
magic, err := helpers.ReadFileMagic(filename) magic, err := helpers.ReadFileMagic(filename)
if err != nil { if err != nil {
return nil, err return nil, err
@ -72,14 +72,14 @@ func NewFile(filename string, c Container, relpath string, defaults templates.Va
// Variables returns the attributes of the template page object. // Variables returns the attributes of the template page object.
// See https://jekyllrb.com/docs/variables/#page-variables // See https://jekyllrb.com/docs/variables/#page-variables
func (p *file) PageVariables() templates.VariableMap { func (p *file) PageVariables() map[string]interface{} {
var ( var (
relpath = "/" + filepath.ToSlash(p.relpath) relpath = "/" + filepath.ToSlash(p.relpath)
base = path.Base(relpath) base = path.Base(relpath)
ext = path.Ext(relpath) ext = path.Ext(relpath)
) )
return templates.MergeVariableMaps(p.frontMatter, templates.VariableMap{ return templates.MergeVariableMaps(p.frontMatter, map[string]interface{}{
"path": relpath, "path": relpath,
"modified_time": p.fileModTime, "modified_time": p.fileModTime,
"name": base, "name": base,
@ -109,5 +109,5 @@ func (p *file) categories() []string {
panic("unimplemented") panic("unimplemented")
} }
} }
return []string{p.frontMatter.String("category", "")} return []string{templates.VariableMap(p.frontMatter).String("category", "")}
} }

View File

@ -38,7 +38,7 @@ func newPage(filename string, f file) (*Page, error) {
} }
// PageVariables returns the attributes of the template page object. // PageVariables returns the attributes of the template page object.
func (p *Page) PageVariables() templates.VariableMap { func (p *Page) PageVariables() map[string]interface{} {
var ( var (
relpath = p.relpath relpath = p.relpath
ext = filepath.Ext(relpath) ext = filepath.Ext(relpath)
@ -46,7 +46,7 @@ func (p *Page) PageVariables() templates.VariableMap {
base = filepath.Base(root) base = filepath.Base(root)
) )
data := templates.VariableMap{ data := map[string]interface{}{
"path": relpath, "path": relpath,
"url": p.Permalink(), "url": p.Permalink(),
// TODO output // TODO output
@ -88,8 +88,8 @@ func (p *Page) PageVariables() templates.VariableMap {
} }
// TemplateContext returns the local variables for template evaluation // TemplateContext returns the local variables for template evaluation
func (p *Page) TemplateContext(rc RenderingContext) templates.VariableMap { func (p *Page) TemplateContext(rc RenderingContext) map[string]interface{} {
return templates.VariableMap{ return map[string]interface{}{
"page": p.PageVariables(), "page": p.PageVariables(),
"site": rc.SiteVariables(), "site": rc.SiteVariables(),
} }
@ -106,7 +106,7 @@ func (p *Page) Write(rc RenderingContext, w io.Writer) error {
if err != nil { if err != nil {
return err return err
} }
layout := p.frontMatter.String("layout", "") layout := templates.VariableMap(p.frontMatter).String("layout", "")
if layout != "" { if layout != "" {
b, err = rp.ApplyLayout(layout, b, p.TemplateContext(rc)) b, err = rp.ApplyLayout(layout, b, p.TemplateContext(rc))
if err != nil { if err != nil {

View File

@ -10,6 +10,7 @@ import (
"github.com/osteele/gojekyll/constants" "github.com/osteele/gojekyll/constants"
"github.com/osteele/gojekyll/helpers" "github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/templates"
) )
// PermalinkStyles defines built-in styles from https://jekyllrb.com/docs/permalinks/#builtinpermalinkstyles // PermalinkStyles defines built-in styles from https://jekyllrb.com/docs/permalinks/#builtinpermalinkstyles
@ -45,14 +46,15 @@ func (p *file) permalinkTemplateVariables() map[string]string {
categories = p.categories() categories = p.categories()
) )
sort.Strings(categories) sort.Strings(categories)
bindings := templates.VariableMap(p.frontMatter)
// TODO recognize category; list // TODO recognize category; list
vs := map[string]string{ vs := map[string]string{
"categories": strings.Join(categories, "/"), "categories": strings.Join(categories, "/"),
"collection": p.frontMatter.String("collection", ""), "collection": bindings.String("collection", ""),
"name": helpers.Slugify(name), "name": helpers.Slugify(name),
"path": "/" + root, "path": "/" + root,
"slug": p.frontMatter.String("slug", helpers.Slugify(name)), "slug": bindings.String("slug", helpers.Slugify(name)),
"title": p.frontMatter.String("slug", helpers.Slugify(name)), "title": bindings.String("slug", helpers.Slugify(name)),
// The following aren't documented, but is evident // The following aren't documented, but is evident
"output_ext": p.OutputExt(), "output_ext": p.OutputExt(),
"y_day": strconv.Itoa(p.fileModTime.YearDay()), "y_day": strconv.Itoa(p.fileModTime.YearDay()),
@ -64,7 +66,7 @@ func (p *file) permalinkTemplateVariables() map[string]string {
} }
func (p *file) expandPermalink() (s string, err error) { func (p *file) expandPermalink() (s string, err error) {
pattern := p.frontMatter.String("permalink", constants.DefaultPermalinkPattern) pattern := templates.VariableMap(p.frontMatter).String("permalink", constants.DefaultPermalinkPattern)
if p, found := PermalinkStyles[pattern]; found { if p, found := PermalinkStyles[pattern]; found {
pattern = p pattern = p

View File

@ -41,13 +41,13 @@ var collectionTests = []pathTest{
func TestExpandPermalinkPattern(t *testing.T) { func TestExpandPermalinkPattern(t *testing.T) {
var ( var (
c = containerMock{} c = containerMock{}
d = templates.VariableMap{ d = map[string]interface{}{
"categories": "b a", "categories": "b a",
} }
) )
testPermalinkPattern := func(pattern, path string, data templates.VariableMap) (string, error) { testPermalinkPattern := func(pattern, path string, data map[string]interface{}) (string, error) {
vs := templates.MergeVariableMaps(data, templates.VariableMap{"permalink": pattern}) vs := templates.MergeVariableMaps(data, map[string]interface{}{"permalink": pattern})
ext := filepath.Ext(path) ext := filepath.Ext(path)
switch ext { switch ext {
case ".md", ".markdown": case ".md", ".markdown":

View File

@ -12,7 +12,7 @@ import (
) )
// FindLayout returns a template for the named layout. // FindLayout returns a template for the named layout.
func (p *Pipeline) FindLayout(base string, fm *templates.VariableMap) (t liquid.Template, err error) { func (p *Pipeline) FindLayout(base string, fm *map[string]interface{}) (t liquid.Template, err error) {
exts := []string{"", ".html"} exts := []string{"", ".html"}
for _, ext := range strings.SplitN(p.config.MarkdownExt, `,`, -1) { for _, ext := range strings.SplitN(p.config.MarkdownExt, `,`, -1) {
exts = append(exts, "."+ext) exts = append(exts, "."+ext)

View File

@ -14,9 +14,9 @@ import (
// PipelineInterface applies transformations to a document. // PipelineInterface applies transformations to a document.
type PipelineInterface interface { type PipelineInterface interface {
ApplyLayout(string, []byte, templates.VariableMap) ([]byte, error) ApplyLayout(string, []byte, map[string]interface{}) ([]byte, error)
OutputExt(pathname string) string OutputExt(pathname string) string
Render(io.Writer, []byte, string, templates.VariableMap) ([]byte, error) Render(io.Writer, []byte, string, map[string]interface{}) ([]byte, error)
} }
// Pipeline applies a rendering transformation to a file. // Pipeline applies a rendering transformation to a file.
@ -59,7 +59,7 @@ func (p *Pipeline) OutputExt(pathname string) string {
} }
// Render returns nil iff it wrote to the writer // Render returns nil iff it wrote to the writer
func (p *Pipeline) Render(w io.Writer, b []byte, filename string, e templates.VariableMap) ([]byte, error) { func (p *Pipeline) Render(w io.Writer, b []byte, filename string, e map[string]interface{}) ([]byte, error) {
if p.config.IsSassPath(filename) { if p.config.IsSassPath(filename) {
return nil, p.WriteSass(w, b) return nil, p.WriteSass(w, b)
} }
@ -73,7 +73,7 @@ func (p *Pipeline) Render(w io.Writer, b []byte, filename string, e templates.Va
return b, nil return b, nil
} }
func (p *Pipeline) renderTemplate(b []byte, e templates.VariableMap, filename string) ([]byte, error) { func (p *Pipeline) renderTemplate(b []byte, e map[string]interface{}, filename string) ([]byte, error) {
b, err := p.liquidEngine.ParseAndRender(b, e) b, err := p.liquidEngine.ParseAndRender(b, e)
if err != nil { if err != nil {
return nil, helpers.PathError(err, "Liquid Error", filename) return nil, helpers.PathError(err, "Liquid Error", filename)
@ -82,14 +82,14 @@ func (p *Pipeline) renderTemplate(b []byte, e templates.VariableMap, filename st
} }
// ApplyLayout applies the named layout to the data. // ApplyLayout applies the named layout to the data.
func (p *Pipeline) ApplyLayout(name string, data []byte, e templates.VariableMap) ([]byte, error) { func (p *Pipeline) ApplyLayout(name string, data []byte, e map[string]interface{}) ([]byte, error) {
for name != "" { for name != "" {
var lfm templates.VariableMap var lfm map[string]interface{}
t, err := p.FindLayout(name, &lfm) t, err := p.FindLayout(name, &lfm)
if err != nil { if err != nil {
return nil, err return nil, err
} }
le := templates.MergeVariableMaps(e, templates.VariableMap{ le := templates.MergeVariableMaps(e, map[string]interface{}{
"content": string(data), "content": string(data),
"layout": lfm, "layout": lfm,
}) })
@ -97,7 +97,7 @@ func (p *Pipeline) ApplyLayout(name string, data []byte, e templates.VariableMap
if err != nil { if err != nil {
return nil, helpers.PathError(err, "render template", name) return nil, helpers.PathError(err, "render template", name)
} }
name = lfm.String("layout", "") name = templates.VariableMap(lfm).String("layout", "")
} }
return data, nil return data, nil
} }

View File

@ -18,7 +18,7 @@ import (
type Site struct { type Site struct {
ConfigFile *string ConfigFile *string
Collections []*collections.Collection Collections []*collections.Collection
// Variables templates.VariableMap // Variables map[string]interface{}
Routes map[string]pages.Document // URL path -> Page, only for output pages Routes map[string]pages.Document // URL path -> Page, only for output pages
config config.Config config config.Config

View File

@ -17,7 +17,7 @@ func (s *Site) SiteVariables() map[string]interface{} {
} }
func (s *Site) initializeSiteVariables() error { func (s *Site) initializeSiteVariables() error {
s.siteVariables = templates.MergeVariableMaps(s.config.Variables, templates.VariableMap{ s.siteVariables = templates.MergeVariableMaps(s.config.Variables, map[string]interface{}{
"data": s.data, "data": s.data,
// TODO read time from _config, if it's available // TODO read time from _config, if it's available
"time": time.Now(), "time": time.Now(),