Skip to content

Commit

Permalink
refactor: use dev tinkr
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed May 24, 2024
1 parent a60a177 commit bca001d
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 6 additions & 4 deletions R/translate.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----
Expand Down
66 changes: 66 additions & 0 deletions tests/testthat/_snaps/translate.new.md
Original file line number Diff line number Diff line change
@@ -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}$"

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"translations": [
{
"detected_source_language": "FR",
"text": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n<document xmlns=\"http://commonmark.org/xml/1.0\">\n <paragraph>\n <text xml:space=\"preserve\"/>\n <math asis=\"true\">$a_{ij}$<\/math>\n <text/>\n <\/paragraph>\n<\/document>\n"
"text": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n<document xmlns=\"http://commonmark.org/xml/1.0\">\n <paragraph>\n <text xml:space=\"preserve\"/>\n <math asis=\"true\" math=\"true\">$a_{ij}$<\/math>\n <text/>\n <\/paragraph>\n<\/document>\n"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"translations": [
{
"detected_source_language": "FR",
"text": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n<document xmlns=\"http://commonmark.org/xml/1.0\">\n <paragraph>\n <text xml:space=\"preserve\">See <\/text>\n <math asis=\"true\" math=\"true\">$a$<\/math>\n <text xml:space=\"preserve\">\n <text xml:space=\"preserve\"> an equation with a note afterwards <\/text>\n <squary>\n <notranslate>^1<\/notranslate>\n <\/squary>\n <text>.<\/text>\n <\/text>\n <\/paragraph>\n <paragraph>\n <text xml:space=\"preserve\">\n <squary><notranslate>^1<\/notranslate>:<\/squary>\n <text> a footnote.<\/text>\n <\/text>\n <\/paragraph>\n<\/document>\n"
}
]
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"translations": [
{
"detected_source_language": "EN",
"text": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE document SYSTEM \"CommonMark.dtd\">\n<document xmlns=\"http://commonmark.org/xml/1.0\">\n <paragraph>\n <text xml:space=\"preserve\">Algo de texto.<\/text>\n <\/paragraph>\n <paragraph>\n <math xml:space=\"preserve\" asis=\"true\" math=\"true\">$$<\/math>\n <math asis=\"true\" math=\"true\"/>\n <math xml:space=\"preserve\" asis=\"true\" math=\"true\">E = m \\times c^2<\/math>\n <math asis=\"true\" math=\"true\"/>\n <math xml:space=\"preserve\" asis=\"true\" math=\"true\">$$<\/math>\n <\/paragraph>\n <paragraph>\n <text xml:space=\"preserve\">¿Te parece bonita esta ecuación? Observa que <\/text>\n <math asis=\"true\" math=\"true\">$\\alpha=1$<\/math>\n <text> y <\/text>\n <math asis=\"true\" math=\"true\">$b$<\/math>\n <text> no está definida.<\/text>\n <\/paragraph>\n <paragraph>\n <text xml:space=\"preserve\"/>\n <math asis=\"true\" math=\"true\">$i_t = j_t$<\/math>\n <text/>\n <\/paragraph>\n<\/document>\n"
}
]
}

This file was deleted.

0 comments on commit bca001d

Please sign in to comment.