2017-06-24 20:00:19 +02:00
|
|
|
package pipelines
|
2017-06-24 19:30:01 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2017-07-24 15:32:57 +02:00
|
|
|
"path/filepath"
|
2017-06-24 19:30:01 +02:00
|
|
|
|
|
|
|
"github.com/osteele/gojekyll/config"
|
2017-07-01 01:40:52 +02:00
|
|
|
"github.com/osteele/gojekyll/filters"
|
|
|
|
"github.com/osteele/gojekyll/tags"
|
2017-07-09 22:17:20 +02:00
|
|
|
"github.com/osteele/gojekyll/utils"
|
2017-07-01 01:37:31 +02:00
|
|
|
"github.com/osteele/liquid"
|
2017-06-24 19:30:01 +02:00
|
|
|
)
|
|
|
|
|
2017-06-24 20:00:19 +02:00
|
|
|
// PipelineInterface applies transformations to a document.
|
|
|
|
type PipelineInterface interface {
|
2017-08-10 16:44:04 +02:00
|
|
|
ApplyLayout(string, []byte, liquid.Bindings) ([]byte, error)
|
2017-06-24 20:00:19 +02:00
|
|
|
OutputExt(pathname string) string
|
2017-08-10 16:44:04 +02:00
|
|
|
Render(io.Writer, []byte, liquid.Bindings, string, int) error
|
|
|
|
RenderTemplate([]byte, liquid.Bindings, string, int) ([]byte, error)
|
2017-06-24 20:00:19 +02:00
|
|
|
}
|
|
|
|
|
2017-06-24 19:30:01 +02:00
|
|
|
// Pipeline applies a rendering transformation to a file.
|
|
|
|
type Pipeline struct {
|
2017-06-29 13:41:14 +02:00
|
|
|
PipelineOptions
|
2017-07-24 14:18:05 +02:00
|
|
|
cfg config.Config
|
2017-07-10 15:17:31 +02:00
|
|
|
liquidEngine *liquid.Engine
|
2017-06-24 19:30:01 +02:00
|
|
|
sassTempDir string
|
2017-07-10 00:19:22 +02:00
|
|
|
sassHash string
|
2017-06-24 19:30:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PipelineOptions configures a pipeline.
|
|
|
|
type PipelineOptions struct {
|
2017-07-01 01:37:31 +02:00
|
|
|
RelativeFilenameToURL tags.LinkTagHandler
|
2017-07-24 14:09:14 +02:00
|
|
|
ThemeDir string
|
2017-06-24 19:30:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPipeline makes a rendering pipeline.
|
2017-06-29 13:41:14 +02:00
|
|
|
func NewPipeline(c config.Config, options PipelineOptions) (*Pipeline, error) {
|
2017-07-24 14:18:05 +02:00
|
|
|
p := Pipeline{PipelineOptions: options, cfg: c}
|
2017-06-29 13:27:43 +02:00
|
|
|
p.liquidEngine = p.makeLiquidEngine()
|
2017-06-24 19:30:01 +02:00
|
|
|
if err := p.CopySassFileIncludes(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &p, nil
|
|
|
|
}
|
|
|
|
|
2017-08-10 16:44:04 +02:00
|
|
|
// sourceDir returns the site source directory. Seeing how far we can bend
|
2017-07-01 05:10:58 +02:00
|
|
|
// the Law of Demeter.
|
2017-08-10 16:44:04 +02:00
|
|
|
func (p *Pipeline) sourceDir() string {
|
2017-07-24 14:18:05 +02:00
|
|
|
return p.cfg.Source
|
2017-07-01 03:06:12 +02:00
|
|
|
}
|
|
|
|
|
2017-07-01 01:40:52 +02:00
|
|
|
// TemplateEngine returns the Liquid engine.
|
2017-07-10 15:17:31 +02:00
|
|
|
func (p *Pipeline) TemplateEngine() *liquid.Engine {
|
2017-06-30 18:53:34 +02:00
|
|
|
return p.liquidEngine
|
|
|
|
}
|
|
|
|
|
2017-06-24 19:30:01 +02:00
|
|
|
// OutputExt returns the output extension.
|
|
|
|
func (p *Pipeline) OutputExt(pathname string) string {
|
2017-07-24 14:18:05 +02:00
|
|
|
return p.cfg.OutputExt(pathname)
|
2017-06-24 19:30:01 +02:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:44:04 +02:00
|
|
|
// Render sends content through SASS and/or Liquid -> Markdown
|
|
|
|
func (p *Pipeline) Render(w io.Writer, src []byte, vars liquid.Bindings, filename string, lineNo int) error {
|
2017-07-24 14:18:05 +02:00
|
|
|
if p.cfg.IsSASSPath(filename) {
|
2017-08-10 16:44:04 +02:00
|
|
|
return p.WriteSass(w, src)
|
2017-06-24 19:30:01 +02:00
|
|
|
}
|
2017-08-10 16:44:04 +02:00
|
|
|
src, err := p.RenderTemplate(src, vars, filename, lineNo)
|
2017-06-24 19:30:01 +02:00
|
|
|
if err != nil {
|
2017-08-10 15:06:53 +02:00
|
|
|
return err
|
2017-06-24 19:30:01 +02:00
|
|
|
}
|
2017-07-24 14:18:05 +02:00
|
|
|
if p.cfg.IsMarkdown(filename) {
|
2017-08-10 16:44:04 +02:00
|
|
|
src = markdownRenderer(src)
|
2017-06-24 19:30:01 +02:00
|
|
|
}
|
2017-08-10 16:44:04 +02:00
|
|
|
_, err = w.Write(src)
|
2017-08-10 15:06:53 +02:00
|
|
|
return err
|
2017-06-24 19:30:01 +02:00
|
|
|
}
|
|
|
|
|
2017-08-10 16:44:04 +02:00
|
|
|
// RenderTemplate renders a Liquid template
|
|
|
|
func (p *Pipeline) RenderTemplate(src []byte, vars liquid.Bindings, filename string, lineNo int) ([]byte, error) {
|
2017-07-14 16:57:14 +02:00
|
|
|
tpl, err := p.liquidEngine.ParseTemplateLocation(src, filename, lineNo)
|
2017-07-04 23:13:47 +02:00
|
|
|
if err != nil {
|
2017-07-12 13:27:46 +02:00
|
|
|
return nil, utils.WrapPathError(err, filename)
|
2017-07-04 23:13:47 +02:00
|
|
|
}
|
2017-08-10 16:44:04 +02:00
|
|
|
out, err := tpl.Render(vars)
|
2017-06-24 19:30:01 +02:00
|
|
|
if err != nil {
|
2017-07-12 13:27:46 +02:00
|
|
|
return nil, utils.WrapPathError(err, filename)
|
2017-06-24 19:30:01 +02:00
|
|
|
}
|
2017-07-02 05:54:12 +02:00
|
|
|
return out, err
|
2017-06-24 19:30:01 +02:00
|
|
|
}
|
|
|
|
|
2017-07-10 15:17:31 +02:00
|
|
|
func (p *Pipeline) makeLiquidEngine() *liquid.Engine {
|
2017-07-24 15:32:57 +02:00
|
|
|
dirs := []string{filepath.Join(p.cfg.Source, p.cfg.IncludesDir)}
|
|
|
|
if p.ThemeDir != "" {
|
|
|
|
dirs = append(dirs, filepath.Join(p.ThemeDir, "_includes"))
|
|
|
|
}
|
2017-06-29 13:27:43 +02:00
|
|
|
engine := liquid.NewEngine()
|
2017-07-24 14:18:05 +02:00
|
|
|
filters.AddJekyllFilters(engine, &p.cfg)
|
2017-07-24 15:32:57 +02:00
|
|
|
tags.AddJekyllTags(engine, &p.cfg, dirs, p.RelativeFilenameToURL)
|
2017-06-24 19:30:01 +02:00
|
|
|
return engine
|
|
|
|
}
|