1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-11 17:00:02 +01:00
gojekyll/commands/variables.go

56 lines
1.1 KiB
Go
Raw Permalink Normal View History

2017-08-11 19:25:51 +02:00
package commands
import (
"strings"
"github.com/k0kubun/pp"
2017-08-11 19:25:51 +02:00
"github.com/osteele/gojekyll/site"
"github.com/osteele/gojekyll/utils"
"github.com/osteele/liquid"
)
var variables = app.Command(
"variables",
"Print site or document variables",
).Alias("v").Alias("var").Alias("vars")
var variablePath = variables.Arg("PATH", `Filename, URL, "site", or e.g. "site.x.y"`).String()
2017-08-11 19:25:51 +02:00
func variablesCommand(site *site.Site) (err error) {
var data interface{}
switch {
case strings.HasPrefix(*variablePath, "site"):
data, err = utils.FollowDots(site, strings.Split(*variablePath, ".")[1:])
if err != nil {
return
}
case *variablePath != "":
data, err = pageFromPathOrRoute(site, *variablePath)
if err != nil {
return
}
default:
data = site
}
data = liquid.FromDrop(data)
bytesToStrings(data)
logger.label("Variables:", "")
_, err = pp.Print(data)
return err
}
// modifies its argument
func bytesToStrings(data interface{}) {
2017-08-11 19:25:51 +02:00
if m, ok := data.(map[string]interface{}); ok {
for k, v := range m {
if b, ok := v.([]byte); ok {
s := string(b)
if len(s) > 200 {
s = s[:200] + "…"
}
m[k] = s
2017-08-11 19:25:51 +02:00
}
}
}
}