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

Fix trailing comma in glue function #151

Closed
wants to merge 1 commit into from
Closed
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
38 changes: 19 additions & 19 deletions R/fix_links.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@
#'
#' @details
#'
#' ## Motivation
#' ## Motivation
#'
#' Jekyll implements [the liquid template
#' language](https://shopify.github.io/liquid/), which can break some syntax
#' expected by commonmark. If this syntax appears in a link context, that link
#' is rendred as text. Carpentries Lessons created before 2023 use Jekyll
#' and have this templating embedded for many links.
#' and have this templating embedded for many links.
#'
#' In order to convert a pre-workbench lesson to use The Workbench, we need to
#' make sure all the links are accurately represented to avoid invalid syntax
#' and broken links from sneaking into the lesson.
#' and broken links from sneaking into the lesson.
#'
#' ## Implementation Details
#'
Expand All @@ -35,7 +35,7 @@
#' ...
#' ```
#'
#' However, if a link uses liquid templating for a variable such as:
#' However, if a link uses liquid templating for a variable such as:
#' `[Home]({{ page.root }}/index.html) and other text`, it will appear in XML as
#'
#' ```xml
Expand All @@ -46,7 +46,7 @@
#' <text>({{ page.root }}/index.html) and other text</text>
#' ...
#' ```
#'
#'
#' Note: the nodes with `asis` elements are from \pkg{tinkr} protecting square
#' brackets. When we run `fix_links()`, these nodes are collapsed into a link:
#'
Expand All @@ -67,7 +67,7 @@
#' e <- Episode$new(loop, fix_links = FALSE)
#' e$links # five links
#' e$images # four images
#'
#'
#' # fix_links() ---------------------------------------------------------------
#' e$body <- asNamespace("pegboard")$fix_links(e$body)
#' e$links # eight links
Expand All @@ -85,7 +85,7 @@ fix_links <- function(body) {
#' `find_broken_links()` uses the pattern generated by `make_link_patterns()`
#' to search for potential links.
#'
#' @return
#' @return
#' - `find_broken_link()`: a list where each element represents a fragmented
#' link. Inside each element are two elements:
#' - parent: the parent paragraph node for the link
Expand All @@ -99,7 +99,7 @@ find_broken_links <- function(body) {
#'
#' @details
#' `fix_broken_links()` uses the output of `find_broken_links()` to replace the
#' node fragments with links.
#' node fragments with links.
fix_broken_links <- function(fragments) {
purrr::walk(fragments, fix_broken_link_too)
}
Expand All @@ -117,7 +117,7 @@ make_link_patterns <- function(ns = "md:") {
predicate <- gsb("(<ctext('({{')> and <ctext('}}')>)")
asis_nodes <- "text[@asis][text()=']']"
destination <- glue::glue(
".//{ns}{asis_nodes}/following-sibling::{ns}text[{predicate}]",
".//{ns}{asis_nodes}/following-sibling::{ns}text[{predicate}]"
)
return(destination)
}
Expand All @@ -130,15 +130,15 @@ gsb <- function(x) glue::glue(x, .open = "<", .close = ">")
#'
#' @param node a node determined to be a text representation of a link
#' destination
#' @return
#' @return
#' - `get_link_fragments()`: the preceding three or four nodes, which will be
#' the text of the link or the alt text of the image.
#' @rdname fix_links
get_link_fragment_nodes <- function(node) {
the_parent <- xml2::xml_parent(node)
the_children <- xml2::xml_children(the_parent)
# find the node in question by testing for identity since they represent the
# same object, they will be identical.
# same object, they will be identical.
id <- which(purrr::map_lgl(the_children, identical, node))
# test for image with endsWith because they may have an inline image.
openid <- get_start_asis(the_children, id)
Expand All @@ -148,7 +148,7 @@ get_link_fragment_nodes <- function(node) {
the_children[seq(offset, id)]
}

# find the asis node that is opener of our fragment
# find the asis node that is opener of our fragment
get_start_asis <- function(chillns, id) {
XPath <- "boolean(./self::*[@asis][text()='['])"
openers <- which(xml2::xml_find_lgl(chillns, XPath))
Expand All @@ -173,7 +173,7 @@ fix_broken_link <- function(nodes) {
# create the nodes that we use to replace the link fragment nodes
to_replace <- text_to_links(text, ns = xml2::xml_ns(nodes[[1]]), type = type)
# insert the replacements before the link fragment nodes
purrr::walk(to_replace,
purrr::walk(to_replace,
~xml2::xml_add_sibling(nodes[[1]], .x, .where = "before")
)
# remove the link fragment nodes
Expand All @@ -195,7 +195,7 @@ fix_broken_link_too <- function(nodes) {
new_txt <- make_text_nodes(as.character(txt))

# create new link node before the nodes
new_node <- xml2::xml_add_sibling(nodes[[1]], new_node_type,
new_node <- xml2::xml_add_sibling(nodes[[1]], new_node_type,
destination = end$destination, .where = "before")
# re-add text into the node
purrr::walk(new_txt, function(node) {
Expand Down Expand Up @@ -297,7 +297,7 @@ links_within_text_regex <- function() {

#' @details
#' `text_to_links()`: Splits links away from text and returns a nodeset to insert
#'
#'
#' @param txt text derived from `xml2::xml_text()`
#' @param ns a namespace object
#' @param type either "image" or "link".
Expand All @@ -309,7 +309,7 @@ links_within_text_regex <- function() {
#' @examples
#'
#' # text_to_links() -----------------------------------------------------------
#' txt <- "Some text [and _a link_]({{ page.root }}/link.to#thing),
#' txt <- "Some text [and _a link_]({{ page.root }}/link.to#thing),
#' some other text."
#' pegboard:::text_to_links(txt, type = "link")
#' md <- c(md = "http://commonmark.org/xml/1.0")
Expand All @@ -330,8 +330,8 @@ text_to_links <- function(txt, ns = NULL, type, sourcepos = NULL) {
texts[are_links] <- purrr::map_chr(texts[are_links], make_link, pattern = lnk, type = type)
texts[!are_links] <- glue::glue("<text>{texts[!are_links]}</text>")
if (!is.null(ns)) {
# TODO: fix this process for creating new nodes. Use the process from
# {tinkr} to do this.
# TODO: fix this process for creating new nodes. Use the process from
# {tinkr} to do this.
texts <- xml_new_paragraph(glue::glue_collapse(texts), ns, tag = FALSE)
texts <- xml2::xml_children(texts)
xml2::xml_set_attr(texts, "sourcepos", sourcepos)
Expand All @@ -345,7 +345,7 @@ text_to_links <- function(txt, ns = NULL, type, sourcepos = NULL) {
#' `make_link()`: makes a link depending on the link type
#'
#' @param pattern a regular expression that is used for splitting the link
#' from the surrounding text.
#' from the surrounding text.
#' @rdname fix_links
make_link <- function(txt, pattern, type = "rel_link") {
# relative tags are processed
Expand Down
Loading