-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Signed-off-by: Adrien Duermael <adrien@duermael.com>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"golang.org/x/net/html" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
var countLinks = 0 | ||
var countImages = 0 | ||
|
||
// 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("/usr/src/app/allvbuild", func(path string, info os.FileInfo, err error) error { | ||
|
||
relPath := strings.TrimPrefix(path, "/usr/src/app/allvbuild") | ||
|
||
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) | ||
if err != nil { | ||
t.Error(err.Error(), "-", relPath) | ||
} | ||
return nil | ||
}) | ||
|
||
fmt.Println("found", count, "html files") | ||
fmt.Println("found", countLinks, "links") | ||
fmt.Println("found", countImages, "images") | ||
} | ||
|
||
// testURLs tests if we're not using absolute paths for URLs | ||
// when pointing to local pages. | ||
func testURLs(htmlBytes []byte) error { | ||
|
||
reader := bytes.NewReader(htmlBytes) | ||
|
||
z := html.NewTokenizer(reader) | ||
|
||
for { | ||
tt := z.Next() | ||
|
||
switch tt { | ||
case html.ErrorToken: | ||
// End of the document, we're done | ||
return nil | ||
case html.StartTagToken: | ||
t := z.Token() | ||
// check tag types | ||
switch t.Data { | ||
case "a": | ||
countLinks++ | ||
ok, _ := getHref(t) | ||
// skip, it may just be an anchor | ||
if !ok { | ||
break | ||
} | ||
|
||
case "img": | ||
countImages++ | ||
ok, _ := getSrc(t) | ||
if !ok { | ||
return errors.New("img with no src: " + t.String()) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// _, md, err := frontparser.ParseFrontmatterAndContent(mdBytes) | ||
// if err != nil { | ||
// return err | ||
// } | ||
|
||
// regularExpression, err := regexp.Compile(`\[[^\]]+\]\(([^\)]+)\)`) | ||
// if err != nil { | ||
// return err | ||
// } | ||
|
||
// submatches := regularExpression.FindAllStringSubmatch(string(md), -1) | ||
|
||
// for _, submatch := range submatches { | ||
// if strings.Contains(submatch[1], "docs.docker.com") { | ||
// return errors.New("found absolute link (" + strings.TrimSpace(submatch[1]) + ")") | ||
// } | ||
// } | ||
|
||
return nil | ||
} | ||
|
||
// 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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/gdevillele/frontparser" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
) | ||
|
||
// isPublishedMarkdown returns wether a file is a published markdown or not | ||
// as a convenience it also returns the markdown bytes to avoid reading files twice | ||
func isPublishedMarkdown(path string) (bool, []byte, error) { | ||
if strings.HasSuffix(path, ".md") { | ||
fileBytes, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return false, nil, err | ||
} | ||
if frontparser.HasFrontmatterHeader(fileBytes) { | ||
fm, _, err := frontparser.ParseFrontmatterAndContent(fileBytes) | ||
if err != nil { | ||
return false, nil, err | ||
} | ||
// skip markdowns that are not published | ||
if published, exists := fm["published"]; exists { | ||
if publishedBool, ok := published.(bool); ok { | ||
if publishedBool { | ||
// file is markdown, has frontmatter and is published | ||
return true, fileBytes, nil | ||
} | ||
} | ||
} else { | ||
// if "published" field is missing, it means published == true | ||
return true, fileBytes, nil | ||
} | ||
} | ||
} | ||
return false, nil, nil | ||
} | ||
|
||
// isHTML returns wether a file is an html file or not | ||
// as a convenience it also returns the markdown bytes to avoid reading files twice | ||
func isHTML(path string) (bool, []byte, error) { | ||
if strings.HasSuffix(path, ".html") { | ||
fileBytes, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return false, nil, err | ||
} | ||
return true, fileBytes, nil | ||
} | ||
return false, nil, nil | ||
} | ||
|
||
// fileExists returns true if the given file exists | ||
func fileExists(name string) bool { | ||
_, err := os.Stat(name) | ||
return err == nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.