mirror of
https://github.com/gohugoio/hugo.git
synced 2024-07-05 08:42:12 +00:00
Add a chomp function.
- Mostly useful in pipelines.
This commit is contained in:
parent
14e93de8a1
commit
6033abe1e7
|
@ -252,6 +252,11 @@ Convert all characters in string to titlecase.
|
||||||
|
|
||||||
e.g. `{{title "BatMan"}}` → "Batman"
|
e.g. `{{title "BatMan"}}` → "Batman"
|
||||||
|
|
||||||
|
### chomp
|
||||||
|
Removes any trailing newline characters. Useful in a pipeline to remove newlines added by other processing (including `markdownify`).
|
||||||
|
|
||||||
|
e.g., `{{chomp "<p>Blockhead</p>\n"` → `"<p>Blockhead</p>"`
|
||||||
|
|
||||||
### highlight
|
### highlight
|
||||||
Take a string of code and a language, uses Pygments to return the syntax
|
Take a string of code and a language, uses Pygments to return the syntax
|
||||||
highlighted code in HTML. Used in the [highlight
|
highlighted code in HTML. Used in the [highlight
|
||||||
|
|
|
@ -29,6 +29,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -37,6 +38,7 @@ import (
|
||||||
var localTemplates *template.Template
|
var localTemplates *template.Template
|
||||||
var tmpl Template
|
var tmpl Template
|
||||||
var funcMap template.FuncMap
|
var funcMap template.FuncMap
|
||||||
|
var chompRegexp *regexp.Regexp
|
||||||
|
|
||||||
type Template interface {
|
type Template interface {
|
||||||
ExecuteTemplate(wr io.Writer, name string, data interface{}) error
|
ExecuteTemplate(wr io.Writer, name string, data interface{}) error
|
||||||
|
@ -667,6 +669,15 @@ func RelRef(page interface{}, ref string) template.HTML {
|
||||||
return refPage(page, ref, "RelRef")
|
return refPage(page, ref, "RelRef")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Chomp(text interface{}) (string, error) {
|
||||||
|
s, err := cast.ToStringE(text)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
return chompRegexp.ReplaceAllString(s, ""), nil
|
||||||
|
}
|
||||||
|
|
||||||
func SafeHtml(text string) template.HTML {
|
func SafeHtml(text string) template.HTML {
|
||||||
return template.HTML(text)
|
return template.HTML(text)
|
||||||
}
|
}
|
||||||
|
@ -1022,5 +1033,8 @@ func init() {
|
||||||
"partial": Partial,
|
"partial": Partial,
|
||||||
"ref": Ref,
|
"ref": Ref,
|
||||||
"relref": RelRef,
|
"relref": RelRef,
|
||||||
|
"chomp": Chomp,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
chompRegexp = regexp.MustCompile("[\r\n]+$")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue