diff --git a/DESCRIPTION b/DESCRIPTION index ffb2e33..f161572 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -47,5 +47,5 @@ Suggests: testthat (>= 3.0.0) Remotes: ropensci-review-tools/babelquarto, - ropensci/tinkr + ropensci/tinkr@feature-add-get-protected Config/testthat/edition: 3 diff --git a/R/translate.R b/R/translate.R index 39ae1d2..1c75960 100644 --- a/R/translate.R +++ b/R/translate.R @@ -157,11 +157,13 @@ translate_part <- function(xml, woolish$body <- fakify_xml(xml) ## protect content inside curly braces and math ---- - woolish$body <- tinkr::protect_math(woolish$body) - woolish$body <- tinkr::protect_curly(woolish$body) - curlies <- xml2::xml_find_all(woolish$body, "//*[@curly]") + ## protect content inside curly braces and math ---- + woolish$protect_math()$protect_curly() + + curlies <- woolish$get_protected("curly") purrr::walk(curlies, protect_curly) - maths <- xml2::xml_find_all(woolish$body, "//*[@asis='true']") + + maths <- woolish$get_protected("math") purrr::walk(maths, protect_math) ## protect content inside square brackets ---- diff --git a/tests/testthat/_snaps/translate.new.md b/tests/testthat/_snaps/translate.new.md new file mode 100644 index 0000000..5a9c617 --- /dev/null +++ b/tests/testthat/_snaps/translate.new.md @@ -0,0 +1,66 @@ +# deepl_translate() errors well + + Code + deepl_translate("non-existing-file") + Condition + Error in `deepl_translate()`: + ! Can't find path "non-existing-file". + +--- + + Code + deepl_translate(file, glossary_name = "non-existing-glossary", source_lang = "en", + target_lang = "es") + Condition + Error in `deepl_translate()`: + ! Can't find glossary_name "non-existing-glossary". + i Check the spelling, or create it with `upsert_glossary()`. + +--- + + Code + deepl_translate(file, formality = "non-existing-formality", source_lang = "en", + target_lang = "es") + Condition + Error in `deepl_translate()`: + ! `formality` must be one of "default", "more", "less", "prefer_more", or "prefer_less", not "non-existing-formality". + +# deepl_translate() handles equations well + + Code + math_lines[4] + Output + [1] "" + +--- + + Code + sub(".*que ", "", math_lines[7]) + Output + [1] "$i_t = j_t$" + +--- + + Code + math_lines[9] + Output + [1] "" + +# 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] "" + +# deepl_translate() handles equations with curly well + + Code + foot_curly_lines[5] + Output + [1] "$a_{ij}$" + diff --git a/tests/testthat/fixtures/example-equations-curly/api/translate-adef71-POST.json b/tests/testthat/fixtures/example-equations-curly/api/translate-8089b4-POST.json similarity index 69% rename from tests/testthat/fixtures/example-equations-curly/api/translate-adef71-POST.json rename to tests/testthat/fixtures/example-equations-curly/api/translate-8089b4-POST.json index b66f6c9..ebeebeb 100644 --- a/tests/testthat/fixtures/example-equations-curly/api/translate-adef71-POST.json +++ b/tests/testthat/fixtures/example-equations-curly/api/translate-8089b4-POST.json @@ -2,7 +2,7 @@ "translations": [ { "detected_source_language": "FR", - "text": "\n\n\n \n \n $a_{ij}$<\/math>\n \n <\/paragraph>\n<\/document>\n" + "text": "\n\n\n \n \n $a_{ij}$<\/math>\n \n <\/paragraph>\n<\/document>\n" } ] } diff --git a/tests/testthat/fixtures/example-equations-footnote/api/translate-37ec3f-POST.json b/tests/testthat/fixtures/example-equations-footnote/api/translate-37ec3f-POST.json new file mode 100644 index 0000000..e405299 --- /dev/null +++ b/tests/testthat/fixtures/example-equations-footnote/api/translate-37ec3f-POST.json @@ -0,0 +1,8 @@ +{ + "translations": [ + { + "detected_source_language": "FR", + "text": "\n\n\n \n See <\/text>\n $a$<\/math>\n \n an equation with a note afterwards <\/text>\n \n ^1<\/notranslate>\n <\/squary>\n .<\/text>\n <\/text>\n <\/paragraph>\n \n \n ^1<\/notranslate>:<\/squary>\n a footnote.<\/text>\n <\/text>\n <\/paragraph>\n<\/document>\n" + } + ] +} 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 deleted file mode 100644 index 4f8bf0a..0000000 --- a/tests/testthat/fixtures/example-equations-footnote/api/translate-746e3b-POST.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "translations": [ - { - "detected_source_language": "FR", - "text": "\n\n\n \n See <\/text>\n $a$<\/math>\n \n an equation with a note afterwards <\/text>\n \n ^1<\/notranslate>\n <\/squary>\n .<\/text>\n <\/text>\n <\/paragraph>\n \n \n ^1<\/notranslate>:<\/squary>\n a footnote.<\/text>\n <\/text>\n <\/paragraph>\n<\/document>\n" - } - ] -} diff --git a/tests/testthat/fixtures/example-equations/api/translate-a9b85c-POST.json b/tests/testthat/fixtures/example-equations/api/translate-a9b85c-POST.json new file mode 100644 index 0000000..6a03a02 --- /dev/null +++ b/tests/testthat/fixtures/example-equations/api/translate-a9b85c-POST.json @@ -0,0 +1,8 @@ +{ + "translations": [ + { + "detected_source_language": "EN", + "text": "\n\n\n \n Algo de texto.<\/text>\n <\/paragraph>\n \n $$<\/math>\n \n E = m \\times c^2<\/math>\n \n $$<\/math>\n <\/paragraph>\n \n ¿Te parece bonita esta ecuación? Observa que <\/text>\n $\\alpha=1$<\/math>\n y <\/text>\n $b$<\/math>\n no está definida.<\/text>\n <\/paragraph>\n \n \n $i_t = j_t$<\/math>\n \n <\/paragraph>\n<\/document>\n" + } + ] +} diff --git a/tests/testthat/fixtures/example-equations/api/translate-f741d7-POST.json b/tests/testthat/fixtures/example-equations/api/translate-f741d7-POST.json deleted file mode 100644 index 70fa605..0000000 --- a/tests/testthat/fixtures/example-equations/api/translate-f741d7-POST.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "translations": [ - { - "detected_source_language": "EN", - "text": "\n\n\n \n Algo de texto.<\/text>\n <\/paragraph>\n \n $$<\/text>\n \n E = m \\times c^2<\/math>\n \n $$<\/text>\n <\/paragraph>\n \n ¿A que es bonita esta ecuación? Observa que <\/text>\n $\\alpha=1$<\/math>\n y <\/text>\n $b$<\/math>\n no está definida.<\/text>\n <\/paragraph>\n \n \n $i_t = j_t$<\/math>\n \n <\/paragraph>\n<\/document>\n" - } - ] -}