Skip to content

Commit

Permalink
not ready
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Nov 28, 2023
1 parent a10f285 commit 815f5f5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
16 changes: 9 additions & 7 deletions R/translate.R
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,16 @@ protect_squaries <- function(node) {
text <- gsub("<text><\\/text>", "", text)
text <- sprintf("<text>%s</text>", 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("<notranslate>%s</notranslate>", x), text),
\(text, x) gsub(x, sprintf("<notranslate>%s</notranslate>", 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)
}

Expand All @@ -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)
)
}
11 changes: 11 additions & 0 deletions tests/testthat/test-translate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
})
})

0 comments on commit 815f5f5

Please sign in to comment.