| 1 |
# processes an HTML resource, given a regular expression that locates |
| 2 |
# instances of that resource |
| 3 |
process_html_res <- function(html, reg, processor) { |
| 4 |
html <- one_string(html) |
| 5 |
m <- gregexpr(reg, html, perl = TRUE, ignore.case = TRUE) |
| 6 |
regmatches(html, m) <- lapply(regmatches(html, m), function(img_src) { |
| 7 |
src <- sub(reg, '\\1', img_src, ignore.case = TRUE) |
| 8 |
vapply( |
| 9 |
seq_along(img_src), |
| 10 |
function(i) processor(img_src[[i]], src[[i]]), |
| 11 |
character(1) |
| 12 |
) |
| 13 |
}) |
| 14 |
strsplit(html, "\n", fixed = TRUE)[[1]] |
| 15 |
} |
| 16 |
|
| 17 |
process_images <- function(html, processor) { |
| 18 |
process_html_res(html, "<\\s*img\\s+.*?src\\s*=\\s*[\"']([^\"']+)[\"']", processor) |
| 19 |
} |
| 20 |
|
| 21 |
base64_encode_images <- function(html) { |
| 22 |
encode <- function(img_src, src) { |
| 23 |
in_file <- utils::URLdecode(src) |
| 24 |
if (length(in_file) && file.exists(in_file)) { |
| 25 |
img_src <- sub(src, xfun::base64_uri(in_file), img_src, fixed = TRUE) |
| 26 |
} |
| 27 |
img_src |
| 28 |
} |
| 29 |
html <- process_images(html, encode) |
| 30 |
process_html_res(html, "<[^>]*style=\"[^\"]*url\\(([^\\)]+)\\)", encode) |
| 31 |
} |