2017-06-17 04:03:28 +02:00
|
|
|
package liquid
|
2017-06-10 21:38:09 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
2017-06-17 18:30:37 +02:00
|
|
|
"github.com/acstech/liquid"
|
2017-06-10 21:38:09 +02:00
|
|
|
"github.com/acstech/liquid/core"
|
|
|
|
)
|
|
|
|
|
2017-06-17 18:30:37 +02:00
|
|
|
func init() {
|
|
|
|
liquid.Tags["link"] = LinkFactory
|
|
|
|
}
|
|
|
|
|
2017-06-17 05:50:30 +02:00
|
|
|
// A FilePathURLGetter given an include tag file name returns a URL.
|
|
|
|
type FilePathURLGetter func(string) (string, bool)
|
2017-06-17 04:03:28 +02:00
|
|
|
|
2017-06-17 05:50:30 +02:00
|
|
|
var filePathURLGetter FilePathURLGetter
|
2017-06-17 04:03:28 +02:00
|
|
|
|
2017-06-17 05:50:30 +02:00
|
|
|
// SetFilePathURLGetter sets the function that resolves an include tag file name to a URL.
|
|
|
|
func SetFilePathURLGetter(getter FilePathURLGetter) {
|
2017-06-17 04:03:28 +02:00
|
|
|
filePathURLGetter = getter
|
|
|
|
}
|
|
|
|
|
2017-06-10 21:38:09 +02:00
|
|
|
// LinkFactory creates a link tag
|
|
|
|
func LinkFactory(p *core.Parser, config *core.Configuration) (core.Tag, error) {
|
|
|
|
start := p.Position
|
|
|
|
p.SkipPastTag()
|
|
|
|
end := p.Position - 2
|
2017-06-17 02:06:55 +02:00
|
|
|
name := strings.TrimSpace(string(p.Data[start:end]))
|
2017-06-17 04:03:28 +02:00
|
|
|
url, ok := filePathURLGetter(name)
|
2017-06-10 21:38:09 +02:00
|
|
|
if !ok {
|
2017-06-17 02:06:55 +02:00
|
|
|
return nil, fmt.Errorf("link tag: %s not found", name)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-13 14:49:44 +02:00
|
|
|
return &Link{url}, nil
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Link tag data, for passing information from the factory to Execute
|
|
|
|
type Link struct {
|
2017-06-13 14:49:44 +02:00
|
|
|
url string
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddCode is equired by the Liquid tag interface
|
|
|
|
func (l *Link) AddCode(code core.Code) {
|
|
|
|
panic("AddCode should not have been called on a Link")
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddSibling is required by the Liquid tag interface
|
|
|
|
func (l *Link) AddSibling(tag core.Tag) error {
|
|
|
|
panic("AddSibling should not have been called on a Link")
|
|
|
|
}
|
|
|
|
|
|
|
|
// LastSibling is required by the Liquid tag interface
|
|
|
|
func (l *Link) LastSibling() core.Tag {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute is required by the Liquid tag interface
|
|
|
|
func (l *Link) Execute(writer io.Writer, data map[string]interface{}) core.ExecuteState {
|
2017-06-13 14:49:44 +02:00
|
|
|
if _, err := writer.Write([]byte(l.url)); err != nil {
|
2017-06-12 02:05:17 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
2017-06-10 21:38:09 +02:00
|
|
|
return core.Normal
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name is required by the Liquid tag interface
|
|
|
|
func (l *Link) Name() string {
|
|
|
|
return "link"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type is required by the Liquid tag interface
|
|
|
|
func (l *Link) Type() core.TagType {
|
|
|
|
return core.StandaloneTag
|
|
|
|
}
|