diff --git a/R/translate.R b/R/translate.R
index 6f8e449..20ec117 100644
--- a/R/translate.R
+++ b/R/translate.R
@@ -386,11 +386,13 @@ unprotect_math <- function(math) {
}
protect_squaries <- function(node) {
text <- xml2::xml_text(node)
- text <- gsub("\\[", "", text)
- text <- gsub("\\]", "", text)
- text <- sprintf("%s", text)
- text <- gsub("<\\/text>", "", text)
- text <- sprintf("%s", text)
+ text <- gsub('\\[', '', text)
+ text <- gsub('\\]', '', text)
+ text <- sprintf('%s', text)
+ text <- gsub('<\\/text>', '', text)
+ text <- sprintf('%s', text)
+ # hack to preserve colon that DeepL API kills
+ text <- gsub(":", ":", text)
at_things <- regmatches(text, gregexpr("@[[:alnum:]]*", text))[[1]]
footnote_things <- regmatches(text, gregexpr("\\^[[:alnum:]]*", text))[[1]]
@@ -407,7 +409,13 @@ protect_squaries <- function(node) {
unprotect_squary <- function(node) {
xml2::xml_name(node) <- "text"
- xml2::xml_text(node) <- sprintf("[%s]", trimws(xml2::xml_text(node)))
+ node_text <- trimws(xml2::xml_text(node))
+ if (!grepl(":$", node_text)) {
+ xml2::xml_text(node) <- sprintf("[%s]", node_text)
+ } else {
+ node_text <- sub(":$", "", node_text)
+ xml2::xml_text(node) <- sprintf("[%s]:", node_text)
+ }
}
unprotect_notranslate <- function(node) {
@@ -423,11 +431,21 @@ unprotect_non_code_block <- function(non_code_block) {
}
untangle_text <- function(node) {
- text <- trimws(xml2::xml_text(node))
+ text <- xml2::xml_text(node)
+ text <- gsub("\\s+", " ", text) # like str_squish w/o str_trim
+ # trying to only leave space where needed
+ no_left_sibling <- (length(xml2::xml_find_first(node, "preceding-sibling::*")) == 0)
+ which <- if (no_left_sibling) {
+ "both"
+ } else {
+ "right"
+ }
+ text <- trimws(text, which = which)
xml2::xml_remove(xml2::xml_children(node))
xml2::xml_replace(
node,
xml2::xml_name(node),
+ `xml:space`="preserve",
asis = 'true',
gsub("\\\n", "", text)
)
diff --git a/inst/example-equations-footnote.md b/inst/example-equations-footnote.md
new file mode 100644
index 0000000..1fe953a
--- /dev/null
+++ b/inst/example-equations-footnote.md
@@ -0,0 +1,3 @@
+Voyez $a$ une équation avec une note ensuite [^1].
+
+[^1]: une note de bas de page.
diff --git a/tests/testthat/_snaps/translate.md b/tests/testthat/_snaps/translate.md
index efd456f..8379f04 100644
--- a/tests/testthat/_snaps/translate.md
+++ b/tests/testthat/_snaps/translate.md
@@ -46,3 +46,14 @@
Output
[1] "$i_t = j_t$"
+# deepl_translate() handles equations+footnote well
+
+ Code
+ foot_math_lines
+ Output
+ [1] "See $a$ an equation with a note afterwards [^1] ."
+ [2] ""
+ [3] "[^1]: a footnote."
+ [4] ""
+ [5] ""
+
diff --git a/tests/testthat/fixtures/example-equations-footnote/api/translate-746e3b-POST.json b/tests/testthat/fixtures/example-equations-footnote/api/translate-746e3b-POST.json
new file mode 100644
index 0000000..4f8bf0a
--- /dev/null
+++ b/tests/testthat/fixtures/example-equations-footnote/api/translate-746e3b-POST.json
@@ -0,0 +1,8 @@
+{
+ "translations": [
+ {
+ "detected_source_language": "FR",
+ "text": "\n\n\n \n See <\/text>\n