-
Notifications
You must be signed in to change notification settings - Fork 1
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
Fix youtube and embed links #14
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8e18cf6
Add in carriewright link splitting code
cansavvy 92ddc63
Mapping things out
cansavvy 4d8d022
Styled code, youtube -> watch
cansavvy dfcb9a0
Merge branch 'master' into cansavvy/link-splitters
cansavvy 5604a53
Get rid of old scratch script "split_embed_links.R"
cansavvy 004f8c2
Merge branch 'cansavvy/link-splitters' of https://github.com/jhudsl/l…
cansavvy 4dab4a7
Adding youtube poster attribute
cansavvy d173d50
Get rid of old version of fix
cansavvy 675d0ff
add stringr as dependency
cansavvy 7ad1b4e
Add comma
cansavvy 85c72ec
Add link to include_url()
cansavvy 874a24e
Change wording of links
cansavvy 74412a6
Merge branch 'master' into cansavvy/link-splitters
cansavvy 94579ac
Get rid of stringr dependency
cansavvy 7c50234
Add logic for ! or not
cansavvy 91af811
Fixing logic
cansavvy 155b18a
Fix some of the logic
cansavvy 62eea5f
Revert "Get rid of stringr dependency"
cansavvy 8641422
Revert "Revert "Get rid of stringr dependency""
cansavvy 53fb9bd
Auto stash before revert of "Revert "Get rid of stringr dependency""
cansavvy 7a05871
Fix logic around videos
cansavvy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -205,7 +205,7 @@ margin_to_align <- function(x) { | |
|
||
build_image <- function(src, ..., caption = NULL, embed = NULL, | ||
fullbleed = FALSE, | ||
remove_resources_start = TRUE) { | ||
remove_resources_start = TRUE, element = NULL) { | ||
if (remove_resources_start) { | ||
src <- gsub("^resources/", "", src) | ||
} | ||
|
@@ -216,11 +216,13 @@ build_image <- function(src, ..., caption = NULL, embed = NULL, | |
src = src | ||
) | ||
myenv <- as.environment(myenv) | ||
x <- c( | ||
specs <- c( | ||
'alt: "{alt}",', | ||
'height: "{height}",', | ||
'width: "{width}",', | ||
'align: "{align}"', | ||
'type: "{type}"', | ||
'poster: "{poster}"', | ||
'embed: "{embed}"' | ||
) | ||
if (is.null(fullbleed) || | ||
|
@@ -229,16 +231,68 @@ build_image <- function(src, ..., caption = NULL, embed = NULL, | |
is.na(fullbleed)) { | ||
fullbleed <- FALSE | ||
} | ||
x <- sapply(x, glue::glue, .envir = myenv) | ||
x <- unlist(sapply(x, as.character)) | ||
x <- c(x, if (fullbleed) "fullbleed: true") | ||
x <- paste(x, collapse = " ") | ||
x <- paste0("{", x, "}\n") | ||
x <- paste0(x, paste0("![", myenv$caption, "](", myenv$src, ")")) | ||
x | ||
|
||
## Set defaults for items that haven't been specified | ||
|
||
# Default for align is center | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added these two defaults. |
||
if(is.null(myenv$align)) {myenv$align <- "center"} | ||
|
||
# Default for width is 100% | ||
if(is.null(myenv$width)) {myenv$width <- "100%"} | ||
|
||
# Put everything together | ||
specs <- sapply(specs, glue::glue, .envir = myenv) | ||
|
||
# Make sure it's coerced as a character | ||
specs <- unlist(sapply(specs, as.character)) | ||
|
||
# Set as fullbleed if TRUE | ||
specs <- c(specs, if (fullbleed) "fullbleed: true") | ||
|
||
# Collapse it all together and add a new line | ||
specs <- paste(specs, collapse = " ") | ||
specs <- paste0("{", specs, "}\n") | ||
|
||
# If caption was set, use that for link | ||
# Default is to set this for a link | ||
words <- "Check out this link" | ||
|
||
# If a caption is set use that | ||
if (!is.null(myenv$caption)) { | ||
words <- myenv$caption | ||
|
||
# Otherwise if video use this wording | ||
} else if (!is.null(myenv$type)) { | ||
if (myenv$type == "video") words <- "Click on the lower right corner to expand the screen" | ||
|
||
# Otherwise if image use this wording | ||
} else if (!is.null(element)) { | ||
if (element == "img") words <- "" | ||
} | ||
|
||
# Default is to not use a ! | ||
link <- paste0("[", words, "](", myenv$src, ").") | ||
|
||
# But if its an image or video, use use ! | ||
if (!is.null(element)) { | ||
if (element == "img") { | ||
link <- paste0("![", words, "](", myenv$src, ").") | ||
} | ||
} | ||
if (!is.null(myenv$type)) { | ||
if (myenv$type == "video") { | ||
link <- paste0("![", words, "](", myenv$src, ").") | ||
} | ||
} | ||
|
||
# Tack on the link | ||
specs <- paste0(specs, link) | ||
|
||
return(specs) | ||
} | ||
|
||
replace_div_data <- function(x, fullbleed = FALSE, remove_resources_start = TRUE) { | ||
replace_div_data <- function(x, fullbleed = FALSE, remove_resources_start = TRUE, | ||
element = NULL) { | ||
div_index <- find_figure_div(x) | ||
if (NROW(div_index) == 0) { | ||
return(x) | ||
|
@@ -256,7 +310,7 @@ replace_div_data <- function(x, fullbleed = FALSE, remove_resources_start = TRUE | |
attributes <- c( | ||
"src", "alt", "height", | ||
"width", "style", "caption", "title", | ||
"embed" | ||
"embed", "type", "poster" | ||
) | ||
if (length(ii) == 1) ii <- ii[[1]] | ||
args <- as.list(ii) | ||
|
@@ -267,6 +321,7 @@ replace_div_data <- function(x, fullbleed = FALSE, remove_resources_start = TRUE | |
} | ||
args <- lapply(args, empty_to_null) | ||
args$remove_resources_start <- remove_resources_start | ||
args$element <- element | ||
do.call(build_image, args = args) | ||
}) | ||
first_div_index <- sapply(div_indices, dplyr::first) | ||
|
@@ -305,14 +360,25 @@ replace_image_data <- function(x, element = c("img", "iframe"), fullbleed = FALS | |
|
||
attributes <- c( | ||
"src", "alt", "height", "width", "style", | ||
"caption", "title", "fullbleed" | ||
"caption", "title", "fullbleed", "type", "poster" | ||
) | ||
# style="display: block; margin: auto;" is center | ||
image_attributes <- lapply(images, function(x) { | ||
out <- lapply(attributes, function(name) { | ||
na_empty(get_html_attr(x = x, name = name, element = element)) | ||
}) | ||
names(out) <- attributes | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the brand new chunk of code that does the youtube link switching. |
||
|
||
# If it has a youtube embed link, switch to the watch format link | ||
if (grepl("www.youtube.com/embed", out$src)) { | ||
out$src <- paste0("https://www.youtube.com/watch?v=", | ||
strsplit(out$src, | ||
split = "www.youtube.com/embed/")[[1]][2] | ||
) | ||
# If it's youtube put this image in the tag | ||
out$type <- "video" | ||
out$poster <- "http://img.youtube.com/vi/VOCYL-FNbr0/mqdefault.jpg" | ||
} | ||
if (length(unlist(out) == 0)) { | ||
# when <p align = "center> | ||
msg <- paste0( | ||
|
@@ -334,8 +400,10 @@ replace_image_data <- function(x, element = c("img", "iframe"), fullbleed = FALS | |
|
||
out_images <- sapply(image_attributes, function(args) { | ||
args$remove_resources_start <- remove_resources_start | ||
args$element <- element | ||
do.call(build_image, args = args) | ||
}) | ||
|
||
out_images <- c(unlist(out_images)) | ||
stopifnot(length(out_images) == length(image_index)) | ||
out_x <- x | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added these two new attributes so we can specify the youtube poster image. 🎉