-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountTextImagesInHTML.go
54 lines (46 loc) · 1.17 KB
/
CountTextImagesInHTML.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
package main
import (
"bytes"
"fmt"
"golang.org/x/net/html"
"strings"
)
var raw = `
<!DOCTYPE html>
<html>
<body>
<h1>Hello, World!</h1>
<image src="https://www.w3schools.com/images/w3schools_green.jpg" alt = "hello"/>
<p>This is a paragraph.</p>
<h3>Program to count images and text.</h3>
<script>
document.getElementById("demo").innerHTML = "Hello JavaScript!";
</script>
</body>
</html>
`
func visit(c *html.Node, words, images *int) {
if c.Type == html.TextNode {
*words += len(strings.Fields(c.Data))
} else if c.Type == html.ElementNode && c.Data == "img" {
*images++
} else if c.Type == html.ElementNode && c.Data == "script" {
return // this will ignore all javascript
}
for n := c.FirstChild; n != nil; n = n.NextSibling {
visit(n, words, images)
}
}
func countWordsAndImages(doc *html.Node) (int, int) {
var countWords, countImages int
visit(doc, &countWords, &countImages)
return countWords, countImages
}
func main() {
doc, err := html.Parse(bytes.NewReader([]byte(raw)))
if err != nil {
fmt.Printf("Error parsing html %v", err)
}
word, pics := countWordsAndImages(doc)
fmt.Printf("Words are %d and images are %d", word, pics)
}