package plugins import ( "fmt" "strings" "github.com/osteele/gojekyll/tags" "github.com/osteele/liquid" "github.com/osteele/liquid/render" ) func init() { register("jekyll-avatar", jekyllAvatarPlugin{}) } type jekyllAvatarPlugin struct{ plugin } func (p jekyllAvatarPlugin) ConfigureTemplateEngine(e liquid.Engine) error { e.RegisterTag("avatar", avatarTag) return nil } const avatarTemplate = `{user}` func avatarTag(ctx render.Context) (string, error) { var ( user string size interface{} = 40 ) argsline, err := ctx.ExpandTagArg() if err != nil { return "", err } args, err := tags.ParseArgs(argsline) if err != nil { return "", err } if len(args.Args) > 0 { user = args.Args[0] } options, err := args.EvalOptions(ctx) if err != nil { return "", err } for name, value := range options { switch name { case "user": user = fmt.Sprint(value) case "size": size = value default: return "", fmt.Errorf("unknown avatar argument: %s", name) } } if user == "" { return "", fmt.Errorf("parse error in avatar tag parameters %s", argsline) } s := strings.Replace(avatarTemplate, "40", fmt.Sprint(size), -1) return strings.Replace(s, "{user}", user, -1), nil }