-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.go
70 lines (66 loc) · 1.78 KB
/
extract.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
package walker
import (
"encoding/json"
"fmt"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/foomo/walker/vo"
)
func extractTrimText(txt string) string {
return strings.Trim(txt, " \n")
}
// ExtractStructure extract the most important semantic elements out of a html document
func ExtractStructure(doc *goquery.Document) (s vo.Structure, err error) {
description, _ := doc.Find("meta[name=description]").First().Attr("content")
robots, _ := doc.Find("meta[name=robots]").First().Attr("content")
s = vo.Structure{
Title: extractTrimText(doc.Find("title").First().Text()),
Description: extractTrimText(description),
Robots: extractTrimText(robots),
}
doc.Find("link[rel=prev], link[rel=next], link[rel=canonical]").Each(func(i int, sel *goquery.Selection) {
attrRelVal, attrRelOK := sel.Attr("rel")
attrHref, attrHrefOK := sel.Attr("href")
if attrRelOK && attrHrefOK {
switch attrRelVal {
case "canonical":
s.Canonical = extractTrimText(attrHref)
case "prev":
s.LinkPrev = extractTrimText(attrHref)
case "next":
s.LinkNext = extractTrimText(attrHref)
}
}
})
doc.Find("script[type=\"application/ld+json\"]").Each(func(i int, sel *goquery.Selection) {
ld := &vo.LinkedData{}
errDoc := json.Unmarshal([]byte(sel.Text()), &ld)
if errDoc != nil {
fmt.Println("json crap", errDoc)
return
}
s.LinkedData = append(s.LinkedData, *ld)
})
doc.Find("h1, h2, h3, h4, h5, h6").Each(func(i int, sel *goquery.Selection) {
level := 0
switch sel.Get(0).Data {
case "h1":
level = 1
case "h2":
level = 2
case "h3":
level = 3
case "h4":
level = 4
case "h5":
level = 5
case "h6":
level = 6
}
s.Headings = append(s.Headings, vo.Heading{
Level: level,
Text: extractTrimText(sel.Text()),
})
})
return
}