2013-09-18 18:55:46 -06:00
|
|
|
// Copyright 2012 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package present
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Register("image", parseImage)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Image struct {
|
2020-03-09 20:20:35 -06:00
|
|
|
Cmd string // original command from present source
|
2013-09-18 18:55:46 -06:00
|
|
|
URL string
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
}
|
|
|
|
|
2020-03-09 20:20:35 -06:00
|
|
|
func (i Image) PresentCmd() string { return i.Cmd }
|
2013-09-18 18:55:46 -06:00
|
|
|
func (i Image) TemplateName() string { return "image" }
|
|
|
|
|
|
|
|
func parseImage(ctx *Context, fileName string, lineno int, text string) (Elem, error) {
|
|
|
|
args := strings.Fields(text)
|
2019-10-25 10:20:13 -06:00
|
|
|
if len(args) < 2 {
|
|
|
|
return nil, fmt.Errorf("incorrect image invocation: %q", text)
|
|
|
|
}
|
2020-03-09 20:20:35 -06:00
|
|
|
img := Image{Cmd: text, URL: args[1]}
|
2013-09-18 18:55:46 -06:00
|
|
|
a, err := parseArgs(fileName, lineno, args[2:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
switch len(a) {
|
|
|
|
case 0:
|
|
|
|
// no size parameters
|
|
|
|
case 2:
|
2014-06-12 16:45:26 -06:00
|
|
|
// If a parameter is empty (underscore) or invalid
|
|
|
|
// leave the field set to zero. The "image" action
|
|
|
|
// template will then omit that img tag attribute and
|
|
|
|
// the browser will calculate the value to preserve
|
|
|
|
// the aspect ratio.
|
2013-09-18 18:55:46 -06:00
|
|
|
if v, ok := a[0].(int); ok {
|
|
|
|
img.Height = v
|
|
|
|
}
|
|
|
|
if v, ok := a[1].(int); ok {
|
|
|
|
img.Width = v
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("incorrect image invocation: %q", text)
|
|
|
|
}
|
|
|
|
return img, nil
|
|
|
|
}
|