forked from JohannesKaufmann/html-to-markdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage.go
More file actions
144 lines (124 loc) · 3 KB
/
Copy pathimage.go
File metadata and controls
144 lines (124 loc) · 3 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"net/http"
"net/url"
"strings"
"sync"
"golang.org/x/net/html"
// "golang.org/x/image/draw"
)
var imageCache = sync.Map{}
func isRelative(u *url.URL) bool {
// A URL is absolute if it has both a Scheme and a Host.
return u.Scheme == "" && u.Host == ""
}
func parseTags(htmlSource string, baseURL *url.URL, downloadImages bool, linkURLs, linkDescs *[]string, title *string) error {
imageURLs := []string{}
doc, err := html.Parse(strings.NewReader(htmlSource))
if err != nil {
return err
}
var visitNode func(*html.Node)
visitNode = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "head" {
for c1 := range n.ChildNodes() {
if c1.Type == html.ElementNode && c1.Data == "title" {
for c2 := range c1.ChildNodes() {
if c2.Type == html.TextNode {
*title = c2.Data
break
}
}
}
}
} else if n.Type == html.ElementNode && n.Data == "img" {
for _, attr := range n.Attr {
if attr.Key == "src" {
// src could be "image1.jpg" or "/images/image2.png" or full url
u, err := url.Parse(attr.Val)
if err != nil {
return
}
if isRelative(u) {
u = baseURL.ResolveReference(u)
}
fullURL := u.String()
imageURLs = append(imageURLs, fullURL)
}
}
} else if n.Type == html.ElementNode && n.Data == "a" {
for _, attr := range n.Attr {
if attr.Key == "href" {
*linkURLs = append(*linkURLs, attr.Val)
}
}
txtData := ""
for v := range n.Descendants() {
if v.Type == html.TextNode {
txtData = txtData + v.Data
}
}
*linkDescs = append(*linkDescs, txtData)
}
// Recursively visit child and sibling nodes
for c := n.FirstChild; c != nil; c = c.NextSibling {
visitNode(c)
}
}
visitNode(doc)
if downloadImages {
var wg sync.WaitGroup
for _, fullURL := range imageURLs {
if _, exists := imageCache.Load(fullURL); !exists {
wg.Go(func() {
resp, err := http.Get(fullURL)
if err != nil {
return
}
defer resp.Body.Close()
m, _, err := image.Decode(resp.Body)
if err != nil {
return
}
imageCache.Store(fullURL, m)
})
}
}
wg.Wait()
}
return nil
}
func downloadImage(src string, terminalWidth int) (image.Image, error) {
val, ok := imageCache.Load(src)
if ok {
return val.(image.Image), nil
}
resp, err := http.Get(src)
if err != nil {
return nil, err
}
defer resp.Body.Close()
m, _, err := image.Decode(resp.Body)
if err != nil {
return nil, err
}
return m, nil
// // Scale image
// b := m.Bounds()
// fmt.Println(b)
// ar := float64(b.Max.X-b.Min.X) / float64(b.Max.Y-b.Min.Y) // width / height
// fmt.Println(ar)
// newWidth := (terminalWidth)
// newHeight := int(float64(terminalWidth) / ar)
//
// dst := image.NewNRGBA(image.Rect(0, 0, newWidth, newHeight))
// fmt.Println(dst.Rect)
// draw.CatmullRom.Scale(dst, dst.Rect, m, m.Bounds(), draw.Over, nil)
// fmt.Println(dst.Rect)
//
// return dst, nil
}