Skip to content

Commit 6743ffc

Browse files
committed
ctan_mirrors() returns more detailed information about CTAN mirrors; the link https://ctan.org/mirrors/mirmon appears to be broken at the moment, so scrape from https://ctan.org/mirrors/ instead
this was written for #450
1 parent 239eb91 commit 6743ffc

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: tinytex
22
Type: Package
33
Title: Helper Functions to Install and Maintain TeX Live, and Compile LaTeX Documents
4-
Version: 0.53.2
4+
Version: 0.53.3
55
Authors@R: c(
66
person("Yihui", "Xie", role = c("aut", "cre", "cph"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
77
person(given = "Posit Software, PBC", role = c("cph", "fnd")),

R/install.R

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,39 @@ auto_repo = function() {
214214

215215
# retrieve all CTAN (https) mirrors
216216
ctan_mirrors = function() {
217-
x = readLines('https://ctan.org/mirrors/mirmon')
218-
u = xfun::grep_sub('.*<TD ALIGN=RIGHT><A HREF="(https://[^"]+)".*', '\\1', x)
219-
xfun::raw_string(u)
217+
html = xfun::file_string('https://ctan.org/mirrors/')
218+
r = function(i) sprintf('^(.*>)?\\s*([^<]+)</h%d>\\s*(.*)$', i)
219+
res = unlist(lapply(unlist(strsplit(html, '<h2[^>]*>')), function(x) {
220+
x = unlist(strsplit(x, '<h3[^>]*>'))
221+
if (length(x) < 2 || !grepl('</h2>', x[1])) return()
222+
r2 = r(2)
223+
continent = gsub(r2, '\\2', x[1])
224+
x[1] = gsub(r2, '\\3', x[1])
225+
x = x[!grepl('^\\s*$', x)]
226+
r3 = r(3)
227+
if (!grepl(r3, x[1])) return()
228+
country = gsub(r3, '\\2', x)
229+
x = gsub(r3, '\\3', x)
230+
r4 = r(4)
231+
x = lapply(x, function(z) {
232+
z = unlist(strsplit(z, '<h4[^>]*>'))
233+
m = regexec('<a href="(https://[^"]+)"[^>]*>https</a>', z)
234+
link = unlist(lapply(regmatches(z, m), `[`, 2))
235+
names(link) = gsub(r4, '\\2', z)
236+
link[!is.na(link)]
237+
})
238+
structure(list(structure(x, names = country)), names = continent)
239+
}))
240+
nm = lapply(strsplit(names(res), '.', fixed = TRUE), function(x) {
241+
x3 = paste(x[-(1:2)], collapse = '.')
242+
r5 = '.*\\(|\\).*'
243+
x3 = if (grepl(r5, x3)) gsub(r5, '', x3) else ''
244+
c(x[1], x[2], x3)
245+
})
246+
nm = do.call(rbind, nm)
247+
res = cbind(nm, unname(res))
248+
colnames(res) = c('Continent', 'Country/Region', 'City', 'URL')
249+
as.data.frame(res)
220250
}
221251

222252
# use %APPDATA%/TinyTeX if it exists or doesn't contain spaces or non-ASCII

0 commit comments

Comments
 (0)