fancl hai 1 ano
pai
achega
c6b1efa7a8
Modificáronse 6 ficheiros con 60 adicións e 27 borrados
  1. 2 2
      element/alternative.go
  2. 5 6
      element/button.go
  3. 40 0
      element/interface.go
  4. 6 11
      element/text.go
  5. 4 4
      pgenr.go
  6. 3 4
      render_test.go

+ 2 - 2
element/alternative.go

@@ -24,8 +24,8 @@ func (element *Alternative) Html() string {
 	return element.String()
 }
 
-func NewAlternative() *Alternative {
+func NewAlternative(eles ...Element) *Alternative {
 	return &Alternative{
-		Elements: make([]Element, 0),
+		Elements: eles,
 	}
 }

+ 5 - 6
element/button.go

@@ -2,7 +2,6 @@ package element
 
 import (
 	"golang.org/x/net/html/atom"
-	"html"
 )
 
 type (
@@ -27,11 +26,11 @@ func (element *Button) String() string {
 	if element.Color != "" {
 		element.Style["color"] = element.Color
 	}
-	return renderTag(atom.A.String(), Attrs{
-		"class": "button",
-		"href":  element.Url,
-		"style": element.Style.String(),
-	}, html.EscapeString(element.Text))
+	return NewElement(atom.A, element.Text,
+		WithTagAttribute("class", "button"),
+		WithTagAttribute("href", element.Url),
+		WithTagAttribute("style", element.Style.String()),
+	).String()
 }
 
 func NewButton(label, link string, opts ...ButtonOption) *Button {

+ 40 - 0
element/interface.go

@@ -0,0 +1,40 @@
+package element
+
+import (
+	"golang.org/x/net/html/atom"
+	"html"
+)
+
+type (
+	TagOption func(t *element)
+	element   struct {
+		Name    atom.Atom
+		Attr    Attrs
+		Content string
+	}
+)
+
+func WithTagAttribute(label string, value string) TagOption {
+	return func(t *element) {
+		if t.Attr == nil {
+			t.Attr = make(map[string]string)
+		}
+		t.Attr[label] = value
+	}
+}
+
+func (tag *element) Html() string {
+	return tag.String()
+}
+
+func (tag *element) String() string {
+	return renderTag(tag.Name.String(), tag.Attr, html.EscapeString(tag.Content))
+}
+
+func NewElement(atom atom.Atom, content string, opts ...TagOption) *element {
+	e := &element{Name: atom, Content: content}
+	for _, cb := range opts {
+		cb(e)
+	}
+	return e
+}

+ 6 - 11
element/text.go

@@ -2,7 +2,6 @@ package element
 
 import (
 	"golang.org/x/net/html/atom"
-	"html"
 )
 
 const (
@@ -34,12 +33,6 @@ func WithTextStyle(ms map[string]string) TextOption {
 	}
 }
 
-func WithTextTag(tag atom.Atom) TextOption {
-	return func(t *Text) {
-		t.Tag = tag
-	}
-}
-
 func WithTextColor(color string) TextOption {
 	return func(t *Text) {
 		t.Color = color
@@ -64,10 +57,12 @@ func (text *Text) String() string {
 	if text.Theme != "" {
 		className = "text-" + text.Theme
 	}
-	return renderTag(text.Tag.String(), Attrs{
-		"class": className,
-		"style": text.Style.String(),
-	}, html.EscapeString(text.Content))
+	return NewElement(
+		text.Tag,
+		text.Content,
+		WithTagAttribute("class", className),
+		WithTagAttribute("style", text.Style.String()),
+	).String()
 }
 
 func NewText(s string, opts ...TextOption) *Text {

+ 4 - 4
pgenr.go

@@ -31,7 +31,7 @@ func (page *Page) SetCopyright(s string) *Page {
 }
 
 func (page *Page) AddPlainIntro(s string) *Page {
-	page.Intros = append(page.Intros, element.NewText(s, element.WithTextTag(atom.P)))
+	page.Intros = append(page.Intros, element.NewElement(atom.P, s))
 	return page
 }
 
@@ -56,12 +56,12 @@ func (page *Page) AddTimeline(t *element.Timeline) *Page {
 }
 
 func (page *Page) AddButtonAction(s string, btn *element.Button) *Page {
-	page.Actions = append(page.Actions, &element.Action{Instructions: element.NewText(s, element.WithTextTag(atom.P)), Button: btn})
+	page.Actions = append(page.Actions, &element.Action{Instructions: element.NewElement(atom.P, s), Button: btn})
 	return page
 }
 
 func (page *Page) AddInviteCodeAction(s string, code string) *Page {
-	page.Actions = append(page.Actions, &element.Action{Instructions: element.NewText(s, element.WithTextTag(atom.P)), InviteCode: code})
+	page.Actions = append(page.Actions, &element.Action{Instructions: element.NewElement(atom.P, s), InviteCode: code})
 	return page
 }
 
@@ -71,7 +71,7 @@ func (page *Page) AddOutro(ele element.Element) *Page {
 }
 
 func (page *Page) AddPlainOutro(s string) *Page {
-	page.Outros = append(page.Outros, element.NewText(s, element.WithTextTag(atom.P)))
+	page.Outros = append(page.Outros, element.NewElement(atom.P, s))
 	return page
 }
 

+ 3 - 4
render_test.go

@@ -3,7 +3,6 @@ package pgenr
 import (
 	"fmt"
 	"git.nspix.com/golang/pgenr/element"
-	"golang.org/x/net/html/atom"
 	"io/ioutil"
 	"testing"
 	"time"
@@ -23,9 +22,9 @@ func TestRender(t *testing.T) {
 	page.AddPlainOutro("Yours truly,")
 	page.AddPlainOutro("Hermes - https://google.com")
 	timeline := element.NewTimeline()
-	timeline.AddItem(time.Now().Add(-2*time.Minute), element.NewText("Event start", element.WithTextTag(atom.Div)))
-	timeline.AddItem(time.Now().Add(-1*time.Minute), element.NewText("Event end", element.WithTextTag(atom.Div)))
-	timeline.AddItem(time.Now().Add(time.Minute), element.NewText("Event closed", element.WithTextTag(atom.Div)))
+	timeline.AddItem(time.Now().Add(-2*time.Minute), element.NewText("Event start"))
+	timeline.AddItem(time.Now().Add(-1*time.Minute), element.NewText("Event end"))
+	timeline.AddItem(time.Now().Add(time.Minute), element.NewText("Event closed"))
 	page.AddTimeline(timeline)
 	table := element.NewTable(element.NewText("This year sale table", element.WithTextStyle(map[string]string{"font-size": "1.06rem", "font-weight": "550"})))
 	table.SetHead(element.NewText("Name"), element.NewText("Age"), element.NewText("Price"))