Incorrect rendering of comparison generics `<`, `>`, etc.
The usage section of the html docs for S4 method implementations of comparison generics incorrectly renders < and > with < and >
Minimal reproducible package example here: https://github.com/bnprks/pkgdownbug (incorrectly rendered reference here)
An R file with text:
#' Comparison methods
#'
#' Generic methods and built-in functions for MyType objects
#'
#' @name ComparisonMethods
#' @rdname ComparisonMethods
NULL
setClass("MyType")
#' @describeIn ComparisonMethods Less than comparison
setMethod("<", signature(e1= "MyType", e2= "numeric"), function(e1, e2) {return(TRUE)})
#' @describeIn ComparisonMethods Less than comparison
setMethod("<=", signature(e1= "MyType", e2= "numeric"), function(e1, e2) {return(TRUE)})
#' @describeIn ComparisonMethods Less than comparison
setMethod(">", signature(e1= "MyType", e2= "numeric"), function(e1, e2) {return(TRUE)})
#' @describeIn ComparisonMethods Less than comparison
setMethod(">=", signature(e1= "MyType", e2= "numeric"), function(e1, e2) {return(TRUE)})
gets a usage section rendered to html as follows:
# S4 method for MyType,numeric
<(e1, e2)
# S4 method for MyType,numeric
<=(e1, e2)
# S4 method for MyType,numeric
>(e1, e2)
# S4 method for MyType,numeric
>=(e1, e2)
Thanks for reporting! I don't have time to investigate right now but it looks related to #2286
It's already using {{{ }}} in the template, so it must be getting escaped elsewhere 🤔
Ok, at least two problems here:
- The
escape_html()in the fallback forhighlight_text()is (I think) generally unnecessary becauseflatten_text()callsas_html()which already escapes text (will need to double check this) - We're only hitting that fallback because we're generating invalid R code because we're not handling methods for infix functions correctly.
And there's yet another bug in the way we're detecting whether or not a function is infix.
Ok, the root problem is that the Rd looks like this:
\S4method{<}{MyType,numeric}(e1, e2)
and we want to generate a usage like this:
# S4 method for MyType,numeric
e1 < e2
In particular, note that (e1, e2) is not the call to \S4method{} so we can't the existing Rd tokenization to help us solve this problem.
It looks like base R effectively uses a hand-rolled tokeniser to solve this problem so we'll need to implement something similar in pkgdown.
A couple of test cases for when I take a stab at this:
test_that("infix methods parsed correctly", {
out <- rd2html("\\S3method{<}{foo}(x, y)")
expect_equal(out[[2]], "x < y")
out <- rd2html("\\S4method{<}{foo}(x, y)")
expect_equal(out[[2]], "x < y")
})
After PR motivating example looks like this: