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

Move getStringArray -> frontmatter.StringArray

This commit is contained in:
Oliver Steele 2017-09-03 13:18:26 -04:00
parent 1b6ef60051
commit 367156b0b3
2 changed files with 21 additions and 30 deletions

View File

@ -43,6 +43,25 @@ func (fm FrontMatter) String(k string, defaultValue string) string {
return defaultValue
}
// StringArray returns m[k] if it's a []string or string array
func (fm FrontMatter) StringArray(k string) []string {
if value, ok := fm[k]; ok {
switch value := value.(type) {
case []string:
return value
case []interface{}:
a := make([]string, len(value))
for i, item := range value {
a[i] = fmt.Sprintf("%s", item)
}
return a
case string:
return []string{value}
}
}
return nil
}
// SortedStringArray returns a sorts list of strings from a
// frontmatter variable that is either a string (in which case it
// is a ws-separated list of words), or a list of strings.

View File

@ -2,7 +2,6 @@ package plugins
import (
"bytes"
"fmt"
"text/template"
)
@ -47,11 +46,7 @@ func (p jekyllRedirectFromPlugin) processRedirectFrom(s Site, ps []Page) (func()
redirects = append(redirects, f)
}
for _, p := range ps {
sources, err := getStringArray(p, "redirect_from")
if err != nil {
return nil, err
}
for _, from := range sources {
for _, from := range p.FrontMatter().StringArray("redirect_from") {
addRedirectFrom(from, p)
}
}
@ -64,10 +59,7 @@ func (p jekyllRedirectFromPlugin) processRedirectFrom(s Site, ps []Page) (func()
func (p jekyllRedirectFromPlugin) processRedirectTo(_ Site, ps []Page) error {
for _, p := range ps {
sources, err := getStringArray(p, "redirect_to")
if err != nil {
return err
}
sources := p.FrontMatter().StringArray("redirect_to")
if len(sources) > 0 {
p.SetContent(createRedirectionHTML(sources[0]))
}
@ -75,26 +67,6 @@ func (p jekyllRedirectFromPlugin) processRedirectTo(_ Site, ps []Page) error {
return nil
}
func getStringArray(p Page, fieldName string) ([]string, error) {
var a []string
if value, ok := p.FrontMatter()[fieldName]; ok {
switch value := value.(type) {
case []string:
a = value
case []interface{}:
a = make([]string, len(value))
for i, item := range value {
a[i] = fmt.Sprintf("%s", item)
}
case string:
a = []string{value}
default:
return nil, fmt.Errorf("unimplemented redirect_from type %T", value)
}
}
return a, nil
}
func createRedirectionHTML(to string) string {
r := redirection{to}
buf := new(bytes.Buffer)