1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-22 12:41:13 +01:00

Approximation of excerpts

This commit is contained in:
Oliver Steele 2017-07-06 19:31:36 -04:00
parent 0a6a7326c3
commit d1abd57e7a
11 changed files with 89 additions and 29 deletions

View File

@ -34,16 +34,21 @@ func New(s Site, name string, metadata map[string]interface{}) *Collection {
}
}
// OutputExt is in the page.Container interface.
func (c *Collection) OutputExt(pathname string) string {
return c.site.OutputExt(pathname)
}
// AbsDir is in the page.Container interface.
func (c *Collection) AbsDir() string {
return filepath.Join(c.config.SourceDir(), c.PathPrefix())
}
// Config is in the page.Container interface.
func (c *Collection) Config() *config.Config {
return c.config
}
// OutputExt is in the page.Container interface.
func (c *Collection) OutputExt(pathname string) string {
return c.site.OutputExt(pathname)
}
// PathPrefix is in the page.Container interface.
// PathPrefix returns the collection's directory prefix, e.g. "_posts/"
func (c *Collection) PathPrefix() string { return filepath.FromSlash("_" + c.Name + "/") }

View File

@ -33,6 +33,9 @@ type Config struct {
// Plugins
Plugins []string
// Plugins
ExcerptSeparator string `yaml:"excerpt_separator"`
// Serving
AbsoluteURL string `yaml:"url"`
BaseURL string

View File

@ -44,6 +44,9 @@ strict_front_matter: false
# Plugins
plugins: []
# Conversion
excerpt_separator: "\n\n"
# Outputting
permalink: date
paginate_path: /page:num

View File

@ -1,6 +1,8 @@
package pages
import (
"bytes"
"fmt"
"path"
"path/filepath"
@ -40,11 +42,27 @@ func (p *page) ToLiquid() interface{} {
ext = filepath.Ext(relpath)
root = helpers.TrimExt(p.relpath)
base = filepath.Base(root)
content = p.raw
excerpt string
)
if p.content != nil {
content = *p.content
}
content = bytes.TrimSpace(content)
if ei, ok := p.frontMatter["excerpt"]; ok {
excerpt = fmt.Sprint(ei)
} else {
pos := bytes.Index(content, []byte(p.container.Config().ExcerptSeparator))
if pos < 0 {
pos = len(content)
}
excerpt = string(content[:pos])
}
data := map[string]interface{}{
"path": relpath,
"url": p.Permalink(),
"content": string(content),
"excerpt": excerpt,
"path": relpath,
"url": p.Permalink(),
// TODO output
// not documented, but present in both collection and non-collection pages
@ -80,10 +98,6 @@ func (p *page) ToLiquid() interface{} {
data[k] = v
}
}
if p.content != nil {
data["content"] = string(*p.content)
// TODO excerpt
}
return data
}

19
pages/drops_test.go Normal file
View File

@ -0,0 +1,19 @@
package pages
import (
"testing"
"github.com/osteele/gojekyll/config"
"github.com/stretchr/testify/require"
)
func TestPage_ToLiquid(t *testing.T) {
cfg := config.Default()
page, err := NewFile("testdata/excerpt.md", containerFake{cfg, ""}, "excerpt.md", map[string]interface{}{})
require.NoError(t, err)
drop := page.ToLiquid()
excerpt := drop.(map[string]interface{})["excerpt"]
// FIXME the following probably isn't right
// TODO also test post-rendering.
require.Equal(t, "First line.", excerpt)
}

View File

@ -8,14 +8,14 @@ import (
"github.com/stretchr/testify/require"
)
type containerMock struct {
c config.Config
type containerFake struct {
cfg config.Config
prefix string
}
func (c containerMock) OutputExt(p string) string { return filepath.Ext(p) }
func (c containerMock) PathPrefix() string { return c.prefix }
func (c containerFake) Config() *config.Config { return &c.cfg }
func (c containerFake) PathPrefix() string { return c.prefix }
func (c containerFake) OutputExt(p string) string { return filepath.Ext(p) }
func TestPageCategories(t *testing.T) {
require.Equal(t, []string{"a", "b"}, sortedStringValue("b a"))
@ -23,7 +23,7 @@ func TestPageCategories(t *testing.T) {
require.Equal(t, []string{"a", "b"}, sortedStringValue([]string{"b", "a"}))
require.Equal(t, []string{}, sortedStringValue(3))
c := containerMock{config.Default(), ""}
c := containerFake{config.Default(), ""}
fm := map[string]interface{}{"categories": "b a"}
f := file{container: c, frontMatter: fm}
require.Equal(t, []string{"a", "b"}, f.Categories())

View File

@ -4,6 +4,7 @@ import (
"io"
"time"
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/pipelines"
"github.com/osteele/liquid"
"gopkg.in/yaml.v2"
@ -53,6 +54,7 @@ type RenderingContext interface {
// Container is the document container.
// It's either the Site or Collection that immediately contains the document.
type Container interface {
Config() *config.Config
OutputExt(pathname string) string
PathPrefix() string // PathPrefix is the relative prefix, "" for the site and "_coll/" for a collection
}

View File

@ -5,28 +5,35 @@ import (
"io"
"testing"
"github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/pipelines"
"github.com/stretchr/testify/require"
)
type mockRenderingContext struct{ t *testing.T }
type renderingContextFake struct {
t *testing.T
cfg config.Config
}
func (c mockRenderingContext) RenderingPipeline() pipelines.PipelineInterface { return c }
func (c mockRenderingContext) OutputExt(string) string { return ".html" }
func (c mockRenderingContext) Site() interface{} { return nil }
func (c mockRenderingContext) ApplyLayout(layout string, src []byte, vars map[string]interface{}) ([]byte, error) {
func (c renderingContextFake) RenderingPipeline() pipelines.PipelineInterface { return c }
func (c renderingContextFake) Config() config.Config { return c.cfg }
func (c renderingContextFake) PathPrefix() string { return "." }
func (c renderingContextFake) OutputExt(string) string { return ".html" }
func (c renderingContextFake) Site() interface{} { return nil }
func (c renderingContextFake) ApplyLayout(layout string, src []byte, vars map[string]interface{}) ([]byte, error) {
require.Equal(c.t, "layout1", layout)
return nil, nil
}
func (c mockRenderingContext) Render(w io.Writer, src []byte, filename string, vars map[string]interface{}) ([]byte, error) {
func (c renderingContextFake) Render(w io.Writer, src []byte, filename string, vars map[string]interface{}) ([]byte, error) {
require.Equal(c.t, "testdata/page_with_layout.md", filename)
return nil, nil
}
func TestPageWrite(t *testing.T) {
p, err := NewFile("testdata/page_with_layout.md", containerMock{}, "page_with_layout.md", map[string]interface{}{})
cfg := config.Default()
p, err := NewFile("testdata/page_with_layout.md", containerFake{cfg, ""}, "page_with_layout.md", map[string]interface{}{})
require.NoError(t, err)
require.NotNil(t, p)
buf := new(bytes.Buffer)
require.NoError(t, p.Write(buf, mockRenderingContext{t}))
require.NoError(t, p.Write(buf, renderingContextFake{t, cfg}))
}

View File

@ -36,7 +36,7 @@ var collectionTests = []pathTest{
func TestExpandPermalinkPattern(t *testing.T) {
var (
c = containerMock{config.Default(), ""}
c = containerFake{config.Default(), ""}
d = map[string]interface{}{
"categories": "b a",
}
@ -68,7 +68,7 @@ func TestExpandPermalinkPattern(t *testing.T) {
runTests(tests)
c = containerMock{config.Default(), "_c/"}
c = containerFake{config.Default(), "_c/"}
d["collection"] = "c"
runTests(collectionTests)

6
pages/testdata/excerpt.md vendored Normal file
View File

@ -0,0 +1,6 @@
---
---
First line.
Second line.

View File

@ -19,7 +19,7 @@ func readTestSiteDrop(t *testing.T) map[string]interface{} {
// TODO test cases for collections, categories, tags, data
func TestSite_ToLiquid_documents(t *testing.T) {
func TestSite_ToLiquid(t *testing.T) {
drop := readTestSiteDrop(t)
docs, isTime := drop["documents"].([]pages.Document)
require.True(t, isTime, fmt.Sprintf("documents has type %T", drop["documents"]))
@ -27,6 +27,7 @@ func TestSite_ToLiquid_documents(t *testing.T) {
}
func TestSite_ToLiquid_time(t *testing.T) {
drop := readTestSiteDrop(t)
_, ok := drop["time"].(time.Time)
require.True(t, ok)