Skip to content

Commit

Permalink
fix: account for more citations formats
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Apr 5, 2024
1 parent 5eb37cb commit eb8ced0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion R/translate.R
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,9 @@ protect_squaries <- function(node) {
# hack to preserve colon that DeepL API kills
text <- gsub("</squary><text>:", ":</squary><text>", text)

at_things <- regmatches(text, gregexpr("@[[:alnum:]]*", text))[[1]]
# https://rstudio.github.io/visual-markdown-editing/citations.html#inserting-citations
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)),
Expand Down
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\">\n <text xml:space=\"preserve\">Wickham dice bla <\/text>\n <squary>-@wickham+2015#<\/squary>\n <text> y dice otras cosas<\/text>\n <squary>\n <notranslate>^footnote1<\/notranslate>\n <\/squary>\n <text>.<\/text>\n <\/text>\n <softbreak/>\n <text xml:space=\"preserve\">\n <text xml:space=\"preserve\">Bla bla bla <\/text>\n <squary>véase <notranslate>@knuth1984<\/notranslate>, pp. 33-35; también <notranslate>@wickham2015<\/notranslate><\/squary>, <squary>cap. 1<\/squary>\n <text/>\n <\/text>\n <\/paragraph>\n<\/document>\n"
}
]
}
4 changes: 2 additions & 2 deletions tests/testthat/test-translate.R
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ test_that("deepl_translate() translate fig-alt", {
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].
"Wickham says blah [-@wickham+2015#] and he says other things[^footnote1].
Blah Blah [see @knuth1984, pp. 33-35; also @wickham2015, chap. 1]",
source_lang = "en",
target_lang = "es"
)
expect_match(lines, "\\[\\^footnote1\\]")
expect_match(lines, "\\[-@wickham2015]")
expect_match(lines, "\\[-@wickham\\+2015\\#]")
expect_match(lines, "también")
})
})
Expand Down

0 comments on commit eb8ced0

Please sign in to comment.