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.

51 lines
1.1 KiB

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