-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlwriter.go
71 lines (63 loc) · 1.26 KB
/
htmlwriter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package web
import (
"io"
"strings"
"github.com/gregoryv/nexus"
)
func NewHtmlWriter(w io.Writer) *HtmlWriter {
p, err := nexus.NewPrinter(w)
return &HtmlWriter{
Printer: p,
err: err,
}
}
type HtmlWriter struct {
*nexus.Printer
err *error
}
func (p *HtmlWriter) WriteHtml(t interface{}) (int64, error) {
p.writeElement(t)
return p.Written, *p.err
}
func (p *HtmlWriter) writeElement(t interface{}) {
switch t := t.(type) {
case *Element:
p.open(t)
for _, child := range t.Children {
p.writeElement(child)
}
p.close(t)
case io.Reader:
io.Copy(p, t)
default:
p.Printf("%v", t)
}
}
func (p *HtmlWriter) open(t *Element) {
p.Print("<", t.Name)
for _, Attributes := range t.Attributes {
p.Print(" ", Attributes.String())
}
if !t.simple {
p.Print(">")
}
if strings.Index("html body head article section ol ul dl", t.Name) > -1 {
p.Println()
}
}
func (p *HtmlWriter) close(t *Element) {
if t.simple {
p.Print("/>")
if strings.Index("meta link", t.Name) > -1 {
p.Println()
}
return
}
if strings.Index("html body head article section style script", t.Name) > -1 {
p.Println()
}
p.Print("</", t.Name, ">")
if strings.Index("p h1 h2 h3 h4 h5 h6 li ul ol dt dd head title style script", t.Name) > -1 {
p.Println()
}
}