1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 07:48:58 +01:00
gojekyll/renderers/renderers.go

99 lines
2.6 KiB
Go
Raw Normal View History

2017-08-18 17:07:01 +02:00
package renderers
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/danog/gojekyll/config"
"github.com/danog/gojekyll/filters"
"github.com/danog/gojekyll/tags"
"github.com/danog/gojekyll/utils"
"github.com/danog/liquid"
2017-06-24 19:30:01 +02:00
)
2017-08-18 17:07:01 +02:00
// Renderers applies transformations to a document.
type Renderers interface {
2017-08-10 16:44:04 +02:00
ApplyLayout(string, []byte, liquid.Bindings) ([]byte, error)
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-08-18 17:07:01 +02:00
// Manager applies a rendering transformation to a file.
type Manager struct {
Options
2017-07-24 14:18:05 +02:00
cfg config.Config
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
}
2017-08-18 17:07:01 +02:00
// Options configures a rendering manager.
type Options 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
}
2017-08-18 17:07:01 +02:00
// New makes a rendering manager.
func New(c config.Config, options Options) (*Manager, error) {
p := Manager{Options: options, cfg: c}
2017-06-29 13:27:43 +02:00
p.liquidEngine = p.makeLiquidEngine()
2017-08-27 19:40:54 +02:00
if err := p.copySASSFileIncludes(); err != nil {
2017-06-24 19:30:01 +02:00
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-18 17:07:01 +02:00
func (p *Manager) 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-08-18 17:07:01 +02:00
func (p *Manager) TemplateEngine() *liquid.Engine {
2017-06-30 18:53:34 +02:00
return p.liquidEngine
}
2017-08-10 16:44:04 +02:00
// Render sends content through SASS and/or Liquid -> Markdown
2017-08-18 17:07:01 +02:00
func (p *Manager) 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-22 16:47:34 +02:00
src, err = renderMarkdown(src)
if err != nil {
return err
}
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
2017-08-18 17:07:01 +02:00
func (p *Manager) RenderTemplate(src []byte, vars liquid.Bindings, filename string, lineNo int) ([]byte, error) {
tpl, err := p.liquidEngine.ParseTemplateLocation(src, filename, lineNo)
2017-07-04 23:13:47 +02:00
if err != nil {
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 {
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-08-18 17:07:01 +02:00
func (p *Manager) 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
}