Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Jan 28, 2017
0 parents commit 5226d92
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
*.log
.DS_Store
doc
tmp
pkg
*.gem
*.pid
coverage
coverage.data
build/*
*.pbxuser
*.mode1v3
.svn
profile
.console_history
.sass-cache/*
.rake_tasks~
*.log.lck
solr/
.jhw-cache/
jhw.*
*.sublime*
node_modules/
dist/
generated/
.vendor/
bin/*
gin-bin
35 changes: 35 additions & 0 deletions block_tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tags

import (
"bytes"
"fmt"
)

type BlockTag struct {
*Tag
Body interface{}
}

func (b BlockTag) String() string {
bb := &bytes.Buffer{}
bb.WriteString(b.Tag.String())
bb.WriteString(fmt.Sprintf("%s", b.Body))
bb.WriteString("</")
bb.WriteString(b.Name)
bb.WriteString(">")
return bb.String()
}

func NewBlockTag(name string, opts Options) *BlockTag {
tag := &BlockTag{
Tag: New(name, opts),
Body: opts["body"],
}
delete(tag.Options, "body")
if tag.Body == nil {
tag.Body = opts["value"]
delete(tag.Options, "value")
}

return tag
}
58 changes: 58 additions & 0 deletions block_tag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package tags_test

import (
"testing"

"github.com/markbates/tags"
"github.com/stretchr/testify/require"
)

func Test_NewBlockTag_WithBody(t *testing.T) {
r := require.New(t)

tag := tags.NewBlockTag("div", tags.Options{
"body": "hi there!",
})
r.Equal(tag.Body, "hi there!")
r.Nil(tag.Options["body"])
}

func Test_NewBlockTag_WithValue(t *testing.T) {
r := require.New(t)

tag := tags.NewBlockTag("div", tags.Options{
"value": "hi there!",
})
r.Equal(tag.Body, "hi there!")
r.Nil(tag.Options["value"])
}

func Test_BlockTag_String(t *testing.T) {
r := require.New(t)

tag := tags.NewBlockTag("div", tags.Options{
"body": "hi there!",
})
r.Equal(`<div>hi there!</div>`, tag.String())
}

func Test_BlockTag_String_WithOpts(t *testing.T) {
r := require.New(t)

tag := tags.NewBlockTag("div", tags.Options{
"body": "hi there!",
"class": "foo bar baz",
})
r.Equal(`<div class="foo bar baz">hi there!</div>`, tag.String())
}

func Test_BlockTag_String_SubTag(t *testing.T) {
r := require.New(t)

tag := tags.NewBlockTag("div", tags.Options{
"body": tags.NewBlockTag("p", tags.Options{
"body": "hi!",
}),
})
r.Equal(`<div><p>hi!</p></div>`, tag.String())
}
32 changes: 32 additions & 0 deletions esc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package tags

import (
"fmt"
"html/template"
"strconv"

"github.com/markbates/pop/nulls"
)

func esc(s interface{}) string {
var st string
switch t := s.(type) {
case fmt.Stringer:
st = t.String()
case nulls.String:
st = t.String
case nulls.Int:
st = strconv.Itoa(t.Int)
case bool:
st = fmt.Sprintf("%t", t)
case string:
st = t
case int:
st = strconv.Itoa(t)
case nil:
st = ""
default:
st = fmt.Sprintf("%s", s)
}
return template.HTMLEscapeString(st)
}
19 changes: 19 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tags

import (
"fmt"
"strings"
)

type Options map[string]interface{}

func (o Options) String() string {
var out = make([]string, 0, len(o))
var tmp = make([]string, 2)
for k, v := range o {
tmp[0] = esc(k)
tmp[1] = fmt.Sprintf("\"%s\"", esc(v))
out = append(out, strings.Join(tmp, "="))
}
return strings.Join(out, " ")
}
30 changes: 30 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package tags_test

import (
"testing"

"github.com/markbates/tags"
"github.com/stretchr/testify/require"
)

func Test_Options_String(t *testing.T) {
r := require.New(t)
o := tags.Options{
"value": "Mark",
"id": "foo-bar",
"class": "foo bar baz",
}
s := o.String()
r.Contains(s, `value="Mark"`)
r.Contains(s, `id="foo-bar"`)
r.Contains(s, `class="foo bar baz"`)
}

func Test_Options_String_Escaped(t *testing.T) {
r := require.New(t)
o := tags.Options{
"<b>": "<p>",
}
s := o.String()
r.Equal(`&lt;b&gt;="&lt;p&gt;"`, s)
}
29 changes: 29 additions & 0 deletions tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tags

import "bytes"

type Tag struct {
Name string
Options Options
}

func (t Tag) String() string {
bb := &bytes.Buffer{}
bb.WriteString("<")
bb.WriteString(t.Name)
if len(t.Options) > 0 {
bb.WriteString(" ")
bb.WriteString(t.Options.String())
}
bb.WriteString(">")
return bb.String()
}

func New(name string, opts Options) *Tag {
tag := &Tag{
Name: name,
Options: opts,
}

return tag
}
29 changes: 29 additions & 0 deletions tag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package tags_test

import (
"testing"

"github.com/markbates/tags"
"github.com/stretchr/testify/require"
)

func Test_Tag(t *testing.T) {
r := require.New(t)
tag := tags.New("input", tags.Options{})
r.Equal("input", tag.Name)
}

func Test_Tag_WithName(t *testing.T) {
r := require.New(t)
tag := tags.New("div", tags.Options{})
r.Equal("div", tag.Name)
r.Equal(`<div>`, tag.String())
}

func Test_Tag_WithValue(t *testing.T) {
r := require.New(t)
tag := tags.New("input", tags.Options{
"value": "Mark",
})
r.Equal(`<input value="Mark">`, tag.String())
}

0 comments on commit 5226d92

Please sign in to comment.