1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 06:28:58 +01:00

Use MapSlice to preserve _config data order

This commit is contained in:
Oliver Steele 2017-08-24 12:21:38 -04:00
parent f247ed4ebe
commit 924fe4e06a
9 changed files with 88 additions and 25 deletions

View File

@ -22,6 +22,7 @@ Gojekyll is a partially-compatible clone of the [Jekyll](https://jekyllrb.com) s
- [Installation](#installation)
- [Binary Downloads](#binary-downloads)
- [From Source](#from-source)
- [[Optional] Install command-line autocompletion](#optional-install-command-line-autocompletion)
- [Status](#status)
- [Current Limitations](#current-limitations)
- [Other Differences](#other-differences)
@ -101,10 +102,6 @@ Missing features:
- [`markdown="span"`, `markdown="block"`](https://kramdown.gettalong.org/syntax.html#html-blocks)
- Markdown configuration options
Differences:
- The order of YAML maps, in `_config` and `site.data`, is not preserved.
Also see the [detailed status](#feature-status) below.
### Other Differences

View File

@ -68,7 +68,8 @@ type Config struct {
// Meta
ConfigFile string `yaml:"-"`
Variables map[string]interface{} `yaml:"-"`
m map[string]interface{} `yaml:"-"` // config file, as map
ms yaml.MapSlice `yaml:"-"` // config file, as MapSlice
// Plugins
RequireFrontMatter bool `yaml:"-"`
@ -142,7 +143,7 @@ func (c *Config) RequiresFrontMatter(rel string) bool {
return true
case !c.IsMarkdown(rel):
return true
case contains(c.Include, rel):
case utils.StringContains(c.Include, rel):
return false
case c.RequireFrontMatterExclude[strings.ToUpper(utils.TrimExt(filepath.Base(rel)))]:
return true
@ -151,15 +152,6 @@ func (c *Config) RequiresFrontMatter(rel string) bool {
}
}
func contains(array []string, s string) bool {
for _, item := range array {
if item == s {
return true
}
}
return false
}
// Unmarshal updates site from a YAML configuration file.
func Unmarshal(bytes []byte, c *Config) error {
var (
@ -169,7 +161,10 @@ func Unmarshal(bytes []byte, c *Config) error {
if err := yaml.Unmarshal(bytes, &c); err != nil {
return err
}
if err := yaml.Unmarshal(bytes, &c.Variables); err != nil {
if err := yaml.Unmarshal(bytes, &c.ms); err != nil {
return err
}
if err := yaml.Unmarshal(bytes, &c.m); err != nil {
return err
}
if err := yaml.Unmarshal(bytes, &cList); err == nil {
@ -192,3 +187,47 @@ func Unmarshal(bytes []byte, c *Config) error {
}
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
}

View File

@ -16,3 +16,11 @@ plugins:
gist:
noscript: false
config_data:
k1: first
k2: second
k0: third
k5: fourth
k4: fifth
k3: sixth

View File

@ -26,3 +26,11 @@
<tr>{% for cell in row %}<td>{{ cell }}</td>{% endfor %}</tr>
{% endfor %}
</table>
## Site data
<table>
{% for row in site.config_data %}
<tr>{% for cell in row %}<td>{{ cell }}</td>{% endfor %}</tr>
{% endfor %}
</table>

View File

@ -35,7 +35,7 @@ func (p *jekyllFeedPlugin) ConfigureTemplateEngine(e *liquid.Engine) error {
func (p *jekyllFeedPlugin) PostRead(s Site) error {
path := "/feed.xml"
if cfg, ok := s.Config().Variables["feed"].(map[string]interface{}); ok {
if cfg, ok := s.Config().Map("feed"); ok {
if pp, ok := cfg["path"].(string); ok {
path = "/" + pp
}
@ -47,9 +47,12 @@ func (p *jekyllFeedPlugin) PostRead(s Site) error {
func (p *jekyllFeedPlugin) feedMetaTag(ctx render.Context) (string, error) {
cfg := p.site.Config()
name, _ := cfg.Variables["name"].(string)
title, _ := cfg.String("name")
if s, ok := cfg.String("title"); ok {
title = s
}
tag := fmt.Sprintf(`<link type="application/atom+xml" rel="alternate" href="%s/feed.xml" title="%s">`,
html.EscapeString(cfg.AbsoluteURL), html.EscapeString(name))
html.EscapeString(cfg.AbsoluteURL), html.EscapeString(title))
return tag, nil
}

View File

@ -120,10 +120,8 @@ func getCurrentRepo(c *config.Config) (string, error) {
if nwo := os.Getenv("PAGES_REPO_NWO"); nwo != "" {
return nwo, nil
}
if s, ok := c.Variables["repository"]; ok {
if s, ok := s.(string); ok {
return s, nil
}
if s, ok := c.String("repository"); ok {
return s, nil
}
cmd := exec.Command("git", "remote", "-v") // nolint: gas
cmd.Dir = c.SourceDir()

View File

@ -25,7 +25,7 @@ func (s *Site) initializeDrop() error {
for _, c := range s.Collections {
docs = append(docs, c.Pages()...)
}
drop := templates.MergeVariableMaps(s.config.Variables, map[string]interface{}{
drop := templates.MergeVariableMaps(s.config.Variables(), map[string]interface{}{
"collections": s.collectionDrops(),
"data": s.data,
"documents": docs,

View File

@ -110,7 +110,7 @@ func New(flags config.Flags) *Site {
// The server uses this.
func (s *Site) SetAbsoluteURL(url string) {
s.config.AbsoluteURL = url
s.config.Variables["url"] = url
s.config.Set("url", url)
if s.drop != nil {
s.drop["url"] = url
}

View File

@ -58,6 +58,16 @@ func StringArrayToMap(a []string) map[string]bool {
return m
}
// StringContains returns a bool indicating whether the array contains the string.
func StringContains(a []string, s string) bool {
for _, item := range a {
if item == s {
return true
}
}
return false
}
// Titleize splits at ` `, capitalizes, and joins.
func Titleize(s string) string {
a := strings.Split(s, "-")