1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-12-11 17:00:02 +01:00
gojekyll/plugins/gist.go

40 lines
841 B
Go
Raw Normal View History

2017-07-02 05:06:47 +02:00
package plugins
import (
"fmt"
"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-02 05:06:47 +02:00
)
func init() {
2017-07-09 04:47:50 +02:00
register("jekyll-gist", jekyllGistPlugin{})
}
type jekyllGistPlugin struct{ plugin }
func (p jekyllGistPlugin) ConfigureTemplateEngine(e *liquid.Engine) error {
2017-07-09 04:47:50 +02:00
e.RegisterTag("gist", gistTag)
return nil
2017-07-02 05:06:47 +02:00
}
2017-07-04 23:13:47 +02:00
func gistTag(ctx render.Context) (string, error) {
2017-07-06 05:26:47 +02:00
argsline, err := ctx.ExpandTagArg()
2017-07-02 05:06:47 +02:00
if err != nil {
2017-07-04 14:06:34 +02:00
return "", err
2017-07-02 05:06:47 +02:00
}
args, err := tags.ParseArgs(argsline)
if err != nil {
2017-07-04 14:06:34 +02:00
return "", err
2017-07-02 05:06:47 +02:00
}
if len(args.Args) < 1 {
2017-07-04 14:06:34 +02:00
return "", fmt.Errorf("gist tag: missing argument")
2017-07-02 05:06:47 +02: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 14:06:34 +02:00
return `<script src=` + url + `> </script>`, nil
2017-07-02 05:06:47 +02:00
}