Skip to content

Commit

Permalink
Merge pull request #5 from gochore/dev
Browse files Browse the repository at this point in the history
support list, headline
  • Loading branch information
wolfogre authored Apr 29, 2020
2 parents 9450a53 + c8945de commit dbcf902
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 20 deletions.
6 changes: 3 additions & 3 deletions email_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func TestEmail_Render(t *testing.T) {
name: "regular",
fields: fields{
elements: []Element{
Table{
Dataset: []TestStruct1{
&Table{
dataset: []TestStruct1{
{
A: "a1",
B: 1,
Expand All @@ -40,7 +40,7 @@ func TestEmail_Render(t *testing.T) {
B: 3,
},
},
Columns: []Column{
columns: []Column{
{
Name: "列1",
Template: "{{.A}}",
Expand Down
18 changes: 18 additions & 0 deletions headline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package emailt

import (
"fmt"
)

type Headline = StringElement

func NewHeadline(level int, format string, a ...interface{}) Headline {
if level < 1 {
level = 1
}
if level > 6 {
level = 6
}
format = fmt.Sprintf("<h%d>%s</h%d>", level, format, level)
return Headline(fmt.Sprintf(format, a...))
}
53 changes: 53 additions & 0 deletions headline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package emailt

import (
"testing"
)

func TestNewHeadline(t *testing.T) {
type args struct {
level int
format string
a []interface{}
}
tests := []struct {
name string
args args
want Headline
}{
{
name: "regular",
args: args{
level: 5,
format: "%v %v",
a: []interface{}{1, "A"},
},
want: "<h5>1 A</h5>",
},
{
name: "h0",
args: args{
level: 0,
format: "%v %v",
a: []interface{}{1, "A"},
},
want: "<h1>1 A</h1>",
},
{
name: "h7",
args: args{
level: 7,
format: "%v %v",
a: []interface{}{1, "A"},
},
want: "<h6>1 A</h6>",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewHeadline(tt.args.level, tt.args.format, tt.args.a...); got != tt.want {
t.Errorf("NewHeadline() = %v, want %v", got, tt.want)
}
})
}
}
54 changes: 54 additions & 0 deletions list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package emailt

import (
"fmt"
"io"
)

type List struct {
items []Element
ordered bool
}

func NewUnorderedList() *List {
return &List{}
}

func NewOrderedList() *List {
return &List{
ordered: true,
}
}

func (l *List) Ordered() bool {
return l.ordered
}

func (l *List) Add(item ...Element) {
l.items = append(l.items, item...)
}

func (l *List) Render(writer io.Writer, themes ...Theme) error {
theme := mergeThemes(themes)

render := newFmtWriter(writer)

tag := "ul"
if l.ordered {
tag = "ol"
}

render.Printlnf("<%s %s>", tag, theme.Attributes(tag))

for _, item := range l.items {
render.Printf("<li %s>", theme.Attributes("li"))
if err := item.Render(render, theme); err != nil {
return fmt.Errorf("render: %w", err)
}
render.Printlnf("</li>")
}

render.Printlnf("</%s>", tag)

return render.Err()
}
77 changes: 77 additions & 0 deletions list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package emailt

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"golang.org/x/net/html"
)

func TestList_Render(t *testing.T) {
type fields struct {
items []Element
ordered bool
}
type args struct {
themes []Theme
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
{
name: "unordered",
fields: fields{
items: []Element{
NewStringElement("A"),
NewStringElement("B"),
},
ordered: false,
},
args: args{},
wantErr: false,
},
{
name: "ordered",
fields: fields{
items: []Element{
NewStringElement("A"),
NewStringElement("B"),
},
ordered: true,
},
args: args{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
l := NewUnorderedList()
if tt.fields.ordered {
l = NewOrderedList()
}
l.Add(tt.fields.items...)
writer := &bytes.Buffer{}
err := l.Render(writer, tt.args.themes...)
if (err != nil) != tt.wantErr {
t.Errorf("Render() error = %v, wantErr %v", err, tt.wantErr)
return
}

if _, err := html.Parse(bytes.NewReader(writer.Bytes())); err != nil {
t.Error(err)
}
t.Log(writer.String())

dir := "output"
_ = os.Mkdir(dir, 0744)
_ = ioutil.WriteFile(filepath.Join(dir, fmt.Sprintf("TestList_Render.%s.html", tt.name)), writer.Bytes(), 0644)
})
}
}
24 changes: 13 additions & 11 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,26 @@ type Column struct {
type Columns []Column

type Table struct {
Dataset interface{}
Columns Columns
dataset interface{}
columns Columns
}

func (t Table) WithDataset(dataset interface{}) Table {
t.Dataset = dataset
return t
func NewTable() *Table {
return &Table{}
}

func (t Table) WithColumns(columns Columns) Table {
t.Columns = columns
return t
func (t *Table) SetDataset(dataset interface{}) {
t.dataset = dataset
}

func (t Table) Render(writer io.Writer, themes ...Theme) error {
func (t *Table) SetColumns(columns Columns) {
t.columns = columns
}

func (t *Table) Render(writer io.Writer, themes ...Theme) error {
theme := mergeThemes(themes)

dataset := reflect.ValueOf(t.Dataset)
dataset := reflect.ValueOf(t.dataset)
if dataset.Kind() != reflect.Slice {
return fmt.Errorf("%v is not a slice", dataset.Type())
}
Expand Down Expand Up @@ -61,7 +63,7 @@ func (t Table) Render(writer io.Writer, themes ...Theme) error {
}
}

columns := t.Columns
columns := t.columns
if len(columns) == 0 {
if mapItem {
var keys []string
Expand Down
4 changes: 3 additions & 1 deletion table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ func TestTable_Render(t1 *testing.T) {
}
for _, tt := range tests {
t1.Run(tt.name, func(t1 *testing.T) {
t := Table{}.WithColumns(tt.fields.Columns).WithDataset(tt.fields.Dataset)
t := NewTable()
t.SetColumns(tt.fields.Columns)
t.SetDataset(tt.fields.Dataset)
got := bytes.NewBuffer(nil)
err := t.Render(got)
if (err != nil) != tt.wantErr {
Expand Down
19 changes: 14 additions & 5 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,22 @@ type fmtWriter struct {
err error
}

func (w fmtWriter) Write(p []byte) (n int, err error) {
return w.writer.Write(p)
}

func newFmtWriter(writer io.Writer) *fmtWriter {
return &fmtWriter{
writer: writer,
err: nil,
}
}

func (w fmtWriter) Write(p []byte) (n int, err error) {
if w.err != nil {
return 0, w.err
}
n, err = w.writer.Write(p)
w.err = err
return
}

func (w *fmtWriter) Err() error {
return w.err
}
Expand All @@ -43,10 +48,14 @@ func (w *fmtWriter) Println(a ...interface{}) {
}

func (w *fmtWriter) Printlnf(format string, a ...interface{}) {
w.Printf(format+"\n", a...)
}

func (w *fmtWriter) Printf(format string, a ...interface{}) {
if w.err != nil {
return
}
_, w.err = fmt.Fprintf(w.writer, format+"\n", a...)
_, w.err = fmt.Fprintf(w.writer, format, a...)
}

func writeStyles(node *html.Node, theme Theme) {
Expand Down

0 comments on commit dbcf902

Please sign in to comment.