1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-30 10:39:02 +01:00

Implement highlight (disabled), avatar

This commit is contained in:
Oliver Steele 2017-06-30 17:34:14 -04:00
parent 7e50bb1de1
commit f2d3896f54
3 changed files with 77 additions and 16 deletions

View File

@ -11,7 +11,7 @@ import (
type Engine interface { type Engine interface {
Parse([]byte) (liquid.Template, error) Parse([]byte) (liquid.Template, error)
ParseAndRender([]byte, map[string]interface{}) ([]byte, error) ParseAndRender([]byte, map[string]interface{}) ([]byte, error)
DefineTag(string, func(string) (func(io.Writer, chunks.Context) error, error)) DefineTag(string, func(string) (func(io.Writer, chunks.RenderContext) error, error))
} }
// Wrapper is a wrapper around the Liquid engine. // Wrapper is a wrapper around the Liquid engine.
@ -38,7 +38,7 @@ func NewEngine() *Wrapper {
} }
// DefineTag is in the Engine interface. // DefineTag is in the Engine interface.
func (e *Wrapper) DefineTag(name string, f func(string) (func(io.Writer, chunks.Context) error, error)) { func (e *Wrapper) DefineTag(name string, f func(string) (func(io.Writer, chunks.RenderContext) error, error)) {
e.engine.DefineTag(name, f) e.engine.DefineTag(name, f)
} }

View File

@ -3,6 +3,8 @@ package liquid
import ( import (
"fmt" "fmt"
"io" "io"
"os"
"os/exec"
"regexp" "regexp"
"strings" "strings"
@ -12,10 +14,42 @@ import (
func (e *Wrapper) addJekyllTags() { func (e *Wrapper) addJekyllTags() {
e.engine.DefineTag("link", e.linkTag) e.engine.DefineTag("link", e.linkTag)
e.engine.DefineTag("include", e.includeTag) e.engine.DefineTag("include", e.includeTag)
// TODO unimplemented
e.engine.DefineTag("post_url", emptyTag)
e.engine.DefineStartTag("highlight", highlightTag)
} }
func (e *Wrapper) linkTag(filename string) (func(io.Writer, chunks.Context) error, error) { func emptyTag(_ string) (func(io.Writer, chunks.RenderContext) error, error) {
return func(w io.Writer, _ chunks.Context) error { return func(w io.Writer, _ chunks.RenderContext) error { return nil }, nil
}
func highlightTag(w io.Writer, ctx chunks.RenderContext) error {
args, err := ctx.ParseTagArgs()
if err != nil {
return err
}
cargs := []string{}
if args != "" {
cargs = append(cargs, "-l"+args)
}
s, err := ctx.InnerString()
if err != nil {
return err
}
if true {
_, err = w.Write([]byte(s))
return err
}
cmd := exec.Command("pygmentize", cargs...)
cmd.Stdin = strings.NewReader(s)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func (e *Wrapper) linkTag(filename string) (func(io.Writer, chunks.RenderContext) error, error) {
return func(w io.Writer, _ chunks.RenderContext) error {
url, found := e.linkHandler(filename) url, found := e.linkHandler(filename)
if !found { if !found {
return fmt.Errorf("missing link filename: %s", filename) return fmt.Errorf("missing link filename: %s", filename)
@ -25,7 +59,7 @@ func (e *Wrapper) linkTag(filename string) (func(io.Writer, chunks.Context) erro
}, nil }, nil
} }
func (e *Wrapper) includeTag(line string) (func(io.Writer, chunks.Context) error, error) { func (e *Wrapper) includeTag(line string) (func(io.Writer, chunks.RenderContext) error, error) {
// TODO string escapes // TODO string escapes
includeLinePattern := regexp.MustCompile(`^\S+(?:\s+\S+=("[^"]+"|'[^']'|[^'"\s]+))*$`) includeLinePattern := regexp.MustCompile(`^\S+(?:\s+\S+=("[^"]+"|'[^']'|[^'"\s]+))*$`)
includeParamPattern := regexp.MustCompile(`\b(\S+)=("[^"]+"|'[^']'|[^'"\s]+)(?:\s|$)`) includeParamPattern := regexp.MustCompile(`\b(\S+)=("[^"]+"|'[^']'|[^'"\s]+)(?:\s|$)`)
@ -45,7 +79,7 @@ func (e *Wrapper) includeTag(line string) (func(io.Writer, chunks.Context) error
} }
params[k] = paramSpec{v, eval} params[k] = paramSpec{v, eval}
} }
return func(w io.Writer, ctx chunks.Context) error { return func(w io.Writer, ctx chunks.RenderContext) error {
include := map[string]interface{}{} include := map[string]interface{}{}
for k, v := range params { for k, v := range params {
if v.eval { if v.eval {

View File

@ -33,8 +33,8 @@ func warnUnimplemented(name string) {
fmt.Printf("warning: gojekyll does not emulate the %s plugin. Some tags have been stubbed to prevent errors.\n", name) fmt.Printf("warning: gojekyll does not emulate the %s plugin. Some tags have been stubbed to prevent errors.\n", name)
} }
func emptyTag(lexer string) (func(io.Writer, chunks.Context) error, error) { func emptyTag(lexer string) (func(io.Writer, chunks.RenderContext) error, error) {
return func(w io.Writer, _ chunks.Context) error { return nil }, nil return func(w io.Writer, _ chunks.RenderContext) error { return nil }, nil
} }
func init() { func init() {
@ -60,15 +60,42 @@ func init() {
// registerPlugin("jemoji") // registerPlugin("jemoji")
} }
const avatarTemplate = `<img class="avatar avatar-small" src="https://avatars3.githubusercontent.com/{username}?v=3&amp;s=40" alt="{username}" srcset="https://avatars3.githubusercontent.com/{username}?v=3&amp;s=40 1x, https://avatars3.githubusercontent.com/{username}?v=3&amp;s=80 2x, https://avatars3.githubusercontent.com/{username}?v=3&amp;s=120 3x, https://avatars3.githubusercontent.com/{username}?v=3&amp;s=160 4x" width="40" height="40" />` // this template is from the plugin documentation
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" />`
func avatarTag(filename string) (func(io.Writer, chunks.Context) error, error) { func avatarTag(_ string) (func(io.Writer, chunks.RenderContext) error, error) {
username := "osteele" // TODO replace with real name return func(w io.Writer, ctx chunks.RenderContext) error {
size := 40 var (
return func(w io.Writer, _ chunks.Context) error { user string
s := strings.Replace(avatarTemplate, "40", fmt.Sprintf("%s", size), -1) size = "40"
s = strings.Replace(s, "{username}", username, -1) )
_, err := w.Write([]byte(s)) args, err := ctx.ParseTagArgs()
fmt.Sprintln("args", args)
if err != nil {
fmt.Println("err", err)
return err
}
for _, arg := range strings.Fields(args) {
split := strings.SplitN(arg, "=", 2)
if len(split) == 1 {
split = []string{"user", arg}
}
switch split[0] {
case "user":
user = split[1]
case "size":
size = split[1]
default:
return fmt.Errorf("unknown avatar argument: %s", split[0])
}
}
if user == "" {
return fmt.Errorf("parse error in avatar tag parameters %s", args)
}
s := strings.Replace(avatarTemplate, "40", fmt.Sprint(size), -1)
s = strings.Replace(s, "{user}", user, -1)
fmt.Println(args, "->", user, size)
_, err = w.Write([]byte(s))
return err return err
}, nil }, nil
} }