diff --git a/server/server.go b/server/server.go index 66a7b913..e49a8348 100644 --- a/server/server.go +++ b/server/server.go @@ -16,6 +16,7 @@ import ( "github.com/jessfraz/reg/clair" "github.com/jessfraz/reg/registry" "github.com/jessfraz/reg/utils" + wordwrap "github.com/mitchellh/go-wordwrap" "github.com/urfave/cli" ) @@ -113,16 +114,7 @@ func main() { templateDir := filepath.Join(staticDir, "../templates") funcMap := template.FuncMap{ "trim": func(s string) string { - a := []rune(s) - var b []rune - for i, r := range a { - // add new line every 80 chars - b = append(b, r) - if i > 0 && (i+1)%80 == 0 { - b = append(b, '\n') - } - } - return string(b) + return wordwrap.WrapString(s, 80) }, } tmpl = template.Must(template.New("").Funcs(funcMap).ParseFiles(filepath.Join(templateDir, "vulns.txt"), filepath.Join(templateDir, "layout.html"))) @@ -278,6 +270,7 @@ func createStaticIndex(r *registry.Registry, staticDir, clairURI string) error { LastUpdated: time.Now().Local().Format(time.RFC1123), } + logrus.Info("rendering index template") if err := renderTemplate(staticDir, "index", "index.html", d); err != nil { return err } @@ -370,7 +363,7 @@ func createVulnStaticPage(r *registry.Registry, staticDir, clairURI, repo, tag s func renderTemplate(staticDir, templateName, dest string, data interface{}) error { // parse & execute the template - logrus.Infof("executing the template %s", templateName) + logrus.Debugf("executing the template %s", templateName) path := filepath.Join(staticDir, dest) if err := os.MkdirAll(filepath.Dir(path), 0644); err != nil { diff --git a/vendor/github.com/mitchellh/go-wordwrap/LICENSE.md b/vendor/github.com/mitchellh/go-wordwrap/LICENSE.md new file mode 100644 index 00000000..22985159 --- /dev/null +++ b/vendor/github.com/mitchellh/go-wordwrap/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2014 Mitchell Hashimoto + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/vendor/github.com/mitchellh/go-wordwrap/wordwrap.go b/vendor/github.com/mitchellh/go-wordwrap/wordwrap.go new file mode 100644 index 00000000..ac67205b --- /dev/null +++ b/vendor/github.com/mitchellh/go-wordwrap/wordwrap.go @@ -0,0 +1,73 @@ +package wordwrap + +import ( + "bytes" + "unicode" +) + +// WrapString wraps the given string within lim width in characters. +// +// Wrapping is currently naive and only happens at white-space. A future +// version of the library will implement smarter wrapping. This means that +// pathological cases can dramatically reach past the limit, such as a very +// long word. +func WrapString(s string, lim uint) string { + // Initialize a buffer with a slightly larger size to account for breaks + init := make([]byte, 0, len(s)) + buf := bytes.NewBuffer(init) + + var current uint + var wordBuf, spaceBuf bytes.Buffer + + for _, char := range s { + if char == '\n' { + if wordBuf.Len() == 0 { + if current+uint(spaceBuf.Len()) > lim { + current = 0 + } else { + current += uint(spaceBuf.Len()) + spaceBuf.WriteTo(buf) + } + spaceBuf.Reset() + } else { + current += uint(spaceBuf.Len() + wordBuf.Len()) + spaceBuf.WriteTo(buf) + spaceBuf.Reset() + wordBuf.WriteTo(buf) + wordBuf.Reset() + } + buf.WriteRune(char) + current = 0 + } else if unicode.IsSpace(char) { + if spaceBuf.Len() == 0 || wordBuf.Len() > 0 { + current += uint(spaceBuf.Len() + wordBuf.Len()) + spaceBuf.WriteTo(buf) + spaceBuf.Reset() + wordBuf.WriteTo(buf) + wordBuf.Reset() + } + + spaceBuf.WriteRune(char) + } else { + + wordBuf.WriteRune(char) + + if current+uint(spaceBuf.Len()+wordBuf.Len()) > lim && uint(wordBuf.Len()) < lim { + buf.WriteRune('\n') + current = 0 + spaceBuf.Reset() + } + } + } + + if wordBuf.Len() == 0 { + if current+uint(spaceBuf.Len()) <= lim { + spaceBuf.WriteTo(buf) + } + } else { + spaceBuf.WriteTo(buf) + wordBuf.WriteTo(buf) + } + + return buf.String() +} diff --git a/vendor/manifest b/vendor/manifest index a3bb06a8..3ad4e2ef 100644 --- a/vendor/manifest +++ b/vendor/manifest @@ -443,6 +443,14 @@ "branch": "master", "notests": true }, + { + "importpath": "github.com/mitchellh/go-wordwrap", + "repository": "https://github.com/mitchellh/go-wordwrap", + "vcs": "git", + "revision": "ad45545899c7b13c020ea92b2072220eefad42b8", + "branch": "master", + "notests": true + }, { "importpath": "github.com/opencontainers/go-digest", "repository": "https://github.com/opencontainers/go-digest",