pkgdown icon indicating copy to clipboard operation
pkgdown copied to clipboard

In section 'Value', lines starting or ending with a command cause starting new paragraphs

Open GeoBosh opened this issue 2 years ago • 7 comments

Consider this paragraph in the 'value' section of an Rd file:

\value{
  Note that \code{xxx}
  by default drops
  \code{yyy}, etc.
}

It renders to

Note that xxx

by default drops

yyy, etc.

It seems that lines starting or ending with a command cause starting new paragraphs.

This issue is somewhat troublesome as one can only see it by examining each page on the generated website.

GeoBosh avatar Nov 17 '23 15:11 GeoBosh

I get the line break after xxx, but not before yyy. (The first line is wrapped in <p></p> in the output.) I didn't have any white space at the start or end of the lines; maybe that's a difference, or different versions of whatever package does the Rd rendering.

dmurdoch avatar Nov 17 '23 16:11 dmurdoch

Indeed, after removing the indent before \code{yyy}, as you suggested:

\value{
  Note that \code{xxx}
  by default drops
\code{yyy}, etc.
}

there is no break before yyy:

Note that xxx

by default drops yyy, etc.

GeoBosh avatar Nov 17 '23 17:11 GeoBosh

I confirm there's a problem with code-formatted text if it is at the end of a line in the Rd file.

Adding another example:

Rd file has:

\value{
Some text
in a first paragraph.

Some text in a second paragraph.
Another sentence in the same paragraph.
Some \code{code formatting} inside an Rd line is no problem.

Another paragraph.
A sentence with \code{code formatting}
at the end of a line in the Rd document.
It should not split this third paragraph.
}

Result:

afbeelding

florisvdh avatar Nov 22 '23 17:11 florisvdh

May be related to work done in solving https://github.com/r-lib/pkgdown/issues/2029.

florisvdh avatar Nov 22 '23 17:11 florisvdh

This function looks like the culprit:

https://github.com/r-lib/pkgdown/blob/5a0a31f89d7c839c04a929844e4b7216196189ca/R/rd-data.R#L90-L112

It assumes that whitespace (including \n) following an item is a paragraph break, but the \code{} macro becomes an item with no newline after.

I think adding something after is_ws is computed should fix it:

  # Drop pure whitespace nodes between items
  is_ws <- purrr::map_lgl(x, is_whitespace)

  # but not if they are preceded by a non-text macro
  is_text <- purrr::map_lgl(x, inherits, "TEXT")
  is_ws <- is_ws & c(TRUE, is_text[-length(is_text)])

dmurdoch avatar Nov 22 '23 20:11 dmurdoch

Minimal reprex:

pkgdown:::value2html("Note that \\code{xxx}
  by default drops
  \\code{yyy}, etc.
")
#> [1] "<p>Note that <code>xxx</code></p>" ""                                 
#> [3] ""                                  "<p>by default drops</p>"          
#> [5] "<p></p>"                           "<p><code>yyy</code>, etc.</p>"

pkgdown:::value2html("Note that \\code{xxx}
by default drops
\\code{yyy}, etc.
")
#> [1] "<p>Note that <code>xxx</code></p>" ""                                 
#> [3] ""                                  "<p>by default drops"              
#> [5] "<code>yyy</code>, etc.</p>"

Created on 2024-04-11 with reprex v2.1.0

hadley avatar Apr 11 '24 21:04 hadley

@dmurdoch I tried that approach and while it solves this problem, it breaks some of the other existing tests.

Do you happen to have a more precise definition of how this is supposed to work? Is there existing base R code that I could look at?

hadley avatar Apr 16 '24 19:04 hadley

Ok, this got way easier when I stopped trying to write clever vectorised code 😆

hadley avatar Jun 03 '24 22:06 hadley