Skip to content

Commit

Permalink
reader
Browse files Browse the repository at this point in the history
  • Loading branch information
Gardego5 committed Aug 4, 2024
1 parent 947d619 commit d60363f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
13 changes: 13 additions & 0 deletions fragment.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type HTMLElement interface {
element()
String() string
Bytes() []byte
Reader() io.Reader
}

type Fragment []HTMLElement
Expand Down Expand Up @@ -42,6 +43,18 @@ func (frag Fragment) WriteTo(w io.Writer) (int64, error) {

return n, nil
}
func (frag Fragment) Reader() io.Reader {
r, w := io.Pipe()
go func(w *io.PipeWriter) {
_, err := frag.WriteTo(w)
if err != nil {
w.CloseWithError(err)
} else {
w.Close()
}
}(w)
return r
}

func render(w io.Writer, child any) (int64, error) {
switch child := child.(type) {
Expand Down
6 changes: 5 additions & 1 deletion literals.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package html

import "io"
import (
"io"
"strings"
)

type literal string

Expand All @@ -9,6 +12,7 @@ const DOCTYPE literal = "<!DOCTYPE html>"
func (lit literal) element() {}
func (lit literal) String() string { return string(lit) }
func (lit literal) Bytes() []byte { return []byte(lit) }
func (lit literal) Reader() io.Reader { return strings.NewReader(string(lit)) }
func (lit literal) WriteTo(w io.Writer) (int64, error) {
n, err := w.Write([]byte(lit))
return int64(n), err
Expand Down
7 changes: 4 additions & 3 deletions text.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package html

import (
"io"
"strings"
)

type text []any
Expand All @@ -13,6 +14,6 @@ func (text PreEscaped) WriteTo(w io.Writer) (int64, error) {
nn, err := w.Write([]byte(text))
return int64(nn), err
}
func (text PreEscaped) String() string {
return string(text)
}
func (text PreEscaped) String() string { return string(text) }
func (text PreEscaped) Bytes() []byte { return []byte(text) }
func (text PreEscaped) Reader() io.Reader { return strings.NewReader(string(text)) }

0 comments on commit d60363f

Please sign in to comment.