1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-02 19:37:47 +01:00
gojekyll/config/config.go

195 lines
4.5 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-07 01:31:36 +02:00
// Plugins
ExcerptSeparator string `yaml:"excerpt_separator"`
2017-07-23 17:25:17 +02:00
// Conversion
Incremental bool
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:"-"`
Variables map[string]interface{} `yaml:"-"`
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
case contains(c.Include, rel):
return false
case c.RequireFrontMatterExclude[strings.ToUpper(utils.TrimExt(filepath.Base(rel)))]:
return true
default:
return false
}
}
func contains(array []string, s string) bool {
for _, item := range array {
if item == s {
return true
}
}
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.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
}