Chromium Code Reviews| Index: milo/appengine/settings/funcs.go |
| diff --git a/milo/appengine/settings/funcs.go b/milo/appengine/settings/funcs.go |
| index 822456043aa22f7735db6d43164d84c686fd91a6..e9d21e794779ea201e4bdf426a5d01c91fa93cfa 100644 |
| --- a/milo/appengine/settings/funcs.go |
| +++ b/milo/appengine/settings/funcs.go |
| @@ -6,6 +6,7 @@ package settings |
| import ( |
| "bytes" |
| + "encoding/json" |
| "fmt" |
| "html/template" |
| "strings" |
| @@ -27,6 +28,7 @@ var funcMap = template.FuncMap{ |
| "startswith": strings.HasPrefix, |
| "sub": sub, |
| "consoleHeader": consoleHeader, |
| + "formatJson": formatJson, |
|
nodir
2017/03/24 06:05:40
s/formatJson/formatJSON/g
|
| } |
| // localTime returns a <span> element with t in human format |
| @@ -186,6 +188,30 @@ func shortHash(s string) string { |
| return s |
| } |
| +// If the string s is a JSON map, then pretty print it. |
| +func formatJson(s string) template.HTML { |
|
nodir
2017/03/24 06:05:40
formatJSON
In Go acronyms are written all caps
|
| + var o interface{} |
| + err := json.Unmarshal([]byte(s), &o) |
| + if err != nil { |
| + return template.HTML(template.HTMLEscapeString(s)) |
| + } |
| + switch t := o.(type) { |
| + case string: |
| + return template.HTML(template.HTMLEscapeString(t)) |
| + case map[string]interface{}: |
| + s, err := json.MarshalIndent(t, "", " ") |
| + if err == nil { |
| + es := strings.Split(string(s), "\n") |
| + var result string |
| + for _, line := range es { |
| + result += strings.Replace(template.HTMLEscapeString(line), " ", " ", -1) + "<br>" |
| + } |
| + return template.HTML("<div class=\"left\">" + result + "</span>") |
| + } |
| + } |
| + return template.HTML(template.HTMLEscapeString(s)) |
| +} |
| + |
| func init() { |
| linkifyTemplate = template.Must( |
| template.New("linkify"). |