1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-03 14:27:49 +01:00
gojekyll/plugins/avatar.go

61 lines
1.6 KiB
Go
Raw Normal View History

2017-07-01 15:38:16 +02:00
package plugins
import (
"fmt"
"strings"
"github.com/osteele/gojekyll/tags"
2017-07-09 04:47:50 +02:00
"github.com/osteele/liquid"
2017-07-04 23:13:47 +02:00
"github.com/osteele/liquid/render"
2017-07-01 15:38:16 +02:00
)
func init() {
2017-07-09 04:47:50 +02:00
register("jekyll-avatar", jekyllAvatarPlugin{})
}
type jekyllAvatarPlugin struct{ plugin }
func (p jekyllAvatarPlugin) ConfigureTemplateEngine(e liquid.Engine) error {
e.RegisterTag("avatar", avatarTag)
return nil
2017-07-01 15:38:16 +02:00
}
const avatarTemplate = `<img class="avatar avatar-small" src="https://avatars3.githubusercontent.com/{user}?v=3&amp;s=40" alt="{user}" srcset="https://avatars3.githubusercontent.com/{user}?v=3&amp;s=40 1x, https://avatars3.githubusercontent.com/{user}?v=3&amp;s=80 2x, https://avatars3.githubusercontent.com/{user}?v=3&amp;s=120 3x, https://avatars3.githubusercontent.com/{user}?v=3&amp;s=160 4x" width="40" height="40" data-proofer-ignore="true" />`
2017-07-04 23:13:47 +02:00
func avatarTag(ctx render.Context) (string, error) {
2017-07-01 16:37:11 +02:00
var (
user string
size interface{} = 40
)
2017-07-06 05:26:47 +02:00
argsline, err := ctx.ExpandTagArg()
2017-07-01 16:37:11 +02:00
if err != nil {
2017-07-04 14:06:34 +02:00
return "", err
2017-07-01 16:37:11 +02:00
}
args, err := tags.ParseArgs(argsline)
if err != nil {
2017-07-04 14:06:34 +02:00
return "", err
2017-07-01 16:37:11 +02:00
}
if len(args.Args) > 0 {
user = args.Args[0]
}
options, err := args.EvalOptions(ctx)
if err != nil {
2017-07-04 14:06:34 +02:00
return "", err
2017-07-01 16:37:11 +02:00
}
for name, value := range options {
switch name {
case "user":
user = fmt.Sprint(value)
case "size":
size = value
default:
2017-07-04 14:06:34 +02:00
return "", fmt.Errorf("unknown avatar argument: %s", name)
2017-07-01 16:37:11 +02:00
}
}
if user == "" {
2017-07-04 14:06:34 +02:00
return "", fmt.Errorf("parse error in avatar tag parameters %s", argsline)
2017-07-01 16:37:11 +02:00
}
s := strings.Replace(avatarTemplate, "40", fmt.Sprint(size), -1)
2017-07-04 14:06:34 +02:00
return strings.Replace(s, "{user}", user, -1), nil
2017-07-01 15:38:16 +02:00
}