Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(105)

Side by Side Diff: milo/appengine/common/funcs.go

Issue 2718373004: Milo: Print raw json for buildbot build properties (Closed)
Patch Set: Rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "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 "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 "formatJson": formatJson,
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
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 // If the string s is a JSON map, then pretty print it.
207 func formatJson(s string) template.HTML {
208 var o interface{}
209 err := json.Unmarshal([]byte(s), &o)
nodir 2017/03/24 06:05:40 here we parse a JSON-formatted value and then cond
210 if err != nil {
211 return template.HTML(template.HTMLEscapeString(s))
212 }
213 switch t := o.(type) {
214 case string:
215 return template.HTML(template.HTMLEscapeString(t))
216 case map[string]interface{}:
217 s, err := json.MarshalIndent(t, "", " ")
nodir 2017/03/24 06:05:40 AFAIR last time we talked about this I asked to re
nodir 2017/03/24 08:46:06 Obviously please update the code that is currently
218 if err == nil {
219 es := strings.Split(string(s), "\n")
220 var result string
221 for _, line := range es {
222 result += strings.Replace(template.HTMLEscapeStr ing(line), " ", "&nbsp;", -1) + "<br>"
223 }
224 return template.HTML("<div class=\"left\">" + result + " </span>")
225 }
226 }
227 return template.HTML(template.HTMLEscapeString(s))
228 }
229
204 func init() { 230 func init() {
205 linkifySetTemplate = template.Must( 231 linkifySetTemplate = template.Must(
206 template.New("linkifySet"). 232 template.New("linkifySet").
207 Funcs(template.FuncMap{ 233 Funcs(template.FuncMap{
208 "linkify": linkify, 234 "linkify": linkify,
209 }).Parse( 235 }).Parse(
210 `{{ range $i, $link := . }}` + 236 `{{ range $i, $link := . }}` +
211 `{{ if gt $i 0 }} {{ end }}` + 237 `{{ if gt $i 0 }} {{ end }}` +
212 `{{ $link | linkify}}` + 238 `{{ $link | linkify}}` +
213 `{{ end }}`)) 239 `{{ end }}`))
214 240
215 linkifyTemplate = template.Must( 241 linkifyTemplate = template.Must(
216 template.New("linkify"). 242 template.New("linkify").
217 Parse( 243 Parse(
218 `<a href="{{.URL}}">` + 244 `<a href="{{.URL}}">` +
219 `{{if .Img}}<img src="{{.Img}}"{{if .Alt }} alt="{{.Alt}}"{{end}}>` + 245 `{{if .Img}}<img src="{{.Img}}"{{if .Alt }} alt="{{.Alt}}"{{end}}>` +
220 `{{else if .Alias}}[{{.Label}}]` + 246 `{{else if .Alias}}[{{.Label}}]` +
221 `{{else}}{{.Label}}{{end}}` + 247 `{{else}}{{.Label}}{{end}}` +
222 `</a>`)) 248 `</a>`))
223 } 249 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698