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

Escape format strings in code #7

Merged
merged 3 commits into from
Oct 7, 2024
Merged
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Imports:
cli,
commonmark,
fs,
glue,
xml2
Suggests:
mockery,
Expand Down
29 changes: 25 additions & 4 deletions R/errors-parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ errors_parse <- function(path_rmd, pattern, cmd_explain, check = TRUE) {
errors <- Map(error_parse, names(dat), dat, MoreArgs = list(info = info))
if (check) {
cli::cli_alert_info("Checking errors render")
for (err in errors) {
utils::capture.output(suppressMessages(error_render(err, TRUE)))
for (code in names(errors)) {
err <- errors[[code]]
tryCatch(
utils::capture.output(suppressMessages(error_render(err, TRUE))),
error = function(e) {
cli::cli_abort("Failure for {code}", parent = e)
})
}
cli::cli_alert_success("...all ok")
}
Expand Down Expand Up @@ -67,9 +72,17 @@ errors_read <- function(path_rmd, pattern) {
"Some headings in '{path_rmd}' don't match expected pattern")
}

nms <- sub(re, "\\1", txt[i])
if (anyDuplicated(nms)) {
dups <- unique(nms[duplicated(nms)])
cli::cli_abort(
"Some headings in '{path_rmd}' are duplicated: {dups}")
}

ret <- Map(function(from, to) trim_blank(txt[from:to]),
i + 1, c(i[-1] - 1, length(txt)))
names(ret) <- sub(re, "\\1", txt[i])
names(ret) <- nms

ret
}

Expand All @@ -96,7 +109,7 @@ error_parse_node <- function(x, info) {
## Hard, inline:
link = error_parse_link(x, info),
## Easy, inline:
code = sprintf("{.code %s}", xml2::xml_text(x)),
code = sprintf("{.code %s}", error_escape_format(xml2::xml_text(x))),
emph = sprintf("{.emph %s}", xml2::xml_text(x)),
strong = sprintf("{.strong %s}", xml2::xml_text(x)),
text = xml2::xml_text(x),
Expand Down Expand Up @@ -151,3 +164,11 @@ trim_blank <- function(x) {
}
x[i:j]
}


error_escape_format <- function(string) {
transformer <- function(text, envir) {
sprintf("{{%s}}", text)
}
glue::glue(string, .transformer = transformer)
}
37 changes: 37 additions & 0 deletions tests/testthat/test-error-parse.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,40 @@ test_that("error on unknown node", {
expect_error(error_parse_md("---", NULL),
"Unknown node in md: 'thematic_break'")
})


test_that("prevent duplicated errors", {
tmp <- withr::local_tempfile()
writeLines(
c("# `E001`",
"",
"error1",
"",
"# `E001`",
"",
"error2"),
tmp)
expect_error(
errors_read(tmp, "E[0-9]{3}"),
"Some headings in '.+' are duplicated")
})


test_that("can escape format in code", {
list(cmd_explain = "x")
txt <- c("Some `code {with}` format templates",
"",
"```",
"a <- if (a) {b} else {c}",
"```")
res <- error_parse_md(txt)
expect_length(res, 2)
expect_equal(
res[[1]],
list(type = "paragraph",
text = "Some {.code code {{with}}} format templates"))
expect_equal(
res[[2]],
list(type = "code_block",
text = "a <- if (a) {b} else {c}"))
})