2017-06-22 21:21:47 +02:00
|
|
|
package config
|
2017-06-17 03:53:14 +02:00
|
|
|
|
2017-06-22 16:42:57 +02:00
|
|
|
import (
|
2017-06-22 21:21:47 +02:00
|
|
|
"strings"
|
|
|
|
|
2017-06-22 16:42:57 +02:00
|
|
|
"github.com/osteele/gojekyll/templates"
|
2017-07-09 22:17:20 +02:00
|
|
|
"github.com/osteele/gojekyll/utils"
|
2017-06-22 16:42:57 +02:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
)
|
2017-06-17 03:53:14 +02:00
|
|
|
|
2017-06-22 21:21:47 +02:00
|
|
|
// Config is the Jekyll site configuration, typically read from _config.yml.
|
2017-06-17 03:53:14 +02:00
|
|
|
// See https://jekyllrb.com/docs/configuration/#default-configuration
|
2017-06-22 21:21:47 +02:00
|
|
|
type Config struct {
|
2017-06-17 03:53:14 +02:00
|
|
|
// 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:"-"`
|
2017-06-17 03:53:14 +02:00
|
|
|
|
|
|
|
// 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-17 03:53:14 +02:00
|
|
|
|
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
|
|
|
|
|
2017-06-17 03:53:14 +02:00
|
|
|
// 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-06-22 21:21:47 +02:00
|
|
|
|
2017-07-01 05:56:29 +02:00
|
|
|
Variables map[string]interface{} `yaml:"-"`
|
2017-06-22 21:21:47 +02:00
|
|
|
}
|
|
|
|
|
2017-07-03 17:48:06 +02:00
|
|
|
type configCompat struct {
|
|
|
|
Gems []string
|
2017-06-17 03:53:14 +02:00
|
|
|
}
|
|
|
|
|
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)
|
2017-06-17 03:53:14 +02:00
|
|
|
}
|
|
|
|
|
2017-06-22 21:21:47 +02:00
|
|
|
// 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{}) {
|
2017-06-22 21:21:47 +02:00
|
|
|
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
|
|
|
|
}
|