Skip to content

Commit

Permalink
Approximation of excerpts
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jul 6, 2017
1 parent 0a6a732 commit d1abd57
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 29 deletions.
15 changes: 10 additions & 5 deletions collection/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 + "/") }
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ type Config struct {
// Plugins
Plugins []string

// Plugins
ExcerptSeparator string `yaml:"excerpt_separator"`

// Serving
AbsoluteURL string `yaml:"url"`
BaseURL string
Expand Down
3 changes: 3 additions & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ strict_front_matter: false
# Plugins
plugins: []
# Conversion
excerpt_separator: "\n\n"
# Outputting
permalink: date
paginate_path: /page:num
Expand Down
28 changes: 21 additions & 7 deletions pages/drops.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pages

import (
"bytes"
"fmt"
"path"
"path/filepath"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
19 changes: 19 additions & 0 deletions pages/drops_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 6 additions & 6 deletions pages/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ 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"))
require.Equal(t, []string{"a", "b"}, sortedStringValue([]interface{}{"b", "a"}))
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())
Expand Down
2 changes: 2 additions & 0 deletions pages/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
23 changes: 15 additions & 8 deletions pages/page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}))
}
4 changes: 2 additions & 2 deletions pages/permalinks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions pages/testdata/excerpt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
---

First line.

Second line.
3 changes: 2 additions & 1 deletion site/drop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ 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"]))
require.Len(t, docs, 4)
}
func TestSite_ToLiquid_time(t *testing.T) {
drop := readTestSiteDrop(t)

_, ok := drop["time"].(time.Time)
require.True(t, ok)

Expand Down

0 comments on commit d1abd57

Please sign in to comment.