1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 08:34:42 +01:00

Implement include parameters

This commit is contained in:
Oliver Steele 2017-06-28 13:32:35 -04:00
parent c41a7b975c
commit 60aa695d6c
2 changed files with 38 additions and 3 deletions

View File

@ -33,8 +33,10 @@ This project is missing more functionality than it implements. It may accidental
- [ ] Remaining Jekyll Liquid tags
- [ ] Jekyll Liquid filters
- [x] Includes
- [ ] parameters
- [ ] `include_relative`
- [x] parameters
- [ ] variable parameters
- [ ] variables `{% include {{ expr }} %}`
- [x] Permalinks
- [ ] Pagination
- [ ] Themes

View File

@ -3,6 +3,8 @@ package liquid
import (
"fmt"
"io"
"regexp"
"strings"
"github.com/osteele/liquid/chunks"
)
@ -18,9 +20,40 @@ func (e *LocalWrapperEngine) addJekyllTags() {
return err
}, nil
})
e.engine.DefineTag("include", func(filename string) (func(io.Writer, chunks.Context) error, error) {
e.engine.DefineTag("include", func(line string) (func(io.Writer, chunks.Context) error, error) {
// TODO escapes
includeLinePattern := regexp.MustCompile(`^\S+(?:\s+\S+=("[^"]+"|'[^']'|[^'"\s]+))*$`)
includeParamPattern := regexp.MustCompile(`\b(\S+)=("[^"]+"|'[^']'|[^'"\s]+)(?:\s|$)`)
if !includeLinePattern.MatchString(line) {
return nil, fmt.Errorf("parse error in include tag parameters")
}
filename := strings.Fields(line)[0]
type paramSpec struct {
value string
eval bool
}
params := map[string]paramSpec{}
for _, m := range includeParamPattern.FindAllStringSubmatch(line, -1) {
k, v, eval := m[1], m[2], true
if strings.HasPrefix(v, `'`) || strings.HasPrefix(v, `"`) {
v, eval = v[1:len(v)-1], false
}
params[k] = paramSpec{v, eval}
}
return func(w io.Writer, ctx chunks.Context) error {
return e.includeTagHandler(filename, w, ctx.GetVariableMap())
include := map[string]interface{}{}
for k, v := range params {
if v.eval {
// TODO include parameter variables
panic(fmt.Errorf("include parameter variables aren't implemented"))
} else {
include[k] = v.value
}
}
bindings := ctx.GetVariableMap()
// TODO use a copy of this
bindings["include"] = include
return e.includeTagHandler(filename, w, bindings)
}, nil
})
}