summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2018-11-17 13:01:20 -0500
committerDave Henderson <dhenderson@gmail.com>2018-11-17 13:01:20 -0500
commita95a25dac45df2fd04fba321eb66dbb868c1283a (patch)
treee6e8b0d9d451b2ce5e64dfec6e08f749722940d1
parent0cf7734ef776cc7701dfbb08b080cecf5066f0eb (diff)
Adding quote and squote functions
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
-rw-r--r--docs-src/content/functions/strings.yml40
-rw-r--r--docs/content/functions/strings.md70
-rw-r--r--funcs/strings.go15
-rw-r--r--funcs/strings_test.go38
4 files changed, 163 insertions, 0 deletions
diff --git a/docs-src/content/functions/strings.yml b/docs-src/content/functions/strings.yml
index 6e3807c3..172184db 100644
--- a/docs-src/content/functions/strings.yml
+++ b/docs-src/content/functions/strings.yml
@@ -1,6 +1,29 @@
ns: strings
preamble: ''
funcs:
+ - name: strings.Quote
+ alias: quote
+ description: |
+ Surrounds an input string with double-quote characters (`"`). If the input is not a string, converts first.
+
+ `"` characters in the input are first escaped with a `\` character.
+
+ This is a convenience function which is equivalent to:
+
+ ```
+ {{ print "%q" "input string" }}
+ ```
+ pipeline: true
+ arguments:
+ - name: in
+ required: true
+ description: The input to quote
+ examples:
+ - |
+ $ gomplate -i '{{ "in" | quote }}'
+ "in"
+ $ gomplate -i '{{ strings.Quote 500 }}'
+ "500"
- name: strings.Sort
alias: sort
description: |
@@ -14,3 +37,20 @@ funcs:
- |
$ gomplate -i '{{ (slice "foo" "bar" "baz") | sort }}'
[bar baz foo]
+ - name: strings.Squote
+ alias: squote
+ description: |
+ Surrounds an input string with a single-quote (apostrophe) character (`'`). If the input is not a string, converts first.
+
+ `'` characters in the input are first escaped in the YAML-style (by repetition: `''`).
+ pipeline: true
+ arguments:
+ - name: in
+ required: true
+ description: The input to quote
+ examples:
+ - |
+ $ gomplate -i '{{ "in" | squote }}'
+ 'in'
+ $ gomplate -i "{{ strings.Squote \"it's a banana\" }}"
+ 'it''s a banana'
diff --git a/docs/content/functions/strings.md b/docs/content/functions/strings.md
index 1f1dae42..460212ad 100644
--- a/docs/content/functions/strings.md
+++ b/docs/content/functions/strings.md
@@ -224,6 +224,44 @@ foo
bar:baz
```
+## `strings.Quote`
+
+**Alias:** `quote`
+
+Surrounds an input string with double-quote characters (`"`). If the input is not a string, converts first.
+
+`"` characters in the input are first escaped with a `\` character.
+
+This is a convenience function which is equivalent to:
+
+```
+{{ print "%q" "input string" }}
+```
+
+### Usage
+```go
+strings.Quote in
+```
+
+```go
+in | strings.Quote
+```
+
+### Arguments
+
+| name | description |
+|------|-------------|
+| `in` | _(required)_ The input to quote |
+
+### Examples
+
+```console
+$ gomplate -i '{{ "in" | quote }}'
+"in"
+$ gomplate -i '{{ strings.Quote 500 }}'
+"500"
+```
+
## `strings.Repeat`
Returns a new string consisting of `count` copies of the input string.
@@ -291,6 +329,38 @@ $ echo 'Rock & Roll @ Cafe Wha?' | gomplate -d in=stdin: -i '{{ strings.Slug (in
rock-and-roll-at-cafe-wha
```
+## `strings.Squote`
+
+**Alias:** `squote`
+
+Surrounds an input string with a single-quote (apostrophe) character (`'`). If the input is not a string, converts first.
+
+`'` characters in the input are first escaped in the YAML-style (by repetition: `''`).
+
+### Usage
+```go
+strings.Squote in
+```
+
+```go
+in | strings.Squote
+```
+
+### Arguments
+
+| name | description |
+|------|-------------|
+| `in` | _(required)_ The input to quote |
+
+### Examples
+
+```console
+$ gomplate -i '{{ "in" | squote }}'
+'in'
+$ gomplate -i "{{ strings.Squote \"it's a banana\" }}"
+'it''s a banana'
+```
+
## `strings.Title`
**Alias:** `title`
diff --git a/funcs/strings.go b/funcs/strings.go
index f803a48d..fbf58dcc 100644
--- a/funcs/strings.go
+++ b/funcs/strings.go
@@ -6,6 +6,7 @@ package funcs
// in templates easier.
import (
+ "fmt"
"sync"
"github.com/Masterminds/goutils"
@@ -40,6 +41,8 @@ func AddStringFuncs(f map[string]interface{}) {
f["trimSpace"] = StrNS().TrimSpace
f["indent"] = StrNS().Indent
f["sort"] = StrNS().Sort
+ f["quote"] = StrNS().Quote
+ f["squote"] = StrNS().Squote
// these are legacy aliases with non-pipelinable arg order
f["contains"] = strings.Contains
@@ -208,3 +211,15 @@ func (f *StringFuncs) Indent(args ...interface{}) (string, error) {
func (f *StringFuncs) Slug(in interface{}) string {
return slug.Make(conv.ToString(in))
}
+
+// Quote -
+func (f *StringFuncs) Quote(in interface{}) string {
+ return fmt.Sprintf("%q", conv.ToString(in))
+}
+
+// Squote -
+func (f *StringFuncs) Squote(in interface{}) string {
+ s := conv.ToString(in)
+ s = strings.Replace(s, `'`, `''`, -1)
+ return fmt.Sprintf("'%s'", s)
+}
diff --git a/funcs/strings_test.go b/funcs/strings_test.go
index f4da1a73..04a97bcc 100644
--- a/funcs/strings_test.go
+++ b/funcs/strings_test.go
@@ -106,3 +106,41 @@ func TestSort(t *testing.T) {
assert.Equal(t, out, must(sf.Sort([]interface{}{"foo", "bar", "baz"})))
}
+
+func TestQuote(t *testing.T) {
+ sf := &StringFuncs{}
+ testdata := []struct {
+ in interface{}
+ out string
+ }{
+ {``, `""`},
+ {`foo`, `"foo"`},
+ {nil, `"nil"`},
+ {123.4, `"123.4"`},
+ {`hello "world"`, `"hello \"world\""`},
+ {`it's its`, `"it's its"`},
+ }
+
+ for _, d := range testdata {
+ assert.Equal(t, d.out, sf.Quote(d.in))
+ }
+}
+
+func TestSquote(t *testing.T) {
+ sf := &StringFuncs{}
+ testdata := []struct {
+ in interface{}
+ out string
+ }{
+ {``, `''`},
+ {`foo`, `'foo'`},
+ {nil, `'nil'`},
+ {123.4, `'123.4'`},
+ {`hello "world"`, `'hello "world"'`},
+ {`it's its`, `'it''s its'`},
+ }
+
+ for _, d := range testdata {
+ assert.Equal(t, d.out, sf.Squote(d.in))
+ }
+}