2017-07-01 15:38:16 +02:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/osteele/gojekyll/tags"
|
|
|
|
"github.com/osteele/liquid/chunks"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-07-02 05:06:47 +02:00
|
|
|
registerPlugin("jekyll-avatar", func(_ PluginContext, h pluginHelper) error {
|
|
|
|
h.tag("avatar", avatarTag)
|
2017-07-01 15:38:16 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const avatarTemplate = `<img class="avatar avatar-small" src="https://avatars3.githubusercontent.com/{user}?v=3&s=40" alt="{user}" srcset="https://avatars3.githubusercontent.com/{user}?v=3&s=40 1x, https://avatars3.githubusercontent.com/{user}?v=3&s=80 2x, https://avatars3.githubusercontent.com/{user}?v=3&s=120 3x, https://avatars3.githubusercontent.com/{user}?v=3&s=160 4x" width="40" height="40" data-proofer-ignore="true" />`
|
|
|
|
|
2017-07-01 16:37:11 +02:00
|
|
|
func avatarTag(w io.Writer, ctx chunks.RenderContext) error {
|
|
|
|
var (
|
|
|
|
user string
|
|
|
|
size interface{} = 40
|
|
|
|
)
|
|
|
|
argsline, err := ctx.ParseTagArgs()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
args, err := tags.ParseArgs(argsline)
|
|
|
|
if err != nil {
|
2017-07-01 15:38:16 +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 {
|
|
|
|
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)
|
|
|
|
s = strings.Replace(s, "{user}", user, -1)
|
|
|
|
_, err = w.Write([]byte(s))
|
|
|
|
return err
|
2017-07-01 15:38:16 +02:00
|
|
|
}
|