1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-23 06:41:10 +01:00
gojekyll/plugins/gist.go

40 lines
840 B
Go
Raw Normal View History

2017-07-01 23:06:47 -04:00
package plugins
import (
"fmt"
"github.com/osteele/gojekyll/tags"
2017-07-08 22:47:50 -04:00
"github.com/osteele/liquid"
2017-07-04 17:13:47 -04:00
"github.com/osteele/liquid/render"
2017-07-01 23:06:47 -04:00
)
func init() {
2017-07-08 22:47:50 -04:00
register("jekyll-gist", jekyllGistPlugin{})
}
type jekyllGistPlugin struct{ plugin }
func (p jekyllGistPlugin) ConfigureTemplateEngine(e liquid.Engine) error {
e.RegisterTag("gist", gistTag)
return nil
2017-07-01 23:06:47 -04:00
}
2017-07-04 17:13:47 -04:00
func gistTag(ctx render.Context) (string, error) {
2017-07-05 23:26:47 -04:00
argsline, err := ctx.ExpandTagArg()
2017-07-01 23:06:47 -04:00
if err != nil {
2017-07-04 08:06:34 -04:00
return "", err
2017-07-01 23:06:47 -04:00
}
args, err := tags.ParseArgs(argsline)
if err != nil {
2017-07-04 08:06:34 -04:00
return "", err
2017-07-01 23:06:47 -04:00
}
if len(args.Args) < 1 {
2017-07-04 08:06:34 -04:00
return "", fmt.Errorf("gist tag: missing argument")
2017-07-01 23:06:47 -04:00
}
url := fmt.Sprintf("https://gist.github.com/%s.js", args.Args[0])
if len(args.Args) >= 2 {
url += fmt.Sprintf("?file=%s", args.Args[1])
}
2017-07-04 08:06:34 -04:00
return `<script src=` + url + `> </script>`, nil
2017-07-01 23:06:47 -04:00
}