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:
parent
f247ed4ebe
commit
924fe4e06a
@ -22,6 +22,7 @@ Gojekyll is a partially-compatible clone of the [Jekyll](https://jekyllrb.com) s
|
|||||||
- [Installation](#installation)
|
- [Installation](#installation)
|
||||||
- [Binary Downloads](#binary-downloads)
|
- [Binary Downloads](#binary-downloads)
|
||||||
- [From Source](#from-source)
|
- [From Source](#from-source)
|
||||||
|
- [[Optional] Install command-line autocompletion](#optional-install-command-line-autocompletion)
|
||||||
- [Status](#status)
|
- [Status](#status)
|
||||||
- [Current Limitations](#current-limitations)
|
- [Current Limitations](#current-limitations)
|
||||||
- [Other Differences](#other-differences)
|
- [Other Differences](#other-differences)
|
||||||
@ -101,10 +102,6 @@ Missing features:
|
|||||||
- [`markdown="span"`, `markdown="block"`](https://kramdown.gettalong.org/syntax.html#html-blocks)
|
- [`markdown="span"`, `markdown="block"`](https://kramdown.gettalong.org/syntax.html#html-blocks)
|
||||||
- Markdown configuration options
|
- 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.
|
Also see the [detailed status](#feature-status) below.
|
||||||
|
|
||||||
### Other Differences
|
### Other Differences
|
||||||
|
@ -68,7 +68,8 @@ type Config struct {
|
|||||||
|
|
||||||
// Meta
|
// Meta
|
||||||
ConfigFile string `yaml:"-"`
|
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
|
// Plugins
|
||||||
RequireFrontMatter bool `yaml:"-"`
|
RequireFrontMatter bool `yaml:"-"`
|
||||||
@ -142,7 +143,7 @@ func (c *Config) RequiresFrontMatter(rel string) bool {
|
|||||||
return true
|
return true
|
||||||
case !c.IsMarkdown(rel):
|
case !c.IsMarkdown(rel):
|
||||||
return true
|
return true
|
||||||
case contains(c.Include, rel):
|
case utils.StringContains(c.Include, rel):
|
||||||
return false
|
return false
|
||||||
case c.RequireFrontMatterExclude[strings.ToUpper(utils.TrimExt(filepath.Base(rel)))]:
|
case c.RequireFrontMatterExclude[strings.ToUpper(utils.TrimExt(filepath.Base(rel)))]:
|
||||||
return true
|
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.
|
// Unmarshal updates site from a YAML configuration file.
|
||||||
func Unmarshal(bytes []byte, c *Config) error {
|
func Unmarshal(bytes []byte, c *Config) error {
|
||||||
var (
|
var (
|
||||||
@ -169,7 +161,10 @@ func Unmarshal(bytes []byte, c *Config) error {
|
|||||||
if err := yaml.Unmarshal(bytes, &c); err != nil {
|
if err := yaml.Unmarshal(bytes, &c); err != nil {
|
||||||
return err
|
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
|
return err
|
||||||
}
|
}
|
||||||
if err := yaml.Unmarshal(bytes, &cList); err == nil {
|
if err := yaml.Unmarshal(bytes, &cList); err == nil {
|
||||||
@ -192,3 +187,47 @@ func Unmarshal(bytes []byte, c *Config) error {
|
|||||||
}
|
}
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
@ -16,3 +16,11 @@ plugins:
|
|||||||
|
|
||||||
gist:
|
gist:
|
||||||
noscript: false
|
noscript: false
|
||||||
|
|
||||||
|
config_data:
|
||||||
|
k1: first
|
||||||
|
k2: second
|
||||||
|
k0: third
|
||||||
|
k5: fourth
|
||||||
|
k4: fifth
|
||||||
|
k3: sixth
|
||||||
|
@ -26,3 +26,11 @@
|
|||||||
<tr>{% for cell in row %}<td>{{ cell }}</td>{% endfor %}</tr>
|
<tr>{% for cell in row %}<td>{{ cell }}</td>{% endfor %}</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
## Site data
|
||||||
|
|
||||||
|
<table>
|
||||||
|
{% for row in site.config_data %}
|
||||||
|
<tr>{% for cell in row %}<td>{{ cell }}</td>{% endfor %}</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</table>
|
||||||
|
@ -35,7 +35,7 @@ func (p *jekyllFeedPlugin) ConfigureTemplateEngine(e *liquid.Engine) error {
|
|||||||
|
|
||||||
func (p *jekyllFeedPlugin) PostRead(s Site) error {
|
func (p *jekyllFeedPlugin) PostRead(s Site) error {
|
||||||
path := "/feed.xml"
|
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 {
|
if pp, ok := cfg["path"].(string); ok {
|
||||||
path = "/" + pp
|
path = "/" + pp
|
||||||
}
|
}
|
||||||
@ -47,9 +47,12 @@ func (p *jekyllFeedPlugin) PostRead(s Site) error {
|
|||||||
|
|
||||||
func (p *jekyllFeedPlugin) feedMetaTag(ctx render.Context) (string, error) {
|
func (p *jekyllFeedPlugin) feedMetaTag(ctx render.Context) (string, error) {
|
||||||
cfg := p.site.Config()
|
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">`,
|
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
|
return tag, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,10 +120,8 @@ func getCurrentRepo(c *config.Config) (string, error) {
|
|||||||
if nwo := os.Getenv("PAGES_REPO_NWO"); nwo != "" {
|
if nwo := os.Getenv("PAGES_REPO_NWO"); nwo != "" {
|
||||||
return nwo, nil
|
return nwo, nil
|
||||||
}
|
}
|
||||||
if s, ok := c.Variables["repository"]; ok {
|
if s, ok := c.String("repository"); ok {
|
||||||
if s, ok := s.(string); ok {
|
return s, nil
|
||||||
return s, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
cmd := exec.Command("git", "remote", "-v") // nolint: gas
|
cmd := exec.Command("git", "remote", "-v") // nolint: gas
|
||||||
cmd.Dir = c.SourceDir()
|
cmd.Dir = c.SourceDir()
|
||||||
|
@ -25,7 +25,7 @@ func (s *Site) initializeDrop() error {
|
|||||||
for _, c := range s.Collections {
|
for _, c := range s.Collections {
|
||||||
docs = append(docs, c.Pages()...)
|
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(),
|
"collections": s.collectionDrops(),
|
||||||
"data": s.data,
|
"data": s.data,
|
||||||
"documents": docs,
|
"documents": docs,
|
||||||
|
@ -110,7 +110,7 @@ func New(flags config.Flags) *Site {
|
|||||||
// The server uses this.
|
// The server uses this.
|
||||||
func (s *Site) SetAbsoluteURL(url string) {
|
func (s *Site) SetAbsoluteURL(url string) {
|
||||||
s.config.AbsoluteURL = url
|
s.config.AbsoluteURL = url
|
||||||
s.config.Variables["url"] = url
|
s.config.Set("url", url)
|
||||||
if s.drop != nil {
|
if s.drop != nil {
|
||||||
s.drop["url"] = url
|
s.drop["url"] = url
|
||||||
}
|
}
|
||||||
|
@ -58,6 +58,16 @@ func StringArrayToMap(a []string) map[string]bool {
|
|||||||
return m
|
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.
|
// Titleize splits at ` `, capitalizes, and joins.
|
||||||
func Titleize(s string) string {
|
func Titleize(s string) string {
|
||||||
a := strings.Split(s, "-")
|
a := strings.Split(s, "-")
|
||||||
|
Loading…
Reference in New Issue
Block a user