1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 21:07:48 +01:00
gojekyll/config/config.go

122 lines
2.8 KiB
Go
Raw Normal View History

package config
import (
"strings"
"github.com/osteele/gojekyll/templates"
2017-07-09 22:17:20 +02:00
"github.com/osteele/gojekyll/utils"
yaml "gopkg.in/yaml.v2"
)
// Config is the Jekyll site configuration, typically read from _config.yml.
// See https://jekyllrb.com/docs/configuration/#default-configuration
type Config struct {
// Where things are:
Source string
Destination string
2017-07-07 20:55:03 +02:00
LayoutsDir string `yaml:"layouts_dir"`
DataDir string `yaml:"data_dir"`
IncludesDir string `yaml:"includes_dir"`
Collections map[string]map[string]interface{} `yaml:"-"`
// Handling Reading
Include []string
Exclude []string
2017-07-02 01:42:48 +02:00
KeepFiles []string `yaml:"keep_files"`
MarkdownExt string `yaml:"markdown_ext"`
// Filtering Content
Drafts bool `yaml:"show_drafts"`
Future bool
Unpublished bool
2017-06-30 18:53:34 +02:00
// Plugins
2017-07-02 01:42:48 +02:00
Plugins []string
2017-06-30 18:53:34 +02:00
2017-07-07 01:31:36 +02:00
// Plugins
ExcerptSeparator string `yaml:"excerpt_separator"`
2017-06-28 22:55:15 +02:00
// Serving
2017-07-07 21:28:51 +02:00
Host string
Port int
2017-06-28 22:55:15 +02:00
AbsoluteURL string `yaml:"url"`
BaseURL string
// Outputting
2017-07-02 01:42:48 +02:00
Permalink string
2017-06-17 05:30:10 +02:00
Defaults []struct {
Scope struct {
Path string
Type string
}
2017-07-01 05:56:29 +02:00
Values map[string]interface{}
2017-06-17 05:30:10 +02:00
}
2017-07-01 05:56:29 +02:00
Variables map[string]interface{} `yaml:"-"`
}
2017-07-03 17:48:06 +02:00
type configCompat struct {
Gems []string
}
2017-07-07 20:55:03 +02:00
type collectionsList struct {
Collections []string
}
type collectionsMap struct {
Collections map[string]map[string]interface{}
}
2017-07-03 17:48:06 +02:00
// SourceDir returns the source directory as an absolute path.
func (c *Config) SourceDir() string {
2017-07-09 22:17:20 +02:00
return utils.MustAbs(c.Source)
}
// GetFrontMatterDefaults implements https://jekyllrb.com/docs/configuration/#front-matter-defaults
2017-07-03 17:48:06 +02:00
func (c *Config) GetFrontMatterDefaults(typename, relpath string) (m map[string]interface{}) {
for _, entry := range c.Defaults {
scope := &entry.Scope
hasPrefix := strings.HasPrefix(relpath, scope.Path)
hasType := scope.Type == "" || scope.Type == typename
if hasPrefix && hasType {
m = templates.MergeVariableMaps(m, entry.Values)
}
}
return
}
2017-07-03 17:48:06 +02:00
// Unmarshal updates site from a YAML configuration file.
func Unmarshal(bytes []byte, c *Config) error {
2017-07-07 20:55:03 +02:00
var (
compat configCompat
cList collectionsList
// cMap collectionsMap
)
2017-07-03 17:48:06 +02:00
if err := yaml.Unmarshal(bytes, &c); err != nil {
return err
}
if err := yaml.Unmarshal(bytes, &c.Variables); err != nil {
return err
}
2017-07-07 20:55:03 +02:00
if err := yaml.Unmarshal(bytes, &cList); err == nil {
if len(c.Collections) == 0 {
c.Collections = make(map[string]map[string]interface{})
}
for _, name := range cList.Collections {
c.Collections[name] = map[string]interface{}{}
}
}
cMap := collectionsMap{c.Collections}
if err := yaml.Unmarshal(bytes, &cMap); err == nil {
c.Collections = cMap.Collections
}
2017-07-03 17:48:06 +02:00
if err := yaml.Unmarshal(bytes, &compat); err != nil {
return err
}
if len(c.Plugins) == 0 {
c.Plugins = compat.Gems
}
return nil
}