Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up the html_page argument in get_chapters() #134

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions R/bookdown_to_leanpub.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ bookdown_to_embed_leanpub <- function(path = ".",
stop("No base_url is supplied and no chapt_img_key file was supplied. Need one or the other.")
}
chapt_df <- get_chapters(
bookdown_index = paste0(base_url, "index.html"),
html_page = paste0(base_url, "index.html"),
base_url = base_url
) %>%
dplyr::mutate(chapt_title = gsub("\\:|\\?|\\&|\\!|\\'", "", chapt_title))
Expand Down Expand Up @@ -431,10 +431,9 @@ make_embed_markdown <- function(url,
#'
#' @export
#'
get_chapters <- function(bookdown_index = file.path("docs", "index.html"),
base_url = NULL) {
get_chapters <- function(html_page = file.path("docs", "index.html")) {
# Read in html
index_html <- suppressWarnings(try(xml2::read_html(bookdown_index)))
index_html <- suppressWarnings(try(xml2::read_html(html_page)))

# Extract chapter nodes the Rmd way
nodes <- rvest::html_nodes(index_html, xpath = paste0("//", 'li[@class="chapter"]'))
Expand Down Expand Up @@ -469,15 +468,15 @@ get_chapters <- function(bookdown_index = file.path("docs", "index.html"),
dplyr::rename_with(~ gsub("-", "_", .x, fixed = TRUE)) %>%
dplyr::mutate(
chapt_title = stringr::word(rvest::html_text(nodes), sep = "\n", 1),
url = paste0(base_url, data_path)
url = paste0(base_url, "/", data_path)
) %>%
dplyr::select(url, chapt_title) %>%
as.data.frame() %>%
dplyr::distinct(url, .keep_all = TRUE)
}

if (nrow(chapt_data) < 1) {
stop(paste("Chapter retrieval from:", bookdown_index, "Failed."))
stop(paste("Chapter retrieval from:", html_page, "Failed."))
}

return(chapt_data)
Expand Down
Loading