mirror of
https://github.com/danog/gojekyll.git
synced 2025-01-22 12:51:24 +01:00
Implement page.content
This commit is contained in:
parent
6938f80fea
commit
8c64cbe6c6
2
build.go
2
build.go
@ -81,6 +81,7 @@ func (site *Site) Build(options BuildOptions) (int, error) {
|
||||
}
|
||||
count += n
|
||||
}
|
||||
site.updateCollectionVariables()
|
||||
n, err := site.WritePages(site, options)
|
||||
return count + n, err
|
||||
}
|
||||
@ -110,6 +111,7 @@ func (site *Site) WritePage(page Page, options BuildOptions) error {
|
||||
fmt.Println("create", to, "from", page.Source())
|
||||
}
|
||||
if options.DryRun {
|
||||
// FIXME render the page in dry run mode, just don't write it
|
||||
return nil
|
||||
}
|
||||
// nolint: gas
|
||||
|
@ -83,7 +83,7 @@ func dataCommand(c *cli.Context, site *gojekyll.Site) error {
|
||||
for _, c := range site.Collections {
|
||||
siteData[c.Name] = fmt.Sprintf("<elided page data for %d items>", len(siteData[c.Name].([]gojekyll.VariableMap)))
|
||||
}
|
||||
b, _ := yaml.Marshal(p.DebugVariables())
|
||||
b, _ := yaml.Marshal(p.Variables())
|
||||
fmt.Println(string(b))
|
||||
return nil
|
||||
}
|
||||
@ -111,6 +111,9 @@ func renderCommand(c *cli.Context, site *gojekyll.Site) error {
|
||||
printPathSetting("Render:", filepath.Join(site.Source, page.Path()))
|
||||
printSetting("URL:", page.Permalink())
|
||||
printSetting("Content:", "")
|
||||
if err := site.CreateCollectionContent(); err != nil {
|
||||
return err
|
||||
}
|
||||
return page.Write(os.Stdout)
|
||||
}
|
||||
|
||||
|
@ -30,7 +30,7 @@ func NewCollection(s *Site, name string, d VariableMap) *Collection {
|
||||
// value of the collection.
|
||||
func (coll *Collection) CollectionValue() (d []VariableMap) {
|
||||
for _, page := range coll.Pages() {
|
||||
d = append(d, page.TemplateObject())
|
||||
d = append(d, page.Variables())
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -16,7 +16,8 @@ import (
|
||||
// DynamicPage is a static page, that includes frontmatter.
|
||||
type DynamicPage struct {
|
||||
pageFields
|
||||
Content []byte
|
||||
raw []byte
|
||||
processed *[]byte
|
||||
}
|
||||
|
||||
// Static returns a bool indicating that the page is a not static page.
|
||||
@ -38,7 +39,7 @@ func NewDynamicPage(fields pageFields) (p *DynamicPage, err error) {
|
||||
fields.frontMatter = MergeVariableMaps(fields.frontMatter, frontMatter)
|
||||
return &DynamicPage{
|
||||
pageFields: fields,
|
||||
Content: data,
|
||||
raw: data,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -63,19 +64,25 @@ func readFrontMatter(sourcePtr *[]byte) (frontMatter VariableMap, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// TemplateObject returns the attributes of the template page object.
|
||||
func (page *DynamicPage) TemplateObject() VariableMap {
|
||||
// Variables returns the attributes of the template page object.
|
||||
func (page *DynamicPage) Variables() VariableMap {
|
||||
var (
|
||||
relpath = page.relpath
|
||||
ext = filepath.Ext(relpath)
|
||||
root = helpers.PathWithoutExtension(page.relpath)
|
||||
base = filepath.Base(root)
|
||||
content = page.processed
|
||||
)
|
||||
|
||||
if content == nil {
|
||||
content = &[]byte{}
|
||||
}
|
||||
|
||||
data := VariableMap{
|
||||
"path": relpath,
|
||||
"url": page.Permalink(),
|
||||
// TODO content output
|
||||
"path": relpath,
|
||||
"url": page.Permalink(),
|
||||
"content": content,
|
||||
// TODO output
|
||||
|
||||
// not documented, but present in both collection and non-collection pages
|
||||
"permalink": page.Permalink(),
|
||||
@ -112,20 +119,14 @@ func (page *DynamicPage) TemplateObject() VariableMap {
|
||||
return data
|
||||
}
|
||||
|
||||
// TemplateVariables returns the local variables for template evaluation
|
||||
func (page *DynamicPage) TemplateVariables() VariableMap {
|
||||
// Context returns the local variables for template evaluation
|
||||
func (page *DynamicPage) Context() VariableMap {
|
||||
return VariableMap{
|
||||
"page": page.TemplateObject(),
|
||||
"page": page.Variables(),
|
||||
"site": page.site.Variables,
|
||||
}
|
||||
}
|
||||
|
||||
// DebugVariables returns a map that's useful to present during diagnostics.
|
||||
// For a dynamic page, this is the local variable map that is used for template evaluation.
|
||||
func (page *DynamicPage) DebugVariables() VariableMap {
|
||||
return page.TemplateVariables()
|
||||
}
|
||||
|
||||
// Output returns a bool indicating whether the page should be written.
|
||||
func (page *DynamicPage) Output() bool {
|
||||
return page.pageFields.Output() && (page.collection == nil || page.collection.Output)
|
||||
@ -133,7 +134,7 @@ func (page *DynamicPage) Output() bool {
|
||||
|
||||
// Write applies Liquid and Markdown, as appropriate.
|
||||
func (page *DynamicPage) Write(w io.Writer) (err error) {
|
||||
body, err := page.site.LiquidEngine().ParseAndRender(page.Content, page.TemplateVariables())
|
||||
body, err := page.site.LiquidEngine().ParseAndRender(page.raw, page.Context())
|
||||
if err != nil {
|
||||
return helpers.PathError(err, "Liquid Error", page.Source())
|
||||
}
|
||||
@ -150,6 +151,7 @@ func (page *DynamicPage) Write(w io.Writer) (err error) {
|
||||
return page.writeSass(w, body)
|
||||
}
|
||||
|
||||
page.processed = &body
|
||||
_, err = w.Write(body)
|
||||
return
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ func (page *DynamicPage) applyLayout(frontMatter VariableMap, body []byte) ([]by
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
vars := MergeVariableMaps(page.TemplateVariables(), VariableMap{
|
||||
vars := MergeVariableMaps(page.Variables(), VariableMap{
|
||||
"content": string(body),
|
||||
"layout": frontMatter,
|
||||
})
|
||||
|
17
page.go
17
page.go
@ -36,8 +36,7 @@ type Page interface {
|
||||
Write(io.Writer) error
|
||||
|
||||
// Variables
|
||||
DebugVariables() VariableMap
|
||||
TemplateObject() VariableMap
|
||||
Variables() VariableMap
|
||||
|
||||
// internal
|
||||
initPermalink() error
|
||||
@ -109,9 +108,9 @@ func ReadPage(site *Site, collection *Collection, relpath string, defaults Varia
|
||||
return
|
||||
}
|
||||
|
||||
// TemplateObject 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
|
||||
func (p *pageFields) TemplateObject() VariableMap {
|
||||
func (p *pageFields) Variables() VariableMap {
|
||||
var (
|
||||
relpath = "/" + filepath.ToSlash(p.relpath)
|
||||
base = path.Base(relpath)
|
||||
@ -127,12 +126,6 @@ func (p *pageFields) TemplateObject() VariableMap {
|
||||
}
|
||||
}
|
||||
|
||||
// DebugVariables returns a map that's useful to present during diagnostics.
|
||||
// For a static page, this is just the page's template object attributes.
|
||||
func (p *pageFields) DebugVariables() VariableMap {
|
||||
return p.TemplateObject()
|
||||
}
|
||||
|
||||
// Source returns the file path of the page source.
|
||||
func (p *pageFields) Source() string {
|
||||
return filepath.Join(p.site.Source, p.relpath)
|
||||
@ -152,8 +145,8 @@ type StaticPage struct {
|
||||
func (page *StaticPage) Static() bool { return true }
|
||||
|
||||
// TemplateObject returns metadata for use in the representation of the page as a collection item
|
||||
func (page *StaticPage) TemplateObject() VariableMap {
|
||||
return MergeVariableMaps(page.frontMatter, page.pageFields.TemplateObject())
|
||||
func (page *StaticPage) Variables() VariableMap {
|
||||
return MergeVariableMaps(page.frontMatter, page.pageFields.Variables())
|
||||
}
|
||||
|
||||
func (page *StaticPage) Write(w io.Writer) error {
|
||||
|
60
site.go
60
site.go
@ -1,15 +1,11 @@
|
||||
package gojekyll
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/osteele/gojekyll/helpers"
|
||||
"github.com/osteele/gojekyll/liquid"
|
||||
@ -200,58 +196,18 @@ func (site *Site) ReadCollections() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (site *Site) initSiteVariables() error {
|
||||
data, err := site.readDataFiles()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
site.Variables = MergeVariableMaps(site.Variables, VariableMap{
|
||||
"data": data,
|
||||
// TODO read time from _config, if it's available
|
||||
"time": time.Now(),
|
||||
// TODO pages, posts, related_posts, static_files, html_pages, html_files, collections, data, documents, categories.CATEGORY, tags.TAG
|
||||
})
|
||||
for _, c := range site.Collections {
|
||||
site.Variables[c.Name] = c.CollectionValue()
|
||||
func (site *Site) CreateCollectionContent() error {
|
||||
for _, coll := range site.Collections {
|
||||
for _, p := range coll.Pages() {
|
||||
if err := p.Write(ioutil.Discard); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
site.updateCollectionVariables()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (site *Site) readDataFiles() (VariableMap, error) {
|
||||
data := VariableMap{}
|
||||
dataDir := filepath.Join(site.Source, site.config.DataDir)
|
||||
files, err := ioutil.ReadDir(dataDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, f := range files {
|
||||
if f.IsDir() {
|
||||
break
|
||||
}
|
||||
filename := filepath.Join(dataDir, f.Name())
|
||||
switch filepath.Ext(f.Name()) {
|
||||
case ".yaml", ".yml":
|
||||
bytes, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fileData := map[interface{}]interface{}{}
|
||||
err = yaml.Unmarshal(bytes, &fileData)
|
||||
switch err.(type) {
|
||||
case *yaml.TypeError:
|
||||
fmt.Printf("Warning: skipping %s because it is a list\n", filename)
|
||||
fmt.Println("See https://github.com/go-yaml/yaml/issues/20")
|
||||
default:
|
||||
if err != nil {
|
||||
return nil, helpers.PathError(err, "read YAML", filename)
|
||||
}
|
||||
data[filepath.Base(f.Name())] = fileData
|
||||
}
|
||||
}
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (site *Site) makeLocalLiquidEngine() liquid.Engine {
|
||||
engine := liquid.NewLocalWrapperEngine()
|
||||
engine.LinkTagHandler(site.GetFileURL)
|
||||
|
67
site_variables.go
Normal file
67
site_variables.go
Normal file
@ -0,0 +1,67 @@
|
||||
package gojekyll
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/osteele/gojekyll/helpers"
|
||||
yaml "gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
func (site *Site) initSiteVariables() error {
|
||||
data, err := site.readDataFiles()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
site.Variables = MergeVariableMaps(site.Variables, VariableMap{
|
||||
"data": data,
|
||||
// TODO read time from _config, if it's available
|
||||
"time": time.Now(),
|
||||
// TODO pages, posts, related_posts, static_files, html_pages, html_files, collections, data, documents, categories.CATEGORY, tags.TAG
|
||||
})
|
||||
site.updateCollectionVariables()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (site *Site) updateCollectionVariables() {
|
||||
for _, c := range site.Collections {
|
||||
site.Variables[c.Name] = c.CollectionValue()
|
||||
}
|
||||
}
|
||||
|
||||
func (site *Site) readDataFiles() (VariableMap, error) {
|
||||
data := VariableMap{}
|
||||
dataDir := filepath.Join(site.Source, site.config.DataDir)
|
||||
files, err := ioutil.ReadDir(dataDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, f := range files {
|
||||
if f.IsDir() {
|
||||
break
|
||||
}
|
||||
filename := filepath.Join(dataDir, f.Name())
|
||||
switch filepath.Ext(f.Name()) {
|
||||
case ".yaml", ".yml":
|
||||
bytes, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fileData := map[interface{}]interface{}{}
|
||||
err = yaml.Unmarshal(bytes, &fileData)
|
||||
switch err.(type) {
|
||||
case *yaml.TypeError:
|
||||
fmt.Printf("Warning: skipping %s because it is a list\n", filename)
|
||||
fmt.Println("See https://github.com/go-yaml/yaml/issues/20")
|
||||
default:
|
||||
if err != nil {
|
||||
return nil, helpers.PathError(err, "read YAML", filename)
|
||||
}
|
||||
data[filepath.Base(f.Name())] = fileData
|
||||
}
|
||||
}
|
||||
}
|
||||
return data, nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user