Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package settings | 5 package settings |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "encoding/json" | |
| 9 "fmt" | 10 "fmt" |
| 10 "html/template" | 11 "html/template" |
| 11 "strings" | 12 "strings" |
| 12 "time" | 13 "time" |
| 13 | 14 |
| 14 "github.com/luci/luci-go/milo/api/resp" | 15 "github.com/luci/luci-go/milo/api/resp" |
| 15 ) | 16 ) |
| 16 | 17 |
| 17 // A collection of useful templating functions | 18 // A collection of useful templating functions |
| 18 | 19 |
| 19 // funcMap is what gets fed into the template bundle. | 20 // funcMap is what gets fed into the template bundle. |
| 20 var funcMap = template.FuncMap{ | 21 var funcMap = template.FuncMap{ |
| 21 "humanDuration": humanDuration, | 22 "humanDuration": humanDuration, |
| 22 "parseRFC3339": parseRFC3339, | 23 "parseRFC3339": parseRFC3339, |
| 23 "linkify": linkify, | 24 "linkify": linkify, |
| 24 "obfuscateEmail": obfuscateEmail, | 25 "obfuscateEmail": obfuscateEmail, |
| 25 "localTime": localTime, | 26 "localTime": localTime, |
| 26 "shortHash": shortHash, | 27 "shortHash": shortHash, |
| 27 "startswith": strings.HasPrefix, | 28 "startswith": strings.HasPrefix, |
| 28 "sub": sub, | 29 "sub": sub, |
| 29 "consoleHeader": consoleHeader, | 30 "consoleHeader": consoleHeader, |
| 31 "formatJson": formatJson, | |
|
nodir
2017/03/24 06:05:40
s/formatJson/formatJSON/g
| |
| 30 } | 32 } |
| 31 | 33 |
| 32 // localTime returns a <span> element with t in human format | 34 // localTime returns a <span> element with t in human format |
| 33 // that will be converted to local timezone in the browser. | 35 // that will be converted to local timezone in the browser. |
| 34 // Recommended usage: {{ .Date | localTime "N/A" }} | 36 // Recommended usage: {{ .Date | localTime "N/A" }} |
| 35 func localTime(ifZero string, t time.Time) template.HTML { | 37 func localTime(ifZero string, t time.Time) template.HTML { |
| 36 if t.IsZero() { | 38 if t.IsZero() { |
| 37 return template.HTML(template.HTMLEscapeString(ifZero)) | 39 return template.HTML(template.HTMLEscapeString(ifZero)) |
| 38 } | 40 } |
| 39 milliseconds := t.UnixNano() / 1e6 | 41 milliseconds := t.UnixNano() / 1e6 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 } | 181 } |
| 180 | 182 |
| 181 // shortHash abbriviates a git hash into 6 characters. | 183 // shortHash abbriviates a git hash into 6 characters. |
| 182 func shortHash(s string) string { | 184 func shortHash(s string) string { |
| 183 if len(s) > 6 { | 185 if len(s) > 6 { |
| 184 return s[0:6] | 186 return s[0:6] |
| 185 } | 187 } |
| 186 return s | 188 return s |
| 187 } | 189 } |
| 188 | 190 |
| 191 // If the string s is a JSON map, then pretty print it. | |
| 192 func formatJson(s string) template.HTML { | |
|
nodir
2017/03/24 06:05:40
formatJSON
In Go acronyms are written all caps
| |
| 193 var o interface{} | |
| 194 err := json.Unmarshal([]byte(s), &o) | |
| 195 if err != nil { | |
| 196 return template.HTML(template.HTMLEscapeString(s)) | |
| 197 } | |
| 198 switch t := o.(type) { | |
| 199 case string: | |
| 200 return template.HTML(template.HTMLEscapeString(t)) | |
| 201 case map[string]interface{}: | |
| 202 s, err := json.MarshalIndent(t, "", " ") | |
| 203 if err == nil { | |
| 204 es := strings.Split(string(s), "\n") | |
| 205 var result string | |
| 206 for _, line := range es { | |
| 207 result += strings.Replace(template.HTMLEscapeStr ing(line), " ", " ", -1) + "<br>" | |
| 208 } | |
| 209 return template.HTML("<div class=\"left\">" + result + " </span>") | |
| 210 } | |
| 211 } | |
| 212 return template.HTML(template.HTMLEscapeString(s)) | |
| 213 } | |
| 214 | |
| 189 func init() { | 215 func init() { |
| 190 linkifyTemplate = template.Must( | 216 linkifyTemplate = template.Must( |
| 191 template.New("linkify"). | 217 template.New("linkify"). |
| 192 Funcs(template.FuncMap{ | 218 Funcs(template.FuncMap{ |
| 193 "linkify": linkify, | 219 "linkify": linkify, |
| 194 }).Parse( | 220 }).Parse( |
| 195 `<a href="{{.URL}}">` + | 221 `<a href="{{.URL}}">` + |
| 196 `{{if .Img}}<img src="{{.Img}}"{{if .Alt}} alt=" {{.Alt}}"{{end}}>` + | 222 `{{if .Img}}<img src="{{.Img}}"{{if .Alt}} alt=" {{.Alt}}"{{end}}>` + |
| 197 `{{else}}{{.Label}}{{end}}` + | 223 `{{else}}{{.Label}}{{end}}` + |
| 198 `</a>` + | 224 `</a>` + |
| 199 `{{range .Aliases}} [{{. | linkify}}]{{end}}`)) | 225 `{{range .Aliases}} [{{. | linkify}}]{{end}}`)) |
| 200 } | 226 } |
| OLD | NEW |