github.com/prattmic/llgo-embedded@v0.0.0-20150820070356-41cfecea0e1e/third_party/gofrontend/libgo/go/old/template/format.go (about) 1 // Copyright 2009 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 // Template library: default formatters 6 7 package template 8 9 import ( 10 "bytes" 11 "fmt" 12 "io" 13 ) 14 15 // StringFormatter formats into the default string representation. 16 // It is stored under the name "str" and is the default formatter. 17 // You can override the default formatter by storing your default 18 // under the name "" in your custom formatter map. 19 func StringFormatter(w io.Writer, format string, value ...interface{}) { 20 if len(value) == 1 { 21 if b, ok := value[0].([]byte); ok { 22 w.Write(b) 23 return 24 } 25 } 26 fmt.Fprint(w, value...) 27 } 28 29 var ( 30 esc_quot = []byte(""") // shorter than """ 31 esc_apos = []byte("'") // shorter than "'" 32 esc_amp = []byte("&") 33 esc_lt = []byte("<") 34 esc_gt = []byte(">") 35 ) 36 37 // HTMLEscape writes to w the properly escaped HTML equivalent 38 // of the plain text data s. 39 func HTMLEscape(w io.Writer, s []byte) { 40 var esc []byte 41 last := 0 42 for i, c := range s { 43 switch c { 44 case '"': 45 esc = esc_quot 46 case '\'': 47 esc = esc_apos 48 case '&': 49 esc = esc_amp 50 case '<': 51 esc = esc_lt 52 case '>': 53 esc = esc_gt 54 default: 55 continue 56 } 57 w.Write(s[last:i]) 58 w.Write(esc) 59 last = i + 1 60 } 61 w.Write(s[last:]) 62 } 63 64 // HTMLFormatter formats arbitrary values for HTML 65 func HTMLFormatter(w io.Writer, format string, value ...interface{}) { 66 ok := false 67 var b []byte 68 if len(value) == 1 { 69 b, ok = value[0].([]byte) 70 } 71 if !ok { 72 var buf bytes.Buffer 73 fmt.Fprint(&buf, value...) 74 b = buf.Bytes() 75 } 76 HTMLEscape(w, b) 77 }