-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.go
111 lines (87 loc) · 2.36 KB
/
rules.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
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
package amphtml
import (
"image"
"io"
"net/url"
"strconv"
"github.com/favclip/html2html"
)
var AMPTags AMPTagList
type AMPTagList []*AMPTag
type AMPTag struct {
SrcTag string
DestTag string
Modifier TagModifier
}
type TagModifier func(conv *Converter, ampTag *AMPTag, tag html2html.Tag) (html2html.Tag, error)
type FileFetcher func(targetURL *url.URL) (io.ReadCloser, error)
func (list AMPTagList) isAMPTag(tag html2html.Tag) bool {
for _, ampTag := range list {
if tag.Name() == ampTag.DestTag {
return true
}
}
return false
}
type AMPImageStatsFetcher interface {
ImageSize(imageURL *url.URL) (*url.URL, int, int, error) // modifiedURL, width, height
ImageSrcSetAttr(imageURL *url.URL) (string, error) // return value uses for <amp-img src=... srcset="{{Here!}}">
}
func init() {
AMPTags = append(AMPTags, &Tag{SrcTag: "img", DestTag: "amp-img", Modifier: ampImageModifier})
// TODO
// video
// audio
// iframe
}
type ampImageStatsFetcherImpl struct {
fileFetcher FileFetcher
}
func (a *ampImageStatsFetcherImpl) ImageSize(imageURL *url.URL) (*url.URL, int, int, error) {
data, err := a.fileFetcher(imageURL)
if err != nil {
return imageURL, 0, 0, err
}
defer data.Close()
config, _, err := image.DecodeConfig(data)
if err != nil {
return imageURL, 0, 0, err
}
return imageURL, config.Width, config.Height, nil
}
func (m *ampImageStatsFetcherImpl) ImageSrcSetAttr(imageURL *url.URL) (string, error) {
return "", nil
}
func ampImageModifier(conv *Converter, ampTag *AMPTag, tag html2html.Tag) (html2html.Tag, error) {
altTag := html2html.CreateElement(ampTag.DestTag)
for _, attr := range tag.Attrs() {
switch attr.Key {
case "alt":
altTag.AddAttr(attr.Key, attr.Value)
case "src":
imgURL, err := url.Parse(attr.Value)
if err != nil {
altTag.AddAttr(attr.Key, attr.Value)
continue
}
imgURL, width, height, err := conv.ampImageStatsFetcher.ImageSize(imgURL)
if err != nil {
return nil, err
}
altTag.AddAttr("src", imgURL.String())
altTag.AddAttr("width", strconv.Itoa(width))
altTag.AddAttr("height", strconv.Itoa(height))
altTag.AddAttr("layout", "responsive")
srcset, err := conv.ampImageStatsFetcher.ImageSrcSetAttr(imgURL)
if err != nil {
return nil, err
}
if srcset != "" {
altTag.AddAttr("srcset", srcset)
}
default:
// ignore
}
}
return altTag, nil
}