1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 15:57:49 +01:00
gojekyll/config/config.go

236 lines
5.6 KiB
Go
Raw Normal View History

package config
import (
2017-07-25 15:48:49 +02:00
"io/ioutil"
"os"
2017-07-24 18:18:24 +02:00
"path/filepath"
"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:"-"`
2017-07-24 13:44:49 +02:00
Theme string
// 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-23 17:25:17 +02:00
// Conversion
2017-08-27 19:40:54 +02:00
ExcerptSeparator string `yaml:"excerpt_separator"`
Incremental bool
Sass struct {
Dir string `yaml:"sass_dir"`
// TODO Style string // compressed
}
2017-07-23 17:25:17 +02:00
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-07-23 17:25:17 +02:00
Timezone string
2017-07-24 18:18:24 +02:00
Verbose bool
Defaults []struct {
2017-06-17 05:30:10 +02:00
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-24 18:18:24 +02:00
// CLI-only
DryRun bool `yaml:"-"`
ForcePolling bool `yaml:"-"`
Watch bool `yaml:"-"`
2017-07-25 15:48:49 +02:00
// Meta
ConfigFile string `yaml:"-"`
m map[string]interface{} `yaml:"-"` // config file, as map
ms yaml.MapSlice `yaml:"-"` // config file, as MapSlice
2017-07-24 18:18:24 +02:00
// Plugins
RequireFrontMatter bool `yaml:"-"`
RequireFrontMatterExclude map[string]bool `yaml:"-"`
}
2017-07-25 15:48:49 +02:00
// FromDirectory updates the config from the config file in
// the directory, if such a file exists.
func (c *Config) FromDirectory(dir string) error {
path := filepath.Join(dir, "_config.yml")
bytes, err := ioutil.ReadFile(path)
switch {
case os.IsNotExist(err):
// break
case err != nil:
return err
default:
if err = Unmarshal(bytes, c); err != nil {
return utils.WrapPathError(err, path)
}
c.ConfigFile = path
}
c.Source = dir
return nil
}
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-25 15:29:38 +02:00
// IsConfigPath returns true if its arguments is a site configuration file.
func (c *Config) IsConfigPath(rel string) bool {
return rel == "_config.yml"
}
2017-07-25 17:08:53 +02:00
// SassDir returns the relative path of the SASS directory.
func (c *Config) SassDir() string {
return "_sass"
}
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-24 18:18:24 +02:00
func (c *Config) GetFrontMatterDefaults(typename, rel string) (m map[string]interface{}) {
for _, entry := range c.Defaults {
scope := &entry.Scope
2017-07-24 18:18:24 +02:00
hasPrefix := strings.HasPrefix(rel, scope.Path)
hasType := scope.Type == "" || scope.Type == typename
if hasPrefix && hasType {
m = templates.MergeVariableMaps(m, entry.Values)
}
}
return
}
2017-07-24 18:18:24 +02:00
// RequiresFrontMatter returns a bool indicating whether the file requires front matter in order to recognize as a page.
func (c *Config) RequiresFrontMatter(rel string) bool {
switch {
case c.RequireFrontMatter:
return true
case !c.IsMarkdown(rel):
return true
2017-09-01 15:10:07 +02:00
case utils.StringArrayContains(c.Include, rel):
2017-07-24 18:18:24 +02:00
return false
case c.RequireFrontMatterExclude[strings.ToUpper(utils.TrimExt(filepath.Base(rel)))]:
return true
default:
return false
}
}
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
)
2017-07-03 17:48:06 +02:00
if err := yaml.Unmarshal(bytes, &c); err != nil {
return err
}
if err := yaml.Unmarshal(bytes, &c.ms); err != nil {
return err
}
if err := yaml.Unmarshal(bytes, &c.m); err != nil {
2017-07-03 17:48:06 +02:00
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
}
// Variables returns the configuration as a Liquid variable map.
func (c *Config) Variables() map[string]interface{} {
m := map[string]interface{}{}
for _, item := range c.ms {
if s, ok := item.Key.(string); ok {
m[s] = item.Value
}
}
return m
}
// Set sets a value in the Liquid variable map.
// This does not update the corresponding value in the Config struct.
func (c *Config) Set(key string, val interface{}) {
c.m[key] = val
for _, item := range c.ms {
if item.Key == key {
item.Value = val
return
}
}
c.ms = append(c.ms, yaml.MapItem{Key: key, Value: val})
}
// Map returns the config indexed by key, if it's a map.
func (c *Config) Map(key string) (map[string]interface{}, bool) {
if m, ok := c.m[key]; ok {
if m, ok := m.(map[string]interface{}); ok {
return m, ok
}
}
return nil, false
}
//String returns the config indexed by key, if it's a string.
func (c *Config) String(key string) (string, bool) {
if m, ok := c.m[key]; ok {
if m, ok := m.(string); ok {
return m, ok
}
}
return "", false
}