Debian Codegraph @

R/html_resource_copy.R

raw
1 # Given an HTML string, a library directory, and an output directory, copies the
2 # HTML's dependencies as appropriate and rewrites the HTML to refer to those
3 # files. If CSS files are among the dependencies, performs the same operation
4 # on each after copying.
5 copy_html_resources <- function(html_str, lib_dir, output_dir) {
6 resource_locator <- function(input_str, resource_copier) {
7 call_resource_attrs(input_str, function(node, att, src, idx) {
8 # copy the resource if needed
9 res_src <- resource_copier(node, att, src, idx)
10 if (is.null(res_src) || nchar(res_src) < 1)
11 return(NULL)
12
13 # if the resource is a CSS file, perform a similar rewriting of its
14 # content in the library directory
15 res_path <- file.path(output_dir, res_src)
16 if (identical(tolower(xfun::file_ext(res_path)), "css") &&
17 file.exists(res_path)) {
18 css_content <- copy_resources(
19 file_string(res_path), lib_dir, lib_dir, call_css_resource_attrs
20 )
21 write_utf8(css_content, res_path)
22 }
23 })
24 }
25 copy_resources(html_str, lib_dir, output_dir, resource_locator)
26 }
27
28 make_abs_path_safe <- function(path) {
29 if (xfun::is_windows()) {
30 sub("^.:(/|\\\\)*", "", path) # windows
31 } else {
32 sub("^[/]*", "", path) # *nix
33 }
34 }
35
36 copy_resources <- function(input_str, lib_dir, output_dir, resource_locator) {
37 # ensure the lib directory exists
38 dir.create(lib_dir, recursive = TRUE, showWarnings = FALSE)
39 relative_lib <- normalized_relative_to(output_dir, lib_dir)
40
41 res_replacements <- c()
42
43 resource_copier <- function(node, att, src, idx) {
44 in_file <- utils::URLdecode(src)
45
46 # only process the file if (a) it isn't already in the library, and (b)
47 # it exists on the local file system (this also excludes external
48 # resources such as "http://foo/bar.png")
49 if (length(in_file) &&
50 substr(src, 1, nchar(lib_dir) + 1) != file.path(relative_lib, "") &&
51 file.exists(in_file)) {
52
53 # check to see if it's already in the library (by absolute path)
54 res_src <- normalized_relative_to(lib_dir, in_file)
55 if (same_path(res_src, in_file, must_work = FALSE)) {
56 # not inside the library, copy it there
57 target_dir <- if (dirname(in_file) == ".")
58 lib_dir
59 else
60 file.path(lib_dir, make_abs_path_safe(dirname(in_file)))
61 dir.create(target_dir, recursive = TRUE, showWarnings = FALSE)
62 target_res_file <- file.path(target_dir, basename(in_file))
63
64 # copy the file to the library
65 if (!file.exists(target_res_file)) {
66 file.copy(in_file, target_res_file)
67 }
68
69 if (identical(lib_dir, output_dir))
70 res_src <- in_file
71 else
72 res_src <- file.path(relative_lib, make_abs_path_safe(src))
73 } else {
74 # inside the library, fix up the URL
75 res_src <- file.path(relative_lib, res_src)
76 }
77
78 # replace the reference in the document and adjust the offset
79 if (!identical(src, res_src)) {
80 res_replacements <<- c(res_replacements, list(list(
81 pos = idx,
82 len = nchar(src),
83 text = res_src)))
84 }
85
86 res_src
87 }
88 }
89
90 # parse the HTML and copy the resources found
91 resource_locator(input_str, resource_copier)
92
93 # rewrite the HTML to refer to the copied resources
94 if (length(res_replacements) > 0) {
95 ch_pos <- 1
96 output_str <- ""
97
98 # we do all replacements in bytes for performance reasons--mark the
99 # output with byte encoding temporarily
100 prev_encoding <- Encoding(input_str)
101 Encoding(input_str) <- "bytes"
102 Encoding(output_str) <- "bytes"
103
104 for (res_rep in seq_along(res_replacements)) {
105 rep <- res_replacements[[res_rep]]
106
107 # the text from the last replacement to the current one
108 before <- substr(input_str, ch_pos, rep$pos - 1)
109 ch_pos <- rep$pos + rep$len
110
111 # the text from the current replacement to the end of the output,
112 # if applicable
113 after <- if (res_rep == length(res_replacements)) {
114 substring(input_str, ch_pos, last = nchar(input_str, type = "bytes"))
115 } else {
116 ""
117 }
118
119 # compose the next segment of the output from the text between
120 # replacements and the current replacement text
121 output_str <- paste(output_str, before, rep$text, after,
122 sep = "")
123 }
124 input_str <- output_str
125 Encoding(input_str) <- prev_encoding
126 }
127
128 input_str
129 }