Created
February 20, 2024 15:10
-
-
Save yjunechoe/ac503750b31b77776d7ef6c424ae5c13 to your computer and use it in GitHub Desktop.
redirect <source> relpaths
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(xml2) | |
| x <- read_html(' | |
| <picture> | |
| <source media="(prefers-color-scheme: dark)" srcset="man/figures/README-/setup-io-dark.svg"> | |
| <img src="man/figures/README-/setup-io.svg" style="display: block; margin: auto;" /> | |
| </picture> | |
| ') | |
| img_src <- xml_find_all(x, ".//img[not(starts-with(@src, 'http'))]") | |
| source_srcset <- xml_find_all(x, ".//source[not(starts-with(@srcset, 'http'))]") | |
| urls <- list( | |
| img = xml_attr(img_src, "src"), | |
| source = xml_attr(source_srcset, "srcset") | |
| ) | |
| redirect_urls <- function(urls) { | |
| new_urls <- gsub("(^|/)vignettes/", "\\1articles/", urls, perl = TRUE) | |
| new_urls <- gsub("(^|/)man/figures/", "\\1reference/figures/", new_urls, perl = TRUE) | |
| new_urls | |
| } | |
| redirected <- map(urls, redirect_urls) | |
| purrr::walk2(img_src, redirected$img, ~ xml2::xml_set_attr(.x, "src", .y)) | |
| purrr::walk2(source_srcset, redirected$source, ~ xml2::xml_set_attr(.x, "srcset", .y)) |
yjunechoe
commented
Mar 12, 2024
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment