Skip to content

Commit

Permalink
fix yihui/litedown#34: allow () around math expressions (outside doll…
Browse files Browse the repository at this point in the history
…ar signs)
  • Loading branch information
yihui committed Oct 10, 2024
1 parent 1a57975 commit a4dd449
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: xfun
Type: Package
Title: Supporting Functions for Packages Maintained by 'Yihui Xie'
Version: 0.48.4
Version: 0.48.5
Authors@R: c(
person("Yihui", "Xie", role = c("aut", "cre", "cph"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666")),
person("Wush", "Wu", role = "ctb"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- `protect_math()` will ignore `$ $` if there are backticks after the opening `$` or before the closing `$`, e.g., ``$`this is not math`$``.

- `protect_math()` allows for parentheses `()` around math expressions now, e.g., `($x$)` was previously not recognized but is recognized now (thanks, @AlbertLei, yihui/litedown#34).

- `html_escape()` will not escape double quotes (i.e., convert `"`" to `"`) by default, and the conversion will be done only for `html_escape(attr = TRUE)`.

- Added an argument `start` to `make_fence()`.
Expand Down
13 changes: 7 additions & 6 deletions R/markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ prose_index = function(x, warn = TRUE) {
#'
#' Expressions in pairs of dollar signs or double dollar signs are treated as
#' math, if there are no spaces after the starting dollar sign, or before the
#' ending dollar sign. There should be spaces before the starting dollar sign,
#' unless the math expression starts from the very beginning of a line. For a
#' pair of single dollar signs, the ending dollar sign should not be followed by
#' a number. With these assumptions, there should not be too many false
#' ending dollar sign. There should be a space or `(` before the starting dollar
#' sign, unless the math expression starts from the very beginning of a line.
#' For a pair of single dollar signs, the ending dollar sign should not be
#' followed by a number, and the inner math expression should not be wrapped in
#' backticks. With these assumptions, there should not be too many false
#' positives when detecing math expressions.
#'
#' Besides, LaTeX environments (\verb{\begin{*}} and \verb{\end{*}}) are also
Expand Down Expand Up @@ -90,15 +91,15 @@ protect_math = function(x, token = '', use_block = FALSE) {

escape_math = function(x, token = '', use_block = FALSE) {
# replace $x$ with `\(x\)` (protect inline math in <code></code>)
m = gregexpr('(?<=^|[\\s])[$](?![ `])[^$]+?(?<![ `])[$](?![$0123456789])', x, perl = TRUE)
m = gregexpr('(?<=^|[\\s(])[$](?![ `])[^$]+?(?<![ `])[$](?![$0123456789])', x, perl = TRUE)
regmatches(x, m) = lapply(regmatches(x, m), function(z) {
if (length(z) == 0) return(z)
z = sub('^[$]', paste0('`', token, '\\\\('), z)
z = sub('[$]$', paste0('\\\\)', token, '`'), z)
z
})
# replace $$x$$ with `$$x$$` (protect display math)
m = gregexpr('(?<=^|[\\s])[$][$](?! )[^$]+?(?<! )[$][$]', x, perl = TRUE)
m = gregexpr('(?<=^|[\\s(])[$][$](?! )[^$]+?(?<! )[$][$]', x, perl = TRUE)
regmatches(x, m) = lapply(regmatches(x, m), function(z) {
if (length(z) == 0) return(z)
paste0('`', token, z, token, '`')
Expand Down
9 changes: 5 additions & 4 deletions man/protect_math.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/test-cran/test-markdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ assert('prose_index() works', {
assert('protect_math() puts inline math expressions in backticks', {
(protect_math('$x$') %==% '`\\(x\\)`')
(protect_math('$x$', '===') %==% '`===\\(x\\)===`')
(protect_math('($x$)') %==% '(`\\(x\\)`)')
(protect_math(' $x$') %==% ' `\\(x\\)`')
(protect_math('.$x$') %==% '.$x$')
(protect_math('$`x`$') %==% '$`x`$')
(protect_math('hi $x$ a') %==% 'hi `\\(x\\)` a')
(protect_math('$ a $') %==% '$ a $')
(protect_math(' $a$') %==% ' `\\(a\\)`')
Expand All @@ -63,6 +67,9 @@ assert('protect_math() puts inline math expressions in backticks', {

(protect_math('$$a$$') %==% '`$$a$$`')
(protect_math('$$a$') %==% '$$a$')
(protect_math('($$a$$)') %==% '(`$$a$$`)')
(protect_math(' $$a$$') %==% ' `$$a$$`')
(protect_math('.$$a$$') %==% '.$$a$$')
(protect_math('hi $$\alpha$$') %==% 'hi `$$\alpha$$`')
(protect_math('hi $$\alpha $$') %==% 'hi $$\alpha $$')
(protect_math('hi $$ \alpha$$') %==% 'hi $$ \alpha$$')
Expand Down

0 comments on commit a4dd449

Please sign in to comment.