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

Add footnote conversion: take 2 #24

Merged
merged 4 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
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: 8 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ Authors@R: c(person(given = "John",family = "Muschelli",
email = "muschellij2@gmail.com",
role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-6469-1750")),
person(given = "Shannon",family = "Ellis",
email = "shannon0ellis@gmail.com",
role = c("aut"))
person(given = "Candace",family = "Savonen",
email = "cansav09@gmail.com",
role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-6331-7070")),
person(given = "Carrie",family = "Wright",
email = "cwrigh60@jhu.edu",
role = c("ctb"))
)
Depends: R (>= 3.5.0)
License: GPL-3
Expand All @@ -31,6 +35,7 @@ Imports:
rprojroot,
magrittr,
yaml,
stringr,
here
Suggests:
didactr,
Expand Down
81 changes: 81 additions & 0 deletions R/footnotes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#' Convert Bookdown footnotes to Leanpub-formatted footnotes
#'
#' @param content a character vector containing the lines of content from a file read in with readLines()
#'
#' @return a character vector containing the content given but with Leanpub formatted foonotes
#' @export
#' @rdname footnotes
#'
convert_footnotes <- function(content) {

#### Find footnotes
# For a vector of content read in, look for Bookdown-formatted footnotes and format them as Leanpub wants them
start_footnote_indices <- grep("\\^\\[", content)

# Don't bother if there are no footnotes
if (length(start_footnote_indices) > 0) {

# Find the line which the footnote ends at
end_footnote_indices <- sapply(start_footnote_indices,
find_end_of_footnote,
content = content
)

### Build footnotes for the end of the page
# Number the footnotes:
footnote_number <- 1:length(start_footnote_indices)

# Build the footnotenotation we will replace the `^[` with
footnote_tag <- paste0("[^note", footnote_number, "]")

# Collapse multiline footnotes:
footnotes <- paste0(trimws(content[start_footnote_indices:end_footnote_indices]), collapse = " ")

# Get rid of bookdown formatting in the footnotes
footnotes <- stringr::str_remove_all(footnotes, "\\^\\[|\\]$")

# Add footnote tag at the beginning
footnotes <- paste0(footnote_tag, ": ", footnotes)

#### Remove footnotes from the middle of the page
# Delete anything after a old footnote tag and put the new footnote tag
content[start_footnote_indices] <- paste0(stringr::word(content[start_footnote_indices], sep = "\\^\\[", 1), footnote_tag)

# Delete end lines
content[end_footnote_indices] <- stringr::word(content[end_footnote_indices], sep = "\\]$", 2)

# Delete middle lines completely
find_any_middle_lines <- setdiff(start_footnote_indices:end_footnote_indices,
c(start_footnote_indices, end_footnote_indices))

content <- content[-find_any_middle_lines]

#### Append footnotes to the end of the file
content <- append(content, c("\n", footnotes))
}
return(content)
}

# Given an index of the start of a footnote, find the end of it.
find_end_of_footnote <- function(start_footnote_index, content) {

# See if the end of the footnote is in the same line
end_bracket <- grepl("\\]$", content[start_footnote_index])

# Keep looking in each next line until we find it.
if (end_bracket == FALSE) {
footnote_index <- start_footnote_index
while (end_bracket == FALSE) {
# Add one
footnote_index <- footnote_index + 1

# Look in next line
end_bracket <- grepl("\\]$", content[footnote_index])

if (footnote_index == length(content) && end_bracket == FALSE) {
stop(paste("Searched end of file and could not find end of footnote:", content[start_footnote_index]))
}
}
return(footnote_index)
}
}
4 changes: 4 additions & 0 deletions R/replace_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,10 @@ replace_single_html <- function(file,
element = "iframe", fullbleed = fullbleed,
remove_resources_start = remove_resources_start
)
if (verbose) {
message("Converting footnotes")
}
x <- convert_footnotes(x)

# need to actually do changes
writeLines(x, con = file)
Expand Down