-
Notifications
You must be signed in to change notification settings - Fork 2
/
file-tools.R
323 lines (311 loc) · 11.5 KB
/
file-tools.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#' @title comment filter for latex lines data
#'
#' @description
#' removes commented latex lines to avoid wrong reference data
#' @param data block of data
#' @keywords internal
#' @return filtered data
#' @noRd
comment_filter <- function(data) {
comment_break_points <- which(grepl("^%", data))
for (pos in comment_break_points) {
data[pos] <- ""
}
comment_break_points <- which(grepl("^%%", data))
for (pos in comment_break_points) {
data[pos] <- ""
}
return(data[nzchar(data)])
}
#' quick function to writelines to a file
#'
#' @param file_path path of text file to write contents to
#' @param mode mode of opening
#' @param raw_text the text/ list of lines to be written
#' @keywords internal
#' @return create/append/write a new file
#' @noRd
write_external_file <- function(file_path, mode, raw_text) {
file_path <- xfun::normalize_path(file_path)
write_file <- file(file_path, mode)
writeLines(raw_text, write_file)
close(write_file)
}
#' Get the name of the tex file included within wrapper file
#'
#'The wrapper file refers to an external tex file which contains
#'the actual document content.
#' @param article_dir path to the directory which contains tex article
#'
#' @return String name of the tex-file name
#' @export
#' @examples
#' article_dir <- system.file("examples/article",
#' package = "texor")
#' dir.create(your_article_folder <- file.path(tempdir(), "tempdir"))
#' x <- file.copy(from = article_dir, to = your_article_folder,recursive = TRUE,)
#' your_article_path <- paste(your_article_folder,"article",sep="/")
#' texor::get_texfile_name(your_article_path)
#' unlink(your_article_folder,recursive = TRUE)
get_texfile_name <- function(article_dir) {
article_dir <- xfun::normalize_path(article_dir)
lookup_file <- get_wrapper_type(article_dir)
if (file.exists(file.path(article_dir,lookup_file))) {
wrapper_file <- readLines(file.path(article_dir, lookup_file))
}
else {
message("LaTeX file not found !")
return(FALSE)
}
wrapper_file <- stringr::str_subset(wrapper_file, ".+")
article_start <- which(grepl(
"^\\s*\\\\begin\\{article\\}",
wrapper_file))
pre_marker <- wrapper_file[seq_len(article_start)]
post_marker <- wrapper_file[seq_len(article_start) + 1]
source_line <- setdiff(post_marker, pre_marker)
tex_file <- gsub("[[:space:]]", "",
gsub("\\\\input\\{|\\}", "", source_line))
if (!grepl(".tex$", tex_file)) {
tex_file <- paste0(tex_file, ".tex")
}
return(tex_file)
}
#' get markdown file name
#'
#' @param article_dir path to the directory which contains tex article
#'
#' @return markdown file name
#' @export
#' @examples
#' article_dir <- system.file("examples/article",
#' package = "texor")
#' dir.create(your_article_folder <- file.path(tempdir(), "tempdir"))
#' x <- file.copy(from = article_dir, to = your_article_folder,recursive = TRUE,)
#' your_article_path <- paste(your_article_folder,"article",sep="/")
#' texor::get_md_file_name(your_article_path)
#' unlink(your_article_folder,recursive = TRUE)
get_md_file_name <- function(article_dir) {
article_dir <- xfun::normalize_path(article_dir)
lookup_file <- get_wrapper_type(article_dir)
markdown_file <- xfun::with_ext(lookup_file,".md")
return (markdown_file)
}
#' get Journal details
#'
#' @param article_dir path to the directory which contains tex article
#'
#' @return journal details in an object
#' @export
#' @examples
#' article_dir <- "/home/user/documents/2022-1/2020-36/"
#' texor::get_journal_details(article_dir)
get_journal_details <- function(article_dir) {
article_dir <- xfun::normalize_path(article_dir)
journal_details <- list()
# windows
if (grepl("\\\\",article_dir)) {
hierarchy <- str_split(article_dir, "\\\\")[[1]]
}
if (grepl("/",article_dir)) {
hierarchy <- str_split(article_dir, "/")[[1]]
} else {
#--pass
}
journal_folder <- hierarchy[length(hierarchy) - 1]
if (journal_folder == "") {
journal_folder <- hierarchy[length(hierarchy) - 2]
}
journal_info <- str_split(journal_folder, "-")[[1]]
journal_details$volume <- strtoi(journal_info[1],10) - 2008
journal_details$issue <- strtoi(journal_info[2],10)
journal_details$slug <- paste("RJ", hierarchy[length(hierarchy)], sep = "-")
if (is.na(journal_details$issue)) {
journal_details$sample <- TRUE
journal_info <- str_split(Sys.Date(), "-")[[1]]
journal_details$volume <- strtoi(journal_info[1],10) - 2008
if ( strtoi(journal_info[1],10) < 2022) {
journal_details$issue <- floor(strtoi(journal_info[2],10) / 6)
}
else {
journal_details$issue <- floor(strtoi(journal_info[2],10) / 3)
journal_details$slug <- paste('RJ', strtoi(journal_info[1],10), "000", sep = "-")
}
} else {
journal_details$sample <- FALSE
}
return(journal_details)
}
#' Copy Supporting Documents like images,bib file,etc.
#'
#' Copies supporting documents like images,pdf,bib files into the output
#' folder for building the HTML version of the R-Markdown file.
#'
#' @param from_path String indicating base path for the working directory
#' @return copies dependency files into the output folder.
#' @export
#' @examples
#' article_dir <- system.file("examples/article", package = "texor")
#' dir.create(your_article_folder <- file.path(tempdir(), "tempdir"))
#' x <- file.copy(from = article_dir, to = your_article_folder,recursive = TRUE,)
#' your_article_path <- paste(your_article_folder,"article",sep="/")
#' rmarkdown::pandoc_version()
#' texor::include_style_file(your_article_path)
#' rebib::aggregate_bibliography(your_article_path)
#' texor::copy_other_files(your_article_path)
#' list.files(paste0(your_article_path,"/web/"))
#' unlink(your_article_folder,recursive = TRUE)
copy_other_files <- function(from_path) {
old_working_directory <- getwd()
setwd(from_path)
on.exit(setwd(old_working_directory))
image_paths <- generate_image_paths(from_path)
if (! dir.exists("web/")) {
dir.create("web/", showWarnings = FALSE)
}
for (path in image_paths) {
if(!dir.exists(paste0("web/",dirname(path)))) {
dir.create(paste0("web/",dirname(path)),showWarnings = TRUE)
}
if (!file.exists(path)) {
# extension naming issue
if (file.exists(xfun::with_ext(path,".PNG"))){
path <- xfun::with_ext(path,".PNG")
}
if (file.exists(xfun::with_ext(path,".JPG"))) {
path <- xfun::with_ext(path,".JPG")
}
if (file.exists(xfun::with_ext(path,".JPEG"))){
path <- xfun::with_ext(path,".JPEG")
}
if (file.exists(xfun::with_ext(path,"pdf"))){
convert_to_png(xfun::with_ext(path,"pdf"))
}
}
if (is.null(path)){
#pass
}
else {
file.copy(path, paste0("web/", path), overwrite = TRUE)
}
}
file_list <- list.files(recursive = FALSE)
extensions <- c("*.bib$", "*.pdf$", "*.R$", "*.bbl$", "_files$")
target_files <- unique(grep(paste(
extensions, collapse = "|"), file_list, value = TRUE))
file.copy(target_files,
to = "web/",
copy.mode = TRUE,
recursive = TRUE, )
}
copy_to_web <- function(rel_path, ext, article_dir){
article_dir <- xfun::normalize_path(article_dir)
if (! grepl(paste0(".",ext,"$"),rel_path)) {
rel_path <- paste0(rel_path,".",ext)
}
web_dir <- paste0(article_dir,"/web")
if (! dir.exists(web_dir)){
dir.create(web_dir)
}
if (dirname(rel_path) == ".") {
image_target_file_path <- (paste0(article_dir,"/web/",rel_path))
} else {
image_target_file_path <- (paste0(article_dir,"/web/",rel_path))
}
image_folder <- paste0(article_dir,"/web/",dirname(rel_path))
if (! dir.exists(paste0(article_dir,"/web/",dirname(rel_path)))) {
dir.create(paste0(article_dir,"/web/",dirname(rel_path)))
}
file_path <- paste0(article_dir,"/",rel_path)
web_file_path <- paste0(article_dir,"/web/",rel_path)
file.copy(file_path, web_file_path)
png_file <- paste(toString(
tools::file_path_sans_ext(file_path)), ".png",
sep = "")
}
#-------------------------------------------------------------------------------
#' generate image paths
#'
#' @param article_dir path to the directory which contains tex article
#'
#' @return list of image paths
#' @noRd
generate_image_paths <- function(article_dir) {
article_dir <- xfun::normalize_path(article_dir)
# wrapper file name
input_file <- get_wrapper_type(article_dir)
# resource path for pandoc
input_file_path <- paste(article_dir, input_file, sep = "/")
abs_file_path <- tools::file_path_as_absolute(input_file_path)
# markdown equivalent filename
temp_file <- "temp-native.txt"
temp_file_path <- paste(article_dir, temp_file, sep = "/")
image_list_filter <- system.file(
"image_list_filter.lua", package = "texor")
pandoc_opt <- c("-s",
"--resource-path", abs_file_path,
"--lua-filter", image_list_filter)
if (! pandoc_version_check()){
message(paste0("pandoc version too old, current-v : ",rmarkdown::pandoc_version()," required-v : >=2.17\n","Please Install a newer version of pandoc to run texor"))
pdf_image_paths <- NULL
return(pdf_image_paths)
}
else {
#pass
}
rmarkdown::pandoc_convert(input_file_path,
from = "latex",
to = "native",
options = pandoc_opt,
output = temp_file_path,
verbose = TRUE)
if (file.exists(paste0(article_dir,"/image_source.txt"))) {
image_paths <- readLines(paste0(article_dir,"/image_source.txt"))
} else {
image_paths <- NULL
}
return(image_paths)
}
#' Check article for sub-files
#'
#' There are some articles which include sub-section as seperate files, this
#' function checks for the existence of such files.
#'
#' @param article_dir path to the directory which contains tex article
#'
#' @return TRUE if article has multiple sub files else FALSE
#' @noRd
check_sub_sec_files <- function(article_dir) {
root_article <- texor::get_texfile_name(article_dir)
article_data <- readLines(paste0(article_dir,"/",root_article))
sub_sec_file <- article_data[grepl("\\\\input\\{",article_data)]
if (length(sub_sec_file) > 0) {
return(TRUE)
}
return(FALSE)
}
#' Ge paths of sub-files from article
#'
#' There are some articles which include sub-section as seperate files, this
#' function fetches the paths of these included files and returns them as a list.
#'
#' @param article_dir path to the directory which contains tex article
#'
#' @return a list of file names/ relative paths of included files
#' @noRd
get_sub_sec_files <- function(article_dir) {
if (!check_sub_sec_files(article_dir)) {
return(FALSE)
}
root_article <- texor::get_texfile_name(article_dir)
sub_sec_file <- readLines(paste0(article_dir,"/",root_article))
sub_sec_file <- sub_sec_file[grepl("\\\\input\\{",sub_sec_file)]
tex_files <- gsub("[[:space:]]", "",
gsub("\\\\input\\{|\\}", "", sub_sec_file))
for (i in 1:length(tex_files)) {
if (!grepl(".tex$", tex_files[i])) {
tex_files[i] <- paste0(tex_files[i], ".tex")
}
}
return(tex_files)
}