1// Copyright 2012 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package present
6
7import (
8	"fmt"
9	"strings"
10)
11
12func init() {
13	Register("image", parseImage)
14}
15
16type Image struct {
17	URL    string
18	Width  int
19	Height int
20}
21
22func (i Image) TemplateName() string { return "image" }
23
24func parseImage(ctx *Context, fileName string, lineno int, text string) (Elem, error) {
25	args := strings.Fields(text)
26	img := Image{URL: args[1]}
27	a, err := parseArgs(fileName, lineno, args[2:])
28	if err != nil {
29		return nil, err
30	}
31	switch len(a) {
32	case 0:
33		// no size parameters
34	case 2:
35		// If a parameter is empty (underscore) or invalid
36		// leave the field set to zero. The "image" action
37		// template will then omit that img tag attribute and
38		// the browser will calculate the value to preserve
39		// the aspect ratio.
40		if v, ok := a[0].(int); ok {
41			img.Height = v
42		}
43		if v, ok := a[1].(int); ok {
44			img.Width = v
45		}
46	default:
47		return nil, fmt.Errorf("incorrect image invocation: %q", text)
48	}
49	return img, nil
50}
51