-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into znk-add-show-arg
- Loading branch information
Showing
10 changed files
with
254 additions
and
5 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ Imports: | |
magrittr, | ||
purrr, | ||
R6, | ||
rlang (>= 0.4.5), | ||
xml2, | ||
xslt, | ||
yaml | ||
|
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
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#' Get protected nodes | ||
#' | ||
#' @param body an `xml_document` object | ||
#' @param type a character vector listing the protections to be included. | ||
#' Defaults to `NULL`, which includes all protected nodes: | ||
#' - math: via the `protect_math()` function | ||
#' - curly: via the `protect_curly()` function | ||
#' - unescaped: via the `protect_unescaped()` function | ||
#' @param ns the namespace of the document (defaults to [md_ns()]) | ||
#' @return an `xml_nodelist` object. | ||
#' @export | ||
#' @examples | ||
#' path <- system.file("extdata", "basic-curly.md", package = "tinkr") | ||
#' ex <- tinkr::yarn$new(path, sourcepos = TRUE) | ||
#' # protect curly braces | ||
#' ex$protect_curly() | ||
#' # add math and protect it | ||
#' ex$add_md(c("## math\n", | ||
#' "$c^2 = a^2 + b^2$\n", | ||
#' "$$", | ||
#' "\\sum_{i}^k = x_i + 1", | ||
#' "$$\n") | ||
#' ) | ||
#' ex$protect_math() | ||
#' # get protected now shows all the protected nodes | ||
#' get_protected(ex$body) | ||
#' get_protected(ex$body, c("math", "curly")) # only show the math and curly | ||
get_protected <- function(body, type = NULL, ns = md_ns()) { | ||
protections <- c( | ||
math = "@math", | ||
curly = "@curly", | ||
unescaped = "(@asis and text()='[' or text()=']')" | ||
) | ||
if (!is.null(type)) { | ||
keep <- rlang::arg_match(type, names(protections), multiple = TRUE) | ||
} else { | ||
keep <- TRUE | ||
} | ||
xpath <- sprintf(".//node()[%s]", paste(protections[keep], collapse = " or ")) | ||
xml2::xml_find_all(body, xpath, ns = ns) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
test_that("protected nodes can be accessed", { | ||
path <- withr::local_tempfile() | ||
# five protected curly elements | ||
curlies <- c("## curlies", | ||
"\nThis line has {xml2} one and {tinkr} two curlies!", | ||
"\n![a pretty kitten](https://placekitten.com/200/300){#kitteh alt='a picture of a kitten'}", | ||
"\n![a pretty puppy](https://placedog.net/200/300){#dog alt=\"a picture", | ||
"of a dog\"}", | ||
# two protected unescaped elements | ||
"\n[span with attributes]{.span-with-attributes ", | ||
"style='color: red'}", | ||
"" | ||
) | ||
# six protected math elements | ||
math <- c("## math", | ||
"\n$c^2 = a^2 + b^2$", # 1 | ||
"\n$$", # 2 | ||
# 3 <softbreak> | ||
"\\sum_{i}^k = x_i + 1", # 4 | ||
# 5 <softbreak> | ||
"$$", # 6 | ||
"" | ||
) | ||
writeLines(c(curlies, "\n", math), path) | ||
ex <- tinkr::yarn$new(path, sourcepos = TRUE) | ||
# we should have two protected elements right off due to the braces | ||
expect_length(ex$get_protected(), 2) | ||
|
||
# one inline math, two softbreaks, one line of block | ||
ex$protect_math() | ||
expect_length(ex$get_protected(), 2 + 6) | ||
|
||
# we should have six protected curly nodes | ||
ex$protect_curly() | ||
expect_length(ex$get_protected(), 2 + 6 + 5) | ||
|
||
expect_length(ex$get_protected("curly"), 5) | ||
expect_length(ex$get_protected("math"), 6) | ||
expect_length(ex$get_protected("unescaped"), 2) | ||
|
||
expect_error(ex$get_protected(c("curly", "shemp")), | ||
"not \"shemp\"" | ||
) | ||
}) | ||
|