Skip to content

Instantly share code, notes, and snippets.

@yjunechoe
Created February 20, 2024 15:10
Show Gist options
  • Save yjunechoe/ac503750b31b77776d7ef6c424ae5c13 to your computer and use it in GitHub Desktop.
Save yjunechoe/ac503750b31b77776d7ef6c424ae5c13 to your computer and use it in GitHub Desktop.
redirect <source> relpaths
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
Copy link
Author

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;"></source>
</picture>
<img src="hi.png"/>
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="man/figures/README-/szzzzk.svg"/>
</picture>
')
x

tweak_source_srcref <- function(html) {
  sources <- xml_find_all(x, "//picture//source[not(starts-with(@srcset, 'http'))]")
  urls <- xml_attr(sources, "srcset")
  new_urls <- gsub("(^|/)vignettes/", "\\1articles/", urls, perl = TRUE)
  new_urls <- gsub("(^|/)man/figures/", "\\1reference/figures/", new_urls, perl = TRUE)
  purrr::map2(sources, new_urls, ~ xml2::xml_set_attr(.x, "src", .y))
}
cat(as.character(x))
cat(as.character(tweak_source_srcref(x)))
# ----

imgs <- xml_find_all(x, ".//img[not(starts-with(@src, 'http'))]")
sources <- xml_find_all(x, "//picture//source[not(starts-with(@srcset, 'http'))]")

fix_links <- function(x) {
  x <- gsub("(^|/)vignettes/", "\\1articles/", x, perl = TRUE)
  x <- gsub("(^|/)man/figures/", "\\1reference/figures/", x, perl = TRUE)
  x
}
new_src <- fix_links(xml_attr(imgs, "src"))
new_srcset <- fix_links(xml_attr(sources, "srcset"))

purrr::walk2(imgs, new_src, ~ xml2::xml_set_attr(.x, "src", .y))
purrr::walk2(sources, new_srcset, ~ xml2::xml_set_attr(.x, "srcset", .y))

cat(as.character(x))

invisible()

# "//picture//.//preceding-sibling::source[not(starts-with(@srcset, 'http'))]"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment