| 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" |
| 11 "net/http" |
| 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 "linkifySet": linkifySet, | 25 "linkifySet": linkifySet, |
| 25 "obfuscateEmail": obfuscateEmail, | 26 "obfuscateEmail": obfuscateEmail, |
| 26 "localTime": localTime, | 27 "localTime": localTime, |
| 27 "shortHash": shortHash, | 28 "shortHash": shortHash, |
| 28 "startswith": strings.HasPrefix, | 29 "startswith": strings.HasPrefix, |
| 29 "sub": sub, | 30 "sub": sub, |
| 30 "consoleHeader": consoleHeader, | 31 "consoleHeader": consoleHeader, |
| 32 "pagedURL": pagedURL, |
| 31 } | 33 } |
| 32 | 34 |
| 33 // localTime returns a <span> element with t in human format | 35 // localTime returns a <span> element with t in human format |
| 34 // that will be converted to local timezone in the browser. | 36 // that will be converted to local timezone in the browser. |
| 35 // Recommended usage: {{ .Date | localTime "N/A" }} | 37 // Recommended usage: {{ .Date | localTime "N/A" }} |
| 36 func localTime(ifZero string, t time.Time) template.HTML { | 38 func localTime(ifZero string, t time.Time) template.HTML { |
| 37 if t.IsZero() { | 39 if t.IsZero() { |
| 38 return template.HTML(template.HTMLEscapeString(ifZero)) | 40 return template.HTML(template.HTMLEscapeString(ifZero)) |
| 39 } | 41 } |
| 40 milliseconds := t.UnixNano() / 1e6 | 42 milliseconds := t.UnixNano() / 1e6 |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 } | 196 } |
| 195 | 197 |
| 196 // shortHash abbriviates a git hash into 6 characters. | 198 // shortHash abbriviates a git hash into 6 characters. |
| 197 func shortHash(s string) string { | 199 func shortHash(s string) string { |
| 198 if len(s) > 6 { | 200 if len(s) > 6 { |
| 199 return s[0:6] | 201 return s[0:6] |
| 200 } | 202 } |
| 201 return s | 203 return s |
| 202 } | 204 } |
| 203 | 205 |
| 206 // pagedURL returns a self URL with the given cursor and limit paging options. |
| 207 // if limit is set to 0, then inherit whatever limit is set in request. If |
| 208 // both are unspecified, then limit is omitted. |
| 209 func pagedURL(r *http.Request, limit int, cursor string) string { |
| 210 if limit == 0 { |
| 211 var err error |
| 212 limit, err = GetLimit(r) |
| 213 if err != nil { |
| 214 // This should not happen because the handler should've
already validated the |
| 215 // limit earlier in the process. |
| 216 panic(err) |
| 217 } |
| 218 if limit < 0 { |
| 219 limit = 0 |
| 220 } |
| 221 } |
| 222 values := r.URL.Query() |
| 223 switch cursor { |
| 224 case "EMPTY": |
| 225 values.Del("cursor") |
| 226 case "": |
| 227 // Do nothing, just leave the cursor in. |
| 228 default: |
| 229 values.Set("cursor", cursor) |
| 230 } |
| 231 switch { |
| 232 case limit < 0: |
| 233 values.Del("limit") |
| 234 case limit > 0: |
| 235 values.Set("limit", fmt.Sprintf("%d", limit)) |
| 236 } |
| 237 result := *r.URL |
| 238 result.RawQuery = values.Encode() |
| 239 return result.String() |
| 240 } |
| 241 |
| 204 func init() { | 242 func init() { |
| 205 linkifySetTemplate = template.Must( | 243 linkifySetTemplate = template.Must( |
| 206 template.New("linkifySet"). | 244 template.New("linkifySet"). |
| 207 Funcs(template.FuncMap{ | 245 Funcs(template.FuncMap{ |
| 208 "linkify": linkify, | 246 "linkify": linkify, |
| 209 }).Parse( | 247 }).Parse( |
| 210 `{{ range $i, $link := . }}` + | 248 `{{ range $i, $link := . }}` + |
| 211 `{{ if gt $i 0 }} {{ end }}` + | 249 `{{ if gt $i 0 }} {{ end }}` + |
| 212 `{{ $link | linkify}}` + | 250 `{{ $link | linkify}}` + |
| 213 `{{ end }}`)) | 251 `{{ end }}`)) |
| 214 | 252 |
| 215 linkifyTemplate = template.Must( | 253 linkifyTemplate = template.Must( |
| 216 template.New("linkify"). | 254 template.New("linkify"). |
| 217 Parse( | 255 Parse( |
| 218 `<a href="{{.URL}}">` + | 256 `<a href="{{.URL}}">` + |
| 219 `{{if .Img}}<img src="{{.Img}}"{{if .Alt
}} alt="{{.Alt}}"{{end}}>` + | 257 `{{if .Img}}<img src="{{.Img}}"{{if .Alt
}} alt="{{.Alt}}"{{end}}>` + |
| 220 `{{else if .Alias}}[{{.Label}}]` + | 258 `{{else if .Alias}}[{{.Label}}]` + |
| 221 `{{else}}{{.Label}}{{end}}` + | 259 `{{else}}{{.Label}}{{end}}` + |
| 222 `</a>`)) | 260 `</a>`)) |
| 223 } | 261 } |
| OLD | NEW |