forked from docker/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_test.go
More file actions
204 lines (172 loc) · 4.5 KB
/
Copy pathhtml_test.go
File metadata and controls
204 lines (172 loc) · 4.5 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"bytes"
"errors"
"fmt"
"golang.org/x/net/html"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"testing"
)
var countLinks = 0
var countImages = 0
var htmlContentRootPath = "/usr/src/app/allvbuild"
// TestURLs tests if we're not using absolute paths for URLs
// when pointing to local pages.
func TestURLs(t *testing.T) {
count := 0
filepath.Walk(htmlContentRootPath, func(path string, info os.FileInfo, err error) error {
relPath := strings.TrimPrefix(path, htmlContentRootPath)
isArchive, err := regexp.MatchString(`^/v[0-9]+\.[0-9]+/.*`, relPath)
if err != nil {
t.Error(err.Error(), "-", relPath)
}
// skip archives for now, only test URLs in current version
// TODO: test archives
if isArchive {
return nil
}
if err != nil {
t.Error(err.Error(), "-", relPath)
}
b, htmlBytes, err := isHTML(path)
if err != nil {
t.Error(err.Error(), "-", relPath)
}
// don't check non-html files
if b == false {
return nil
}
count++
err = testURLs(htmlBytes, path)
if err != nil {
t.Error(relPath + err.Error())
}
return nil
})
fmt.Println("found", count, "html files (excluding archives)")
fmt.Println("found", countLinks, "links (excluding archives)")
fmt.Println("found", countImages, "images (excluding archives)")
}
// testURLs tests if we're not using absolute paths for URLs
// when pointing to local pages.
func testURLs(htmlBytes []byte, htmlPath string) error {
reader := bytes.NewReader(htmlBytes)
z := html.NewTokenizer(reader)
urlErrors := ""
// fmt.Println("urlErrors:", urlErrors)
done := false
for !done {
tt := z.Next()
switch tt {
case html.ErrorToken:
// End of the document, we're done
done = true
case html.StartTagToken:
t := z.Token()
urlStr := ""
// check tag types
switch t.Data {
case "a":
countLinks++
ok, href := getHref(t)
// skip, it may just be an anchor
if !ok {
break
}
urlStr = href
case "img":
countImages++
ok, src := getSrc(t)
if !ok {
urlErrors += "\nimg with no src: " + t.String()
break
}
urlStr = src
}
// there's an url to test!
if urlStr != "" {
u, err := url.Parse(urlStr)
if err != nil {
urlErrors += "\ncan't parse url: " + t.String()
break
// return errors.New("can't parse url: " + t.String())
}
// test with github.com
if u.Scheme != "" && u.Host == "docs.docker.com" {
urlErrors += "\nabsolute: " + t.String()
break
}
// relative link
if u.Scheme == "" {
resourcePath := ""
resourcePathIsAbs := false
if filepath.IsAbs(u.Path) {
resourcePath = filepath.Join(htmlContentRootPath, mdToHtmlPath(u.Path))
resourcePathIsAbs = true
} else {
resourcePath = filepath.Join(filepath.Dir(htmlPath), mdToHtmlPath(u.Path))
}
if _, err := os.Stat(resourcePath); os.IsNotExist(err) {
fail := true
// index.html could mean there's a corresponding index.md meaning built the correct path
// but Jekyll actually creates index.html files for all md files.
// foo.md -> foo/index.html
// it does this to prettify urls, content of foo.md would then be rendered here:
// http://domain.com/foo/ (instead of http://domain.com/foo.html)
// so if there's an error, let's see if index.md exists, otherwise retry from parent folder
// (only if the resource path is not absolute)
if !resourcePathIsAbs && filepath.Base(htmlPath) == "index.html" {
// retry from parent folder
resourcePath = filepath.Join(filepath.Dir(htmlPath), "..", mdToHtmlPath(u.Path))
if _, err := os.Stat(resourcePath); err == nil {
fail = false
}
}
if fail {
urlErrors += "\nbroken: " + t.String()
break
}
}
}
}
}
}
// fmt.Println("urlErrors:", urlErrors)
if urlErrors != "" {
return errors.New(urlErrors)
}
return nil
}
func mdToHtmlPath(mdPath string) string {
if strings.HasSuffix(mdPath, ".md") == false {
// file is not a markdown, don't change anything
return mdPath
}
if strings.HasSuffix(mdPath, "index.md") {
return strings.TrimSuffix(mdPath, "md") + "html"
}
return strings.TrimSuffix(mdPath, ".md") + "/index.html"
}
// helpers
func getHref(t html.Token) (ok bool, href string) {
for _, a := range t.Attr {
if a.Key == "href" {
href = a.Val
ok = true
}
}
return
}
func getSrc(t html.Token) (ok bool, src string) {
for _, a := range t.Attr {
if a.Key == "src" {
src = a.Val
ok = true
}
}
return
}