You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.1 KiB

  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. package present
  5. import (
  6. "fmt"
  7. "strings"
  8. )
  9. func init() {
  10. Register("image", parseImage)
  11. }
  12. type Image struct {
  13. URL string
  14. Width int
  15. Height int
  16. }
  17. func (i Image) TemplateName() string { return "image" }
  18. func parseImage(ctx *Context, fileName string, lineno int, text string) (Elem, error) {
  19. args := strings.Fields(text)
  20. img := Image{URL: args[1]}
  21. a, err := parseArgs(fileName, lineno, args[2:])
  22. if err != nil {
  23. return nil, err
  24. }
  25. switch len(a) {
  26. case 0:
  27. // no size parameters
  28. case 2:
  29. // If a parameter is empty (underscore) or invalid
  30. // leave the field set to zero. The "image" action
  31. // template will then omit that img tag attribute and
  32. // the browser will calculate the value to preserve
  33. // the aspect ratio.
  34. if v, ok := a[0].(int); ok {
  35. img.Height = v
  36. }
  37. if v, ok := a[1].(int); ok {
  38. img.Width = v
  39. }
  40. default:
  41. return nil, fmt.Errorf("incorrect image invocation: %q", text)
  42. }
  43. return img, nil
  44. }