1
0
mirror of https://github.com/beego/bee.git synced 2024-12-18 13:40:49 +00:00

Removes the use of template/html

This uses the template/text instead because template/html does not escape
quotes and replaces them with their HTML codes. Added two more util
functions to make it easy to use bold and colored text in the command long
description.
This commit is contained in:
Faissal Elamraoui 2016-12-03 11:46:10 +01:00
parent 6cf7b5d518
commit 196e732e19
2 changed files with 33 additions and 15 deletions

23
bee.go
View File

@ -18,11 +18,11 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"html/template"
"io" "io"
"log" "log"
"os" "os"
"strings" "strings"
"text/template"
) )
const version = "1.5.2" const version = "1.5.2"
@ -41,10 +41,10 @@ type Command struct {
UsageLine string UsageLine string
// Short is the short description shown in the 'go help' output. // Short is the short description shown in the 'go help' output.
Short template.HTML Short string
// Long is the long message shown in the 'go help <this-command>' output. // Long is the long message shown in the 'go help <this-command>' output.
Long template.HTML Long string
// Flag is a set of flags specific to this command. // Flag is a set of flags specific to this command.
Flag flag.FlagSet Flag flag.FlagSet
@ -189,7 +189,7 @@ var helpTemplate = `{{"USAGE" | headline}}
{{if .Options}}{{endline}}{{"OPTIONS" | headline}}{{range $k,$v := .Options}} {{if .Options}}{{endline}}{{"OPTIONS" | headline}}{{range $k,$v := .Options}}
{{$k | printf "-%-11s" | bold}} {{$v}}{{end}}{{endline}}{{end}} {{$k | printf "-%-11s" | bold}} {{$v}}{{end}}{{endline}}{{end}}
{{"DESCRIPTION" | headline}} {{"DESCRIPTION" | headline}}
{{.Long | trim}} {{tmpltostr .Long . | trim}}
` `
var errorTemplate = `bee: %s. var errorTemplate = `bee: %s.
@ -204,18 +204,11 @@ func usage() {
func tmpl(text string, data interface{}) { func tmpl(text string, data interface{}) {
output := NewColorWriter(os.Stderr) output := NewColorWriter(os.Stderr)
t := template.New("top") t := template.New("usage").Funcs(BeeFuncMap())
t.Funcs(template.FuncMap{
"trim": func(s template.HTML) template.HTML { return template.HTML(strings.TrimSpace(string(s))) },
"bold": bold,
"headline": MagentaBold,
"endline": EndLine,
})
template.Must(t.Parse(text)) template.Must(t.Parse(text))
if err := t.Execute(output, data); err != nil {
panic(err) err := t.Execute(output, data)
} MustCheck(err)
} }
func help(args []string) { func help(args []string) {

25
util.go
View File

@ -15,6 +15,7 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
@ -24,6 +25,7 @@ import (
"regexp" "regexp"
"runtime" "runtime"
"strings" "strings"
"text/template"
"time" "time"
) )
@ -281,3 +283,26 @@ func __LINE__() int {
_, _, line, _ := runtime.Caller(1) _, _, line, _ := runtime.Caller(1)
return line return line
} }
// BeeFuncMap returns a FuncMap of functions used in different templates.
func BeeFuncMap() template.FuncMap {
return template.FuncMap{
"trim": strings.TrimSpace,
"bold": bold,
"headline": MagentaBold,
"endline": EndLine,
"tmpltostr": TmplToString,
}
}
// TmplToString parses a text template and return the result as a string.
func TmplToString(tmpl string, data interface{}) string {
t := template.New("tmpl").Funcs(BeeFuncMap())
template.Must(t.Parse(tmpl))
var doc bytes.Buffer
err := t.Execute(&doc, data)
MustCheck(err)
return doc.String()
}