Skip to content

Commit

Permalink
fix bug with backwards additions
Browse files Browse the repository at this point in the history
  • Loading branch information
zkamvar committed Oct 18, 2024
1 parent 6fff395 commit 798f62b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
25 changes: 20 additions & 5 deletions R/add_md.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,15 @@ insert_md <- function(body, md, nodes, where = "after", space = TRUE) {

shove_nodes_in <- function(body, new, nodes, where = "after", space = TRUE) {
if (inherits(nodes, "character")) {
xpath <- nodes
nodes <- xml2::xml_find_all(body, nodes, ns = md_ns())
} else {
xpath <- NULL
}
if (length(nodes) == 0) {
msg <- glue::glue("No nodes matched the expression {sQuote(xpath)}")
rlang::abort(msg, class = "insert-md-xpath")
}
if (!inherits(nodes, c("xml_node", "xml_nodeset"))) {
rlang::abort("an object of class `xml_node` or `xml_nodeset` was expected",
class = "insert-md-node"
Expand Down Expand Up @@ -83,16 +90,24 @@ add_nodes_to_nodes <- function(new, old, where = "after", space = TRUE) {
# allow purrr::walk() to work on a single node
old <- list(old)
}
purrr::walk(old, add_node_siblings, new, where = where, remove = FALSE)
purrr::walk(.x = old, .f = add_node_siblings,
new = new, where = where, remove = FALSE
)
}

# Add siblings to a node
add_node_siblings <- function(node, nodes, where = "after", remove = TRUE) {
add_node_siblings <- function(node, new, where = "after", remove = TRUE) {
# if there is a single node, then we need only add it
if (inherits(nodes, "xml_node")) {
xml2::xml_add_sibling(node, nodes, .where = where)
if (inherits(new, "xml_node")) {
xml2::xml_add_sibling(node, new, .where = where)
} else {
purrr::walk(rev(nodes), ~xml2::xml_add_sibling(node, .x, .where = where))
if (where == "after") {
# Appending new nodes requires us to insert them from the bottom to
# the top. The reason for this is because we are always using the existing
# node as a reference.
new <- rev(new)
}
purrr::walk(new, ~xml2::xml_add_sibling(node, .x, .where = where))
}
if (remove) xml2::xml_remove(node)
}
Expand Down
13 changes: 11 additions & 2 deletions tests/testthat/test-class-yarn.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ test_that("markdown can be appended to elements", {
path <- system.file("extdata", "example2.Rmd", package = "tinkr")
ex <- tinkr::yarn$new(path)
# append a note after the first heading
txt <- c("> Hello from *tinkr*!", ">", "> :heart: R")
txt <- c("The following message is sponsored by me:\n", "> Hello from *tinkr*!", ">", "> :heart: R")
# Via XPath ------------------------------------------------------------------
ex$append_md(txt, ".//md:heading[1]")
# the block quote has been added to the first heading
Expand Down Expand Up @@ -222,10 +222,12 @@ test_that("markdown can be prepended", {
nodes <- xml2::xml_find_all(ex$body,
".//node()[contains(text(), 'NERDS')]", ex$ns)
expect_length(nodes, 0)
ex$prepend_md("Table: BIRDS, NERDS", ".//md:table")
ex$prepend_md("I come before the table.\n\nTable: BIRDS, NERDS", ".//md:table")
nodes <- xml2::xml_find_all(ex$body,
".//node()[contains(text(), 'NERDS')]", ex$ns)
expect_length(nodes, 1)
pretxt <- xml2::xml_find_first(nodes[[1]], ".//parent::*/preceding-sibling::*[1]")
expect_equal(xml2::xml_text(pretxt), "I come before the table.")
})


Expand All @@ -235,6 +237,13 @@ test_that("an error happens when you try to append with a number", {
expect_error(ex$append_md("WRONG", 42), class = "insert-md-node")
})

test_that("an error happens when you try to append to a non-existant node", {
path <- system.file("extdata", "example2.Rmd", package = "tinkr")
ex <- tinkr::yarn$new(path)
expect_error(ex$append_md("WRONG", ".//md:nope"),
"No nodes matched the expression './/md:nope'",
class = "insert-md-xpath")
})


test_that("an error happens when you try to append markdown to disparate elements", {
Expand Down

0 comments on commit 798f62b

Please sign in to comment.