-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from gochore/dev
support list, headline
- Loading branch information
Showing
8 changed files
with
235 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters