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

simplest way to add the embedded files and video scripts although possibly there is a better way #3

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ Imports:
curl,
rprojroot,
magrittr,
yaml
yaml,
here,
stringr
Suggests:
didactr,
knitr,
Expand Down
2 changes: 2 additions & 0 deletions R/bookdown_to_leanpub.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ bookdown_to_leanpub = function(path = ".",
make_book_txt = TRUE,
remove_resources_start = FALSE,
verbose = TRUE) {
source(build_leanpub_embed_files.R)

rmd_regex = "[.][R|r]md$"

Expand Down Expand Up @@ -212,6 +213,7 @@ bookdown_to_leanpub = function(path = ".",
full_output_files = normalizePath(md_files, winslash = "/"))
L$book_txt_output = out
return(L)
source(build_leanpub_video.R)
}


Expand Down
41 changes: 41 additions & 0 deletions R/build_leanpub_embed_files.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# script to fix embedded files for Leanpub
# originally used the raw rmd files before conversion with bookdown_to_leanpub() - now part of the function at the beginning

library(here)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think since you added these in the dependencies list, having them called by library() here is not necessary.

library(stringr)


split_embed_knitr<-function(to_replace_embed) {
# grab the second set of text in quotes
link_url <-strsplit(to_replace_embed, "\\\"")[[1]][2]
y = paste0("Check out this [link](", link_url,").")
y}

split_embed_html<-function(to_replace_embed) {
# grab contents after src=
link_url <-strsplit(to_replace_embed, "src=")[[1]][2]
# only keep the part after "\" and bfore the next element
link_url <-strsplit(link_url, "\\\"")[[1]][2]
y = paste0("Check out this [link](", link_url,").")
y}

# to find rmd files to modify
files<-list.files(here(), pattern = ".rmd|.Rmd|.RMd|.RMD")

#code to modify the lines within the rmd about the embedded files - to instead create a link
for(i in files){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ideally this would be a function too. I can work on functional-izing this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be used for a function to add text about feedback...

tx_i <- readLines(here::here(i))
# want lines that have include_url but not youtube
to_replace_embed_knitr <-tx_i[grepl(pattern = "include_url", tx_i, fixed=TRUE) & !grepl("^<!--", trimws(tx_i)) & !grepl("youtube", trimws(tx_i))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense for this to be its own function in the package.

to_replace_embed_html <-tx_i[grepl(pattern = "iframe src", tx_i, fixed=TRUE) & !grepl("^<!--", trimws(tx_i)) & !grepl("youtube", trimws(tx_i))]
to_replace_embed_knitr_loc <-intersect(intersect(grep(pattern = "include_url", tx_i, fixed=TRUE), grep("^<!--", trimws(tx_i), invert = TRUE)) ,grep("youtube", trimws(tx_i), invert = TRUE))
to_replace_embed_knitr_loc <- (to_replace_embed_knitr_loc +2) #move one line down outside chunk
to_replace_embed_html_loc <-intersect(intersect(grep(pattern = "iframe src", tx_i, fixed=TRUE), grep("^<!--", trimws(tx_i), invert = TRUE)), grep("youtube", trimws(tx_i), invert = TRUE))
# use split_emed_knitr function to replace the knitr version of embedded files
tx_i[to_replace_embed_knitr_loc] <-unlist(lapply(to_replace_embed_knitr , split_embed_knitr))
# use split_emed_html function to replace the html version of embedded files
tx_i[to_replace_embed_html_loc] <-unlist(lapply(to_replace_embed_html , split_embed_html))
writeLines(tx_i, con=here(i))
#just in case lets remove the temp version of the file
rm(tx_i)
}
42 changes: 42 additions & 0 deletions R/build_leanpub_video.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# script to fix videos for Leanpub
# originally used the output of the bookdown_to_leanpub() - now part of the function at the end

library(here)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment here.

library(stringr)

# to change from embed to watch for lines about youtube videos
split_youtube<-function(utube) {
alt <- str_extract(string = utube, pattern = "(?<=\\[).*(?=\\])")
utube <-str_extract(string = utube, pattern = "(?<=embed/).*(?=\\))")
y = paste0( "![Video](", "https://www.youtube.com/watch?v=", utube, ")")
y}

# to find md files to modify
files<-list.files(here("manuscript"), pattern = ".md")
# to only modify original - will thus overwrite any "leanpub_ready" markdown files which are only created with the following code
files <- files[!grepl(pattern = "leanpub_ready", files)]

#code to modify the lines within the md about the youtube videos - both attributes and the actual video call
for(i in files){
tx_i <- readLines(here::here("manuscript", i))
# want lines that have youtube and https in them but not commented out
to_replace_video <-tx_i[grepl(pattern = "youtube", tx_i, fixed=TRUE) & grepl(pattern = "https", tx_i, fixed=TRUE) & !grepl("^<!--", trimws(tx_i))]
# use split_youtube function to change the way the video is called
tx_i[grepl(pattern = "youtube", tx_i) & grepl(pattern = "https", tx_i) & !grepl("^<!--", trimws(tx_i))]<- unlist(lapply(to_replace_video, split_youtube))
# will replace attribute line with this so that an image shows and the video works in markua
r <-"{type: video, poster: 'http://img.youtube.com/vi/VOCYL-FNbr0/mqdefault.jpg',"
# will replace attribute lines ... one above video line - so first select these lines
to_replace_video_att <-(intersect(intersect(grep("youtube",tx_i) , grep(pattern = "https", tx_i)), grep("^<!--",tx_i,invert=TRUE))-1)
tx_i[to_replace_video_att]<-
str_replace(tx_i[to_replace_video_att], "\\{", r)
#write new files with the replaced lines but call the files leanpub_ready
writeLines(tx_i, con=here("manuscript", paste("leanpub_ready", i, sep = "_")))
#just in case lets remove the temp version of the file
rm(tx_i)
}

# code to change book.text to include only leanpub_ready md files
Book <-readLines(here("manuscript", "Book.txt"))
# will only change orininal md files... thus the leanpub_ready md files will get rewritten each time code is run
Book[!grepl(pattern = "leanpub_ready", Book)] <- paste("leanpub_ready", Book[!grepl(pattern = "leanpub_ready", Book)], sep = "_")
writeLines(Book, here("manuscript", "Book.txt"))