1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-26 23:34:47 +01:00

Randomize the avatar plugin subdomain

This commit is contained in:
Oliver Steele 2017-07-20 12:26:47 -04:00
parent f4025dee84
commit 4c3d35f8b3
2 changed files with 15 additions and 4 deletions

View File

@ -6,7 +6,7 @@ The functionality of some plugins is built into the core program:
| Plugin | Motivation | Basic Functionality | Missing Features |
|------------------------------|---------------|---------------------|-------------------------------------------------------------------------|
| jekyll-avatar | GitHub Pages² | ✓ | randomized hostname |
| jekyll-avatar | GitHub Pages² | ✓ | |
| jekyll-coffeescript | GitHub Pages | | |
| jekyll-default-layout | GitHub Pages | | |
| jekyll-feed | GitHub Pages | ✓ | |

View File

@ -1,8 +1,11 @@
package plugins
import (
"bytes"
"fmt"
"hash/crc32"
"strings"
"text/template"
"github.com/osteele/gojekyll/tags"
"github.com/osteele/liquid"
@ -20,7 +23,9 @@ func (p jekyllAvatarPlugin) ConfigureTemplateEngine(e *liquid.Engine) error {
return nil
}
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" />`
var avatarTemplate = template.Must(template.New("avatar").Parse(strings.TrimSpace(`
<img class="avatar avatar-small" src="https://{{.Subdomain}}.githubusercontent.com/{{.User}}?v=3&amp;s={{.Size}}" alt="{{.User}}" srcset="https://{{.Subdomain}}.githubusercontent.com/{{.User}}?v=3&amp;s={{.Size}} 1x, https://{{.Subdomain}}.githubusercontent.com/{{.User}}?v=3&amp;s=80 2x, https://{{.Subdomain}}.githubusercontent.com/{{.User}}?v=3&amp;s=120 3x, https://{{.Subdomain}}.githubusercontent.com/{{.User}}?v=3&amp;s=160 4x" width="{{.Size}}" height="{{.Size}}" data-proofer-ignore="true" />
`)))
func avatarTag(ctx render.Context) (string, error) {
var (
@ -55,6 +60,12 @@ func avatarTag(ctx render.Context) (string, error) {
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
n := crc32.Checksum([]byte(fmt.Sprintf("%s:%d", user, size)), crc32.IEEETable) % 4
s := struct {
User, Subdomain string
Size interface{}
}{user, fmt.Sprintf("avatar%d", n), size}
buf := new(bytes.Buffer)
err = avatarTemplate.Execute(buf, s)
return buf.String(), err
}