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