-
Notifications
You must be signed in to change notification settings - Fork 55
/
build.R
106 lines (86 loc) · 3.02 KB
/
build.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
source("r/utils.R")
source("r/ascii.R")
if (!dir.exists("ascii")) dir.create("ascii")
if (!dir.exists("mds")) dir.create("mds")
chapters <- list(
"preface",
"intro",
"starting",
"analysis",
"modeling",
"pipelines",
"clusters",
"connections",
"data",
"tuning",
"extensions",
"distributed-r",
"streaming",
"contributing",
"appendix"
)
chapters_pattern <- paste(chapters, collapse = "|")
for (chapter_file in dir(pattern = paste0(chapters_pattern, ".Rmd"))) {
chapter_name <- tools::file_path_sans_ext(basename(chapter_file))
withr::with_envvar(
list(ASCIITEXT_RENDERING = TRUE), {
knitr::knit(chapter_file, file.path("mds", paste(chapter_name, "md", sep = ".")))
}
)
rmarkdown::pandoc_convert(
input = normalizePath(paste0("mds/", chapter_name, ".md")),
to = "asciidoc",
options = c("--columns=2000"),
output = file.path("../ascii", paste0(chapter_name, ".asciidoc"))
)
if (identical(chapter_name, "preface")) {
preface_own <- readLines("ascii/preface.asciidoc")
preface_publisher <- readLines("../learning-apache-spark-with-r/preface_publisher.asciidoc")
preface_joined <- c("[preface]", "== Preface", preface_own[-c(1,2)], "", preface_publisher)
writeLines(preface_joined, "ascii/preface.asciidoc")
}
}
files <- normalizePath(dir("ascii", full.names = T, pattern = chapters_pattern))
books_bib <- bibtex::read.bib("book.bib")
transformations <- list(
" ?\\[@([a-zA-Z0-9\\-]+)\\]" = ascii_add_footnotes,
"\nimage:images([^\\[]+)\\[([^\\]]+)\\]" = ascii_add_image_captions,
"\n\\*(Note|Tip|Warning):\\* " = ascii_add_notes,
"Figure[ \n]+@ref\\(fig:([^\\)]+)\\)" = ascii_add_figure_references,
"=== Later review" = ascii_remove_later
)
for (file in files) {
lines <- readLines(file)
all_lines <- paste(lines, collapse = "\n")
for (regval in names(transformations)) {
rem_lines <- all_lines
fixed_lines <- c()
while (grepl(regval, rem_lines, perl = TRUE)) {
r <- regexec(regval, rem_lines, perl = TRUE)
groups <- regmatches(rem_lines, r)[[1]]
start_idx <- r[[1]][1]
end_idx <- start_idx + nchar(groups[1])
previous_str <- substr(rem_lines, 1, start_idx - 1)
rem_lines <- substr(rem_lines, end_idx, nchar(rem_lines))
result <- transformations[[regval]](groups[-1], books_bib, rem_lines)
if (is.list(result)) {
replacement <- result$replacement
rem_lines <- substr(rem_lines, result$end_idx, nchar(rem_lines))
}
else {
replacement <- result
}
fixed_lines <- c(
fixed_lines,
previous_str,
replacement
)
}
all_lines <- paste(c(fixed_lines, rem_lines), collapse = "")
}
writeLines(all_lines, file)
}
ascii_files <- normalizePath(dir("ascii", chapters_pattern, full.names = T))
image_files <- normalizePath(dir("images", chapters_pattern, full.names = T))
file.copy(from = ascii_files, "../learning-apache-spark-with-r/", overwrite = T)
file.copy(from = image_files, "../learning-apache-spark-with-r/images/", overwrite = T)