1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 07:18:59 +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/.
type Collection struct {
Name string
Metadata templates.VariableMap
Metadata map[string]interface{}
container pages.Container
pages []pages.Document
}
// 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{
Name: name,
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" }
// 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.
func (c *Collection) Pages() []pages.Document {
@ -62,7 +62,7 @@ func (c *Collection) TemplateVariable(ctx pages.RenderingContext, includeContent
if err != nil {
return nil, err
}
v = templates.MergeVariableMaps(v, templates.VariableMap{
v = templates.MergeVariableMaps(v, map[string]interface{}{
"content": string(c),
})
}
@ -84,12 +84,12 @@ func (c *Collection) PermalinkPattern() string {
if c.IsPostsCollection() {
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.
func (c *Collection) ReadPages(sitePath string, frontMatterDefaults func(string, string) templates.VariableMap) error {
pageDefaults := templates.VariableMap{
func (c *Collection) ReadPages(sitePath string, frontMatterDefaults func(string, string) map[string]interface{}) error {
pageDefaults := map[string]interface{}{
"collection": c.Name,
"permalink": c.PermalinkPattern(),
}

View File

@ -4,7 +4,6 @@ import (
"path"
"testing"
"github.com/osteele/gojekyll/templates"
"github.com/stretchr/testify/require"
)
@ -19,36 +18,36 @@ type MockContainer struct{}
func (c MockContainer) PathPrefix() string { return "" }
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")
// }
// 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")
// }
// 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) {
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, "_c/", c1.PathPrefix())
c2 := NewCollection("c", templates.VariableMap{}, ctx)
c2 := NewCollection("c", map[string]interface{}{}, ctx)
require.Equal(t, false, c2.Output())
}
func TestPermalinkPattern(t *testing.T) {
ctx := MockContainer{}
c1 := NewCollection("c", templates.VariableMap{}, ctx)
c1 := NewCollection("c", map[string]interface{}{}, ctx)
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())
c3 := NewCollection("posts", templates.VariableMap{}, ctx)
c3 := NewCollection("posts", map[string]interface{}{}, ctx)
require.Contains(t, c3.PermalinkPattern(), "/:year/:month/:day/:title")
}

View File

@ -16,7 +16,7 @@ type Config struct {
LayoutsDir string `yaml:"layouts_dir"`
DataDir string `yaml:"data_dir"`
IncludesDir string `yaml:"includes_dir"`
Collections map[string]templates.VariableMap
Collections map[string]map[string]interface{}
// Handling Reading
Include []string
@ -38,10 +38,10 @@ type Config struct {
Path 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.
@ -68,7 +68,7 @@ func Unmarshal(bytes []byte, c *Config) error {
}
// 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 {
scope := &entry.Scope
hasPrefix := strings.HasPrefix(relpath, scope.Path)

View File

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

View File

@ -21,7 +21,7 @@ type file struct {
outputExt string
permalink string // cached permalink
fileModTime time.Time
frontMatter templates.VariableMap
frontMatter map[string]interface{}
}
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) Path() string { return p.relpath }
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 }
// 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)
if err != nil {
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.
// See https://jekyllrb.com/docs/variables/#page-variables
func (p *file) PageVariables() templates.VariableMap {
func (p *file) PageVariables() map[string]interface{} {
var (
relpath = "/" + filepath.ToSlash(p.relpath)
base = path.Base(relpath)
ext = path.Ext(relpath)
)
return templates.MergeVariableMaps(p.frontMatter, templates.VariableMap{
return templates.MergeVariableMaps(p.frontMatter, map[string]interface{}{
"path": relpath,
"modified_time": p.fileModTime,
"name": base,
@ -109,5 +109,5 @@ func (p *file) categories() []string {
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.
func (p *Page) PageVariables() templates.VariableMap {
func (p *Page) PageVariables() map[string]interface{} {
var (
relpath = p.relpath
ext = filepath.Ext(relpath)
@ -46,7 +46,7 @@ func (p *Page) PageVariables() templates.VariableMap {
base = filepath.Base(root)
)
data := templates.VariableMap{
data := map[string]interface{}{
"path": relpath,
"url": p.Permalink(),
// TODO output
@ -88,8 +88,8 @@ func (p *Page) PageVariables() templates.VariableMap {
}
// TemplateContext returns the local variables for template evaluation
func (p *Page) TemplateContext(rc RenderingContext) templates.VariableMap {
return templates.VariableMap{
func (p *Page) TemplateContext(rc RenderingContext) map[string]interface{} {
return map[string]interface{}{
"page": p.PageVariables(),
"site": rc.SiteVariables(),
}
@ -106,7 +106,7 @@ func (p *Page) Write(rc RenderingContext, w io.Writer) error {
if err != nil {
return err
}
layout := p.frontMatter.String("layout", "")
layout := templates.VariableMap(p.frontMatter).String("layout", "")
if layout != "" {
b, err = rp.ApplyLayout(layout, b, p.TemplateContext(rc))
if err != nil {

View File

@ -10,6 +10,7 @@ import (
"github.com/osteele/gojekyll/constants"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/gojekyll/templates"
)
// 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()
)
sort.Strings(categories)
bindings := templates.VariableMap(p.frontMatter)
// TODO recognize category; list
vs := map[string]string{
"categories": strings.Join(categories, "/"),
"collection": p.frontMatter.String("collection", ""),
"collection": bindings.String("collection", ""),
"name": helpers.Slugify(name),
"path": "/" + root,
"slug": p.frontMatter.String("slug", helpers.Slugify(name)),
"title": p.frontMatter.String("slug", helpers.Slugify(name)),
"slug": bindings.String("slug", helpers.Slugify(name)),
"title": bindings.String("slug", helpers.Slugify(name)),
// The following aren't documented, but is evident
"output_ext": p.OutputExt(),
"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) {
pattern := p.frontMatter.String("permalink", constants.DefaultPermalinkPattern)
pattern := templates.VariableMap(p.frontMatter).String("permalink", constants.DefaultPermalinkPattern)
if p, found := PermalinkStyles[pattern]; found {
pattern = p

View File

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

View File

@ -12,7 +12,7 @@ import (
)
// 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"}
for _, ext := range strings.SplitN(p.config.MarkdownExt, `,`, -1) {
exts = append(exts, "."+ext)

View File

@ -14,9 +14,9 @@ import (
// PipelineInterface applies transformations to a document.
type PipelineInterface interface {
ApplyLayout(string, []byte, templates.VariableMap) ([]byte, error)
ApplyLayout(string, []byte, map[string]interface{}) ([]byte, error)
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.
@ -59,7 +59,7 @@ func (p *Pipeline) OutputExt(pathname string) string {
}
// 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) {
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
}
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)
if err != nil {
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.
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 != "" {
var lfm templates.VariableMap
var lfm map[string]interface{}
t, err := p.FindLayout(name, &lfm)
if err != nil {
return nil, err
}
le := templates.MergeVariableMaps(e, templates.VariableMap{
le := templates.MergeVariableMaps(e, map[string]interface{}{
"content": string(data),
"layout": lfm,
})
@ -97,7 +97,7 @@ func (p *Pipeline) ApplyLayout(name string, data []byte, e templates.VariableMap
if err != nil {
return nil, helpers.PathError(err, "render template", name)
}
name = lfm.String("layout", "")
name = templates.VariableMap(lfm).String("layout", "")
}
return data, nil
}

View File

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

View File

@ -17,7 +17,7 @@ func (s *Site) SiteVariables() map[string]interface{} {
}
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,
// TODO read time from _config, if it's available
"time": time.Now(),