Timothy Studd | b57d82d | 2016-01-10 14:03:33 -0800 | [diff] [blame] | 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 | |
| 5 | package present |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | func init() { |
| 13 | Register("video", parseVideo) |
| 14 | } |
| 15 | |
| 16 | type Video struct { |
Russ Cox | 657575a | 2020-03-09 22:20:35 -0400 | [diff] [blame] | 17 | Cmd string // original command from present source |
Timothy Studd | b57d82d | 2016-01-10 14:03:33 -0800 | [diff] [blame] | 18 | URL string |
| 19 | SourceType string |
| 20 | Width int |
| 21 | Height int |
| 22 | } |
| 23 | |
Russ Cox | 657575a | 2020-03-09 22:20:35 -0400 | [diff] [blame] | 24 | func (v Video) PresentCmd() string { return v.Cmd } |
Timothy Studd | b57d82d | 2016-01-10 14:03:33 -0800 | [diff] [blame] | 25 | func (v Video) TemplateName() string { return "video" } |
| 26 | |
| 27 | func parseVideo(ctx *Context, fileName string, lineno int, text string) (Elem, error) { |
| 28 | args := strings.Fields(text) |
Dmitri Shuralyov | b2104f8 | 2019-10-25 12:20:13 -0400 | [diff] [blame] | 29 | if len(args) < 3 { |
| 30 | return nil, fmt.Errorf("incorrect video invocation: %q", text) |
| 31 | } |
Russ Cox | 657575a | 2020-03-09 22:20:35 -0400 | [diff] [blame] | 32 | vid := Video{Cmd: text, URL: args[1], SourceType: args[2]} |
Timothy Studd | b57d82d | 2016-01-10 14:03:33 -0800 | [diff] [blame] | 33 | 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 | } |