| 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 common | 5 package common |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "fmt" | 9 "fmt" |
| 10 "html/template" | 10 "html/template" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 "linkify": linkify, | 24 "linkify": linkify, |
| 25 "linkifySet": linkifySet, | 25 "linkifySet": linkifySet, |
| 26 "obfuscateEmail": obfuscateEmail, | 26 "obfuscateEmail": obfuscateEmail, |
| 27 "localTime": localTime, | 27 "localTime": localTime, |
| 28 "shortHash": shortHash, | 28 "shortHash": shortHash, |
| 29 "startswith": strings.HasPrefix, | 29 "startswith": strings.HasPrefix, |
| 30 "sub": sub, | 30 "sub": sub, |
| 31 "consoleHeader": consoleHeader, | 31 "consoleHeader": consoleHeader, |
| 32 "pagedURL": pagedURL, | 32 "pagedURL": pagedURL, |
| 33 "formatTime": formatTime, | 33 "formatTime": formatTime, |
| 34 "percent": percent, |
| 34 } | 35 } |
| 35 | 36 |
| 36 // localTime returns a <span> element with t in human format | 37 // localTime returns a <span> element with t in human format |
| 37 // that will be converted to local timezone in the browser. | 38 // that will be converted to local timezone in the browser. |
| 38 // Recommended usage: {{ .Date | localTime "N/A" }} | 39 // Recommended usage: {{ .Date | localTime "N/A" }} |
| 39 func localTime(ifZero string, t time.Time) template.HTML { | 40 func localTime(ifZero string, t time.Time) template.HTML { |
| 40 if t.IsZero() { | 41 if t.IsZero() { |
| 41 return template.HTML(template.HTMLEscapeString(ifZero)) | 42 return template.HTML(template.HTMLEscapeString(ifZero)) |
| 42 } | 43 } |
| 43 milliseconds := t.UnixNano() / 1e6 | 44 milliseconds := t.UnixNano() / 1e6 |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 case limit < 0: | 239 case limit < 0: |
| 239 values.Del("limit") | 240 values.Del("limit") |
| 240 case limit > 0: | 241 case limit > 0: |
| 241 values.Set("limit", fmt.Sprintf("%d", limit)) | 242 values.Set("limit", fmt.Sprintf("%d", limit)) |
| 242 } | 243 } |
| 243 result := *r.URL | 244 result := *r.URL |
| 244 result.RawQuery = values.Encode() | 245 result.RawQuery = values.Encode() |
| 245 return result.String() | 246 return result.String() |
| 246 } | 247 } |
| 247 | 248 |
| 249 // percent divides one number by a divisor and returns the percentage in string
form. |
| 250 func percent(numerator, divisor int) string { |
| 251 p := float64(numerator) * 100.0 / float64(divisor) |
| 252 return fmt.Sprintf("%.1f", p) |
| 253 } |
| 254 |
| 248 func init() { | 255 func init() { |
| 249 linkifySetTemplate = template.Must( | 256 linkifySetTemplate = template.Must( |
| 250 template.New("linkifySet"). | 257 template.New("linkifySet"). |
| 251 Funcs(template.FuncMap{ | 258 Funcs(template.FuncMap{ |
| 252 "linkify": linkify, | 259 "linkify": linkify, |
| 253 }).Parse( | 260 }).Parse( |
| 254 `{{ range $i, $link := . }}` + | 261 `{{ range $i, $link := . }}` + |
| 255 `{{ if gt $i 0 }} {{ end }}` + | 262 `{{ if gt $i 0 }} {{ end }}` + |
| 256 `{{ $link | linkify}}` + | 263 `{{ $link | linkify}}` + |
| 257 `{{ end }}`)) | 264 `{{ end }}`)) |
| 258 | 265 |
| 259 linkifyTemplate = template.Must( | 266 linkifyTemplate = template.Must( |
| 260 template.New("linkify"). | 267 template.New("linkify"). |
| 261 Parse( | 268 Parse( |
| 262 `<a href="{{.URL}}">` + | 269 `<a href="{{.URL}}">` + |
| 263 `{{if .Img}}<img src="{{.Img}}"{{if .Alt
}} alt="{{.Alt}}"{{end}}>` + | 270 `{{if .Img}}<img src="{{.Img}}"{{if .Alt
}} alt="{{.Alt}}"{{end}}>` + |
| 264 `{{else if .Alias}}[{{.Label}}]` + | 271 `{{else if .Alias}}[{{.Label}}]` + |
| 265 `{{else}}{{.Label}}{{end}}` + | 272 `{{else}}{{.Label}}{{end}}` + |
| 266 `</a>`)) | 273 `</a>`)) |
| 267 } | 274 } |
| OLD | NEW |