blob: ad06bfb5ded1a54f4bec9b2fb80fa8844375f567 [file] [log] [blame]
Timothy Studdb57d82d2016-01-10 14:03:33 -08001// 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
5package present
6
7import (
8 "fmt"
9 "strings"
10)
11
12func init() {
13 Register("video", parseVideo)
14}
15
16type Video struct {
Russ Cox657575a2020-03-09 22:20:35 -040017 Cmd string // original command from present source
Timothy Studdb57d82d2016-01-10 14:03:33 -080018 URL string
19 SourceType string
20 Width int
21 Height int
22}
23
Russ Cox657575a2020-03-09 22:20:35 -040024func (v Video) PresentCmd() string { return v.Cmd }
Timothy Studdb57d82d2016-01-10 14:03:33 -080025func (v Video) TemplateName() string { return "video" }
26
27func parseVideo(ctx *Context, fileName string, lineno int, text string) (Elem, error) {
28 args := strings.Fields(text)
Dmitri Shuralyovb2104f82019-10-25 12:20:13 -040029 if len(args) < 3 {
30 return nil, fmt.Errorf("incorrect video invocation: %q", text)
31 }
Russ Cox657575a2020-03-09 22:20:35 -040032 vid := Video{Cmd: text, URL: args[1], SourceType: args[2]}
Timothy Studdb57d82d2016-01-10 14:03:33 -080033 a, err := parseArgs(fileName, lineno, args[3:])
34 if err != nil {
35 return nil, err
36 }
37 switch len(a) {
38 case 0:
39 // no size parameters
40 case 2:
41 // If a parameter is empty (underscore) or invalid
42 // leave the field set to zero. The "video" action
43 // template will then omit that vid tag attribute and
44 // the browser will calculate the value to preserve
45 // the aspect ratio.
46 if v, ok := a[0].(int); ok {
47 vid.Height = v
48 }
49 if v, ok := a[1].(int); ok {
50 vid.Width = v
51 }
52 default:
53 return nil, fmt.Errorf("incorrect video invocation: %q", text)
54 }
55 return vid, nil
56}