From 815f5f5de54c628df8e21c5c891392a44a3e2ed6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Tue, 28 Nov 2023 16:02:13 +0100 Subject: [PATCH] not ready --- R/translate.R | 16 +++++++++------- tests/testthat/test-translate.R | 11 +++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/R/translate.R b/R/translate.R index d407838..74d2fa2 100644 --- a/R/translate.R +++ b/R/translate.R @@ -347,18 +347,16 @@ protect_squaries <- function(node) { text <- gsub("<\\/text>", "", text) text <- sprintf("%s", text) - at_things <- regmatches(text, regexpr("@[[:alnum:]]*", text)) - footnote_things <- regmatches(text, regexpr("\\^[[:alnum:]]*", text)) + at_things <- regmatches(text, gregexpr("@[[:alnum:]]*", text))[[1]] + footnote_things <- regmatches(text, gregexpr("\\^[[:alnum:]]*", text))[[1]] text <- purrr::reduce( c(at_things, sub("\\^", "\\\\^", footnote_things)), - \(text, x) sub(x, sprintf("%s", x), text), + \(text, x) gsub(x, sprintf("%s", x), text), .init = text ) text <- xml2::read_xml(text) - xml2::xml_ns_strip(text) text_node <- xml2::xml_find_first(text, "//text") - xml2::xml_replace(node, .value = text_node) } @@ -382,6 +380,10 @@ unprotect_non_code_block <- function(non_code_block) { untangle_text <- function(node) { text <- trimws(xml2::xml_text(node)) xml2::xml_remove(xml2::xml_children(node)) - xml2::xml_text(node) <- gsub("\\\n", "", text) - xml2::xml_attr(node, "asis") <- 'true' + xml2::xml_replace( + node, + xml2::xml_name(node), + asis = 'true', + gsub("\\\n", "", text) + ) } diff --git a/tests/testthat/test-translate.R b/tests/testthat/test-translate.R index b6d2ea8..9056681 100644 --- a/tests/testthat/test-translate.R +++ b/tests/testthat/test-translate.R @@ -168,3 +168,14 @@ test_that("deepl_translate() doesn't remove backslashes", { expect_no_match(lines, " U00B7") }) }) + +test_that("deepl_translate() handles square brackets stuff well", { + with_mock_dir("example-square", { + lines <- deepl_translate_markdown_string( + "Wickham says blah [-@wickham2015] and he says other things[^footnote1]. + Blah Blah [see @knuth1984, pp. 33-35; also @wickham2015, chap. 1]", + source_lang = "en", + target_lang = "es" + ) + }) +})