-
-
Notifications
You must be signed in to change notification settings - Fork 978
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
it's unlikely for an img src to contain line breaks, so it shouldn't be necessary to one_string(html) beforehand and split it afterwards
- Loading branch information
Showing
1 changed file
with
7 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,30 @@ | ||
# processes an HTML resource, given a regular expression that locates | ||
# instances of that resource | ||
process_html_res <- function(html, reg, processor) { | ||
html <- one_string(html) | ||
process_img_src <- function(img_src) { | ||
m <- gregexpr(reg, html, perl = TRUE, ignore.case = TRUE) | ||
regmatches(html, m) <- lapply(regmatches(html, m), function(img_src) { | ||
src <- sub(reg, '\\1', img_src, ignore.case = TRUE) | ||
vapply( | ||
seq_along(img_src), | ||
function(i) processor(img_src[[i]], src[[i]]), | ||
character(1) | ||
) | ||
} | ||
m <- gregexpr(reg, html, perl = TRUE, ignore.case = TRUE) | ||
regmatches(html, m) <- lapply(regmatches(html, m), process_img_src) | ||
|
||
strsplit(html, "\n", fixed = TRUE)[[1]] | ||
}) | ||
html | ||
} | ||
|
||
process_images <- function(html, processor) { | ||
process_html_res(html, "<\\s*img\\s+.*?src\\s*=\\s*[\"']([^\"']+)[\"']", processor) | ||
} | ||
|
||
base64_encode_images <- function(html) { | ||
base64_encode_img <- function(img_src, src) { | ||
encode <- function(img_src, src) { | ||
in_file <- utils::URLdecode(src) | ||
if (length(in_file) && file.exists(in_file)) { | ||
img_src <- sub(src, xfun::base64_uri(in_file), img_src, fixed = TRUE) | ||
} | ||
img_src | ||
} | ||
html <- process_images(html, base64_encode_img) | ||
process_html_res(html, "<[^>]*style=\"[^\"]*url\\(([^\\)]+)\\)", base64_encode_img) | ||
html <- process_images(html, encode) | ||
process_html_res(html, "<[^>]*style=\"[^\"]*url\\(([^\\)]+)\\)", encode) | ||
} |
0951a2f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yihui I believe this breaks some edge cases where the HTML contains
<img
andsrc=
on separate linesFor example, it prevents some absolute links to be made relative the
html_document_base
processing.Took me a while to come to here, but I believe this is why this
one_string
calls was there - to avoid dealing with new lines like this.I think what is causing the issue is the pandoc 2.17 change in
--wrap=auto
being the default for HTML which creates this wrapping whensrc=
is very very long.It happens with
knitr::include_graphics()
usage or any other that will write the![](very/long/path.qmd)
So either we do something about
--wrap
, or we revert this change not doing aone_string
0951a2f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry about the oversight!
I'm okay with either solution. The latter may be safer, though. Do you want me to do it or you can go ahead and do it? I think it should be two lines of code.
0951a2f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do it. By the way, this goes with a second issue in
knitr::include_graphics()
where the absolute path is not computed usingoutput.dir=
from rmarkdown but usingknitr
output.dir
option which is different.fig_path
will be computed to be output directory as set by R Markdown, but if someone like magick or gganimate creates image infig_path
and then attached usinginclude_graphics()
- the wrong relative path is computed.I'll try to open an issue but this is related to yihui/knitr#2171 IMO. We can discuss live too