/
read.go
47 lines (42 loc) · 1.16 KB
/
read.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
package htmldiff
import (
"io"
"github.com/andybalholm/cascadia"
"golang.org/x/net/html"
)
// ReadHeader reads html from 'r' reader, extract header,
// and writes the header string to the 'w' writer.
func ReadHeader(r io.Reader, w io.Writer) error {
doc, err := html.Parse(r)
if err != nil {
return err
}
h := cascadia.MustCompile("head").MatchFirst(doc)
return html.Render(w, h)
}
// ReadBody reads html from 'r' reader, extract body,
// and writes the body string to the 'w' writer.
func ReadBody(r io.Reader, w io.Writer) error {
doc, err := html.Parse(r)
if err != nil {
return err
}
h := cascadia.MustCompile("body").MatchFirst(doc)
return html.Render(w, h)
}
// ReadHeaderAndBody reads html from 'r' reader, extract header and body.
// Then, this function writes the header string to the 'wh' writer and
// writes the body string to the 'wb' writer.
func ReadHeaderAndBody(r io.Reader, wh, wb io.Writer) error {
doc, err := html.Parse(r)
if err != nil {
return err
}
h := cascadia.MustCompile("head").MatchFirst(doc)
err = html.Render(wh, h)
if err != nil {
return err
}
h = cascadia.MustCompile("body").MatchFirst(doc)
return html.Render(wb, h)
}