-
Notifications
You must be signed in to change notification settings - Fork 10
/
checkLinks.R
46 lines (37 loc) · 1.04 KB
/
checkLinks.R
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
library(XML)
library(RCurl)
checkLinks =
function(u, doc = htmlParse(u), links = getHTMLLinks(doc), recursive = TRUE, collector = new.env(parent = emptyenv()))
{
# browser()
links = table(links)
todo = setdiff(names(links), ls(envir = collector))
ans = sapply(todo, urlExists, docName(doc), USE.NAMES = FALSE)
if(recursive) {
browser()
if(!is.logical(recursive))
recursive = recursive - 1
list2env(as.list(a), collector)
todo = setdiff(names(ans)[ans], ls(envir = collector))
sapply(todo, checkLinks, recursive = recursive, collector = collector)
collector
} else
ans
}
urlExists =
function(u, base = "./foo")
{
u = XML:::trim(u)
if(grepl("^(http|ftp)", u))
return(structure(url.exists(u), names = u))
if(grepl("^mailto:", u))
return(structure(TRUE, names = u))
u = mkRelative(u, base)
structure(file.exists(u), names = u)
}
mkRelative =
function(file, baseDoc)
{
ans = getRelativeURL(file, baseDoc)
gsub("/\\./", "/", ans)
}