Skip to content

Commit

Permalink
Fix issues in R-intro #1
Browse files Browse the repository at this point in the history
  • Loading branch information
andrie committed Jan 6, 2022
1 parent a029640 commit f7b35ed
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 27 deletions.
23 changes: 17 additions & 6 deletions R/convert.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ convert_html_to_md <- function(input_file, verbose = FALSE) {
if (basename(input_file) == "index.html") {
book_title <- extract_title_from_index(input_file)
meta_data <- glue::glue("--metadata=book_title:\"{book_title}\"")
lua_filters <- c(make_lua_filter("index.lua"), lua_filters)
lua_filters <- c(lua_filters, make_lua_filter("index.lua"))
} else {
meta_data = character()
}
Expand All @@ -52,6 +52,8 @@ convert_html_to_md <- function(input_file, verbose = FALSE) {
)
# })

# browser(expr = basename(input_file) == "index.html")

temp_md %>%
read_lines() %>%
# remove empty hyperlinks [](...)
Expand All @@ -60,18 +62,23 @@ convert_html_to_md <- function(input_file, verbose = FALSE) {
gsub("\\{\\.sample\\}", "", .) %>%
# fix footnote cross-references
gsub("\\[\\^(\\d+)\\^\\]\\(#FOOT\\d+\\)\\{#DOCF\\d+\\}", "[^\\1]", .) %>%
gsub("\\[\\^(\\d+)\\^\\]\\(#FOOT\\d+\\)\\{#DOCF\\d+\\}", "[^\\1]", .) %>%
gsub("\\[\\((\\d+)\\)\\]\\(#DOCF\\d+\\)\\{#FOOT\\d+\\}", "[^\\1]", .) %>%
gsub("\\[\\((\\d+)\\)\\]\\(#DOCF\\d+\\)\\{#FOOT\\d+\\}", "[^\\1]:", .) %>%
# Fix function definition lists
gsub("^(Function: .*)\\\\$", "\\1\n: \n", .) %>%
gsub("^(`.*`)\\\\$", "\\1\n: \n", .) %>%
# Fix indented codeblocks by adding a newline in front of them
gsub("(^ ``` c)", "\n\\1", .) %>%
# remove quote around backticks
gsub("('`|`')", "`", .) %>%
# insert missing colon in footnote references
gsub("^(\\[\\^\\d+\\])$", "\\1:", .) %>%
write_lines(path = output_file)

# Remove Table of Contents heading from index.md
if (basename(input_file) == "index.html") {
read_lines(temp_md) %>%
read_lines(output_file) %>%
sub("^Table of Contents \\{.*\\}$", "", .) %>%
write_lines(output_file)
}
Expand All @@ -97,21 +104,25 @@ convert_html_to_md <- function(input_file, verbose = FALSE) {
convert_to_md <- function(path = "temp", verbose = FALSE) {
file_list <- fs::dir_ls(path = path, glob = "*.html")

if (verbose) cli::cli_progress_step("Converting HTML to markdown")
# if (verbose) cli::cli_progress_step("Converting HTML to markdown")

# create progress bar
withr::local_options(list(cli.progress_show_after = 0))
cli::cli_progress_bar(
total = length(file_list),
extra = list(file = ""),
format = "{cli::pb_spin} Converting {cli::pb_extra$file} {cli::pb_current}/{cli::pb_total}",
clear = FALSE
format = paste(
"{cli::pb_spin} Converting {cli::pb_extra$file}",
"{cli::pb_current}/{cli::pb_total} to markdown"
),
clear = TRUE
)

for (i in seq_along(file_list)) {
cli::cli_progress_update(extra = list(file = file_list[i]))
convert_html_to_md(file_list[i])
cli::cli_progress_update(extra = list(file = file_list[i]))
}
cli::cli_progress_done()
cli::cli_alert_success("Converted all HTML files to markdown")
invisible()
}
6 changes: 4 additions & 2 deletions R/download_manuals.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ download_manuals <- function(
readr::read_lines(glue::glue("{destdir}/{manual}")) %>%
stringr::str_match("@image\\{images/(.*?),.*\\}") %>%
na.omit() %>%
.[,2]
.[, 2]

fs::dir_create(glue::glue("{destdir}/images"))
for (i in seq_along(image_names)) {
Expand All @@ -128,7 +128,9 @@ download_manuals <- function(

#' Pre-process the texi files.
#'
#' At the moment the only pre-processing is to remove `@group` statements. In PDF format this ensures line breaks don't occur in the middle of code blocks, but in HTML this causes multiple `pre` statements.
#' At the moment the only pre-processing is to remove `@group` statements.
#' In PDF format this ensures line breaks don't occur in the middle of code blocks,
#' but in HTML this causes multiple `pre` statements.
#'
#' @inheritParams download_manuals
pre_process_manuals <- function(
Expand Down
2 changes: 1 addition & 1 deletion R/make_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ make_info <- function(manual, input_dir = "data", output_dir = "temp", verbose =
fs::dir_create(output_dir)
fs::dir_ls(path = output_dir) %>% fs::file_delete()
filename <- glue::glue("{input_dir}/{manual}")
cli::cli_progress_step("Running makeinfo to convert {.file {filename}} to html")
cli::cli_progress_step("Running makeinfo to convert {.file {filename}} to html", msg_done = "Running makeinfo")

if (.Platform$OS.type == "windows") {
system2(
Expand Down
36 changes: 22 additions & 14 deletions R/process_manual.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
#'
#' @return NULL.
#' @export
process_manual <- function(
manual = "R-exts.texi",
folder = tolower(sub(".texi", "", manual)),
manuals_folder = "manuals",
.quicktest = FALSE,
.make_info = !.quicktest,
.download = !.quicktest,
verbose = TRUE
) {
process_manual <- function(manual = "R-exts.texi",
folder = tolower(sub(".texi", "", manual)),
manuals_folder = "manuals",
.quicktest = FALSE,
.make_info = !.quicktest,
.download = !.quicktest,
verbose = TRUE) {
cli::cli_h2("Processing {manual}")

data_folder <- glue::glue("{manuals_folder}/{folder}/data")
Expand All @@ -44,7 +42,11 @@ process_manual <- function(
# create folder for intermediate output
if (fs::dir_exists(prep_folder)) fs::dir_delete(prep_folder)
fs::dir_create(prep_folder)
make_info(manual, input_dir = data_folder, output_dir = prep_folder, verbose = verbose)
make_info(
manual,
input_dir = data_folder, output_dir = prep_folder,
verbose = verbose
)

# Rename files containing "_002d" to "-"
fs::dir_ls(prep_folder, glob = "*_002d*") %>%
Expand All @@ -62,9 +64,6 @@ process_manual <- function(
fs::dir_ls(book_folder, glob = "*.html") %>% fs::file_delete()

# copy markdown files to book folder

# browser()

if (.quicktest) {
fs::dir_ls(prep_folder, glob = "*.md")[1:3] %>%
fs::file_copy(book_folder, overwrite = TRUE)
Expand All @@ -73,6 +72,15 @@ process_manual <- function(
fs::file_copy(book_folder, overwrite = TRUE)
}

# copy images if present
images_present <- dir_ls(data_folder, glob = "*images")
if (length(images_present)) {
fs::dir_copy(
glue::glue("{data_folder}/images"),
glue::glue("{book_folder}/images")
)
}

# copy template files to book folder

glue_quarto_yaml(
Expand All @@ -83,4 +91,4 @@ process_manual <- function(

fs::file_copy("book_template/custom.scss", book_folder, overwrite = TRUE)
invisible()
}
}
16 changes: 12 additions & 4 deletions scripts/build_website.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ manuals <- c(
"R-admin.texi",
"R-exts.texi",
"R-lang.texi",
"R-ints.texi"
"R-ints.texi",
NULL
)

# purrr::walk(manuals, process_manual, .quicktest = TRUE)
Expand Down Expand Up @@ -72,8 +73,15 @@ build_a_book <- function(x, index){
#
# This will modify the _quarto.yml navbar to include all manuals and then
# build each manual as a Quarto book project
build_books <- function(manuals_folder = "manuals") {
manuals <- fs::dir_ls(manuals_folder)
build_books <- function(manuals_folder = "manuals", manuals) {

if (!missing(manuals) && !is.null(manuals)) {
manuals <- tolower(gsub("\\.texi", "", manuals))
manuals <- file.path(manuals_folder, manuals)
} else {
manuals <- fs::dir_ls(manuals_folder)
}

index <- fs::path("..", fs::path_file(manuals), "index.html")
names(index) <- fs::path_file(manuals)

Expand All @@ -87,7 +95,7 @@ build_books <- function(manuals_folder = "manuals") {
})
}

build_books()
build_books(manuals = manuals)

# Build website --------------
#
Expand Down

0 comments on commit f7b35ed

Please sign in to comment.