Skip to content

Commit

Permalink
refactor: use tinkr::get_protected() (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle authored May 28, 2024
1 parent a60a177 commit 8cce747
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 22 deletions.
21 changes: 16 additions & 5 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 Expand Up @@ -379,10 +381,19 @@ unprotect_curly <- function(curly) {
}

protect_math <- function(math) {
if (xml2::xml_name(math) == "softbreak") {
xml2::xml_attr(math, "softbreak") <- "yes"
}

xml2::xml_name(math) <- "math"

}
unprotect_math <- function(math) {
xml2::xml_name(math) <- "text"
if (xml2::xml_has_attr(math, "softbreak")) {
xml2::xml_name(math) <- "softbreak"
} else {
xml2::xml_name(math) <- "text"
}
}
protect_squaries <- function(node) {
text <- xml2::xml_text(node)
Expand Down
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\" softbreak=\"yes\"/>\n <math xml:space=\"preserve\" asis=\"true\" math=\"true\">E = m \\times c^2<\/math>\n <math asis=\"true\" math=\"true\" softbreak=\"yes\"/>\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 8cce747

Please sign in to comment.