1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 07:44:40 +01:00

Add page.IsMarkdown()

This commit is contained in:
Oliver Steele 2017-06-18 22:26:49 -04:00
parent c934c3fa6d
commit 6025509bf9
4 changed files with 46 additions and 40 deletions

View File

@ -20,7 +20,7 @@ type DynamicPage struct {
}
// Static returns a bool indicating that the page is a not static page.
func (p *DynamicPage) Static() bool { return false }
func (page *DynamicPage) Static() bool { return false }
// NewDynamicPage reads the front matter from a file to create a new DynamicPage.
func NewDynamicPage(fields pageFields) (p *DynamicPage, err error) {
@ -64,21 +64,21 @@ func readFrontMatter(sourcePtr *[]byte) (frontMatter VariableMap, err error) {
}
// TemplateObject returns the attributes of the template page object.
func (p *DynamicPage) TemplateObject() VariableMap {
func (page *DynamicPage) TemplateObject() VariableMap {
var (
relpath = p.relpath
relpath = page.relpath
ext = filepath.Ext(relpath)
root = helpers.PathWithoutExtension(p.relpath)
root = helpers.PathWithoutExtension(page.relpath)
base = filepath.Base(root)
)
data := VariableMap{
"path": relpath,
"url": p.Permalink(),
"url": page.Permalink(),
// TODO content output
// not documented, but present in both collection and non-collection pages
"permalink": p.Permalink(),
"permalink": page.Permalink(),
// TODO only in non-collection pages:
// TODO dir
@ -93,13 +93,13 @@ func (p *DynamicPage) TemplateObject() VariableMap {
// TODO slug
// TODO Only present in collection pages https://jekyllrb.com/docs/collections/#documents
"relative_path": p.Path(),
"relative_path": page.Path(),
// TODO collection(name)
// TODO undocumented; only present in collection pages:
"ext": ext,
}
for k, v := range p.frontMatter {
for k, v := range page.frontMatter {
switch k {
// doc implies these aren't present, but they appear to be present in a collection page:
// case "layout", "published":
@ -113,36 +113,36 @@ func (p *DynamicPage) TemplateObject() VariableMap {
}
// TemplateVariables returns the local variables for template evaluation
func (p *DynamicPage) TemplateVariables() VariableMap {
func (page *DynamicPage) TemplateVariables() VariableMap {
return VariableMap{
"page": p.TemplateObject(),
"site": p.site.Variables,
"page": page.TemplateObject(),
"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 (p *DynamicPage) DebugVariables() VariableMap {
return p.TemplateVariables()
func (page *DynamicPage) DebugVariables() VariableMap {
return page.TemplateVariables()
}
// Write applies Liquid and Markdown, as appropriate.
func (p *DynamicPage) Write(w io.Writer) (err error) {
body, err := p.site.LiquidEngine().ParseAndRender(p.Content, p.TemplateVariables())
func (page *DynamicPage) Write(w io.Writer) (err error) {
body, err := page.site.LiquidEngine().ParseAndRender(page.Content, page.TemplateVariables())
if err != nil {
return helpers.PathError(err, "Liquid Error", p.Source())
return helpers.PathError(err, "Liquid Error", page.Source())
}
if p.Site().IsMarkdown(p.relpath) {
if page.IsMarkdown() {
body = blackfriday.MarkdownCommon(body)
body, err = p.applyLayout(p.frontMatter, body)
body, err = page.applyLayout(page.frontMatter, body)
if err != nil {
return
}
}
if p.Site().IsSassPath(p.relpath) {
return p.writeSass(w, body)
if page.Site().IsSassPath(page.relpath) {
return page.writeSass(w, body)
}
_, err = w.Write(body)

View File

@ -43,17 +43,18 @@ func (s *Site) FindLayout(base string, fm *VariableMap) (t liquid.Template, err
return s.LiquidEngine().Parse(content)
}
func (p *DynamicPage) applyLayout(frontMatter VariableMap, body []byte) ([]byte, error) {
func (page *DynamicPage) applyLayout(frontMatter VariableMap, body []byte) ([]byte, error) {
for {
layoutName := frontMatter.String("layout", "")
if layoutName == "" {
break
name := frontMatter.String("layout", "")
println("name", name)
if name == "" {
return body, nil
}
template, err := p.site.FindLayout(layoutName, &frontMatter)
template, err := page.site.FindLayout(name, &frontMatter)
if err != nil {
return nil, err
}
vars := MergeVariableMaps(p.TemplateVariables(), VariableMap{
vars := MergeVariableMaps(page.TemplateVariables(), VariableMap{
"content": string(body),
"layout": frontMatter,
})
@ -61,6 +62,6 @@ func (p *DynamicPage) applyLayout(frontMatter VariableMap, body []byte) ([]byte,
if err != nil {
return nil, err
}
println("layout", name, "->", string(body))
}
return body, nil
}

29
page.go
View File

@ -87,15 +87,6 @@ func ReadPage(site *Site, collection *Collection, relpath string, defaults Varia
return
}
func (p *StaticPage) Write(w io.Writer) error {
source, err := ioutil.ReadFile(p.Source())
if err != nil {
return err
}
_, err = w.Write(source)
return err
}
// TemplateObject returns the attributes of the template page object.
// See https://jekyllrb.com/docs/variables/#page-variables
func (p *pageFields) TemplateObject() VariableMap {
@ -125,15 +116,29 @@ func (p *pageFields) Source() string {
return filepath.Join(p.site.Source, p.relpath)
}
// IsMarkdown returns a bool indicating whether the page is markdown.
func (p *pageFields) IsMarkdown() bool {
return p.site.IsMarkdown(p.relpath)
}
// StaticPage is a static page.
type StaticPage struct {
pageFields
}
// Static returns a bool indicating that the page is a static page.
func (p *StaticPage) Static() bool { return true }
func (page *StaticPage) Static() bool { return true }
// TemplateObject returns metadata for use in the representation of the page as a collection item
func (p *StaticPage) TemplateObject() VariableMap {
return MergeVariableMaps(p.frontMatter, p.pageFields.TemplateObject())
func (page *StaticPage) TemplateObject() VariableMap {
return MergeVariableMaps(page.frontMatter, page.TemplateObject())
}
func (page *StaticPage) Write(w io.Writer) error {
source, err := ioutil.ReadFile(page.Source())
if err != nil {
return err
}
_, err = w.Write(source)
return err
}

View File

@ -47,7 +47,7 @@ func (p *pageFields) permalinkTemplateVariables() map[string]string {
title = p.frontMatter.String("title", name)
)
switch {
case p.site.IsMarkdown(path):
case p.IsMarkdown():
outputExt = ".html"
case p.site.IsSassPath(path):
outputExt = ".css"