knitr icon indicating copy to clipboard operation
knitr copied to clipboard

double quotes in restructuredText figure captions erroneously being converted to html

Open trevorld opened this issue 2 years ago • 6 comments

I've recently noticed in my knitted README and blog posts that double quoted figure captions in restructuredText documents are now being erroneously converted to html in recent versions of {knitr}. {knitr} didn't used to do that so this seems to be new {knitr} bug introduced sometime "recently".

Contents of minimal example test.Rrst:

A simple restructuredText example
=================================

.. {r plot, fig.cap='A figure caption with \'single\' or "double" quotes'}
plot(1:5, rnorm(5))
.. ..

when knit by knitr::knit("test.Rrst") becomes:

A simple restructuredText example
=================================


.. sourcecode:: r
    

    plot(1:5, rnorm(5))

.. figure:: figure/plot-1.png
    :alt: A figure caption with 'single' or "double" quotes

    A figure caption with 'single' or "double" quotes

Instead of "double" we should instead see "double" in both places it appears in the knit .rst document.

Thanks!

xfun::session_info()
R version 4.1.0 (2021-05-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.2 LTS

Locale:
  LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
  LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
  LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
  LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   

Package version:
  askpass_1.1       brew_1.0.6        brio_1.1.2        cachem_1.0.5     
  callr_3.7.0       cli_3.0.0         clipr_0.7.1       commonmark_1.7   
  compiler_4.1.0    crayon_1.4.1      credentials_1.3.0 curl_4.3.2       
  desc_1.3.0        devtools_2.4.1    diffobj_0.3.4     digest_0.6.27    
  ellipsis_0.3.2    evaluate_0.14     fansi_0.5.0       fastmap_1.1.0    
  fs_1.5.0          gert_1.3.0        gh_1.3.0          gitcreds_0.1.1   
  glue_1.4.2        graphics_4.1.0    grDevices_4.1.0   highr_0.9        
  httr_1.4.2        ini_0.3.1         jsonlite_1.7.2    knitr_1.33.7     
  lifecycle_1.0.0   magrittr_2.0.1    markdown_1.1      memoise_2.0.0    
  methods_4.1.0     mime_0.11         openssl_1.4.4     parallel_4.1.0   
  pillar_1.6.1      pkgbuild_1.2.0    pkgconfig_2.0.3   pkgload_1.2.1    
  praise_1.0.0      prettyunits_1.1.1 processx_3.5.2    ps_1.6.0         
  purrr_0.3.4       R6_2.5.0          rappdirs_0.3.3    rcmdcheck_1.3.3  
  Rcpp_1.0.6        rematch2_2.1.2    remotes_2.3.0     rlang_0.4.11     
  roxygen2_7.1.1    rprojroot_2.0.2   rstudioapi_0.13   rversions_2.1.0  
  sessioninfo_1.1.1 stats_4.1.0       stringi_1.6.2     stringr_1.4.0    
  sys_3.4           testthat_3.0.3    tibble_3.1.2      tools_4.1.0      
  usethis_2.0.1     utf8_1.2.1        utils_4.1.0       vctrs_0.3.8      
  waldo_0.2.5       whisker_0.4       withr_2.4.2       xfun_0.24        
  xml2_1.3.2        xopen_1.0.0       yaml_2.2.1        zip_2.1.1    

By filing an issue to this repo, I promise that

  • [X] I have fully read the issue guide at https://yihui.org/issue/.
  • [X] I have provided the necessary information about my issue.
    • If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('knitr'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('yihui/knitr').
  • [X] I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

trevorld avatar Jul 07 '21 20:07 trevorld

Thanks for the report.

A git bisect leads me to the issue being introduced by 17a64ff10e9f97697178bf79de279aa668def830 (#1900)

This PR touches fig.alt but for HTML output mainly. Something must have introduced the regression. I'll look into this.

cderv avatar Jul 08 '21 14:07 cderv

And issue is from here: https://github.com/yihui/knitr/blob/3237add034368a3018ff26fa9f4d0ca89a4afd78/R/hooks-html.R#L59

We should escape really only when the output format supports it.

cderv avatar Jul 08 '21 14:07 cderv

However, this happens because the caption for rst output uses .img.cap(..., alt = "TRUE") Maybe there is something to change in how it is handled for rst format

https://github.com/yihui/knitr/blob/3237add034368a3018ff26fa9f4d0ca89a4afd78/R/hooks-rst.R#L5-L19

I don't know a lot about asciidoc yet. Is there a way to add alt text for figures ?

cderv avatar Jul 08 '21 14:07 cderv

I don't know a lot about asciidoc yet. Is there a way to add alt text for figures ?

I've never used asciidoc but the following documentation says to add an alt text then simply put it in between the square brackets of the image macro: https://docs.asciidoctor.org/asciidoc/latest/macros/images/

trevorld avatar Jul 08 '21 14:07 trevorld

Sorry I just for mixed up. rst is for restructuredText not asciidoc. This is another hook for asciidoc (hook_plot_asciidoc, here we are talking about hook_plot_rst() above)

I see how I can make rst support the new fig.alt option because it should supports it I guess as you can separated :alt: from caption

cderv avatar Jul 08 '21 15:07 cderv

I see how I can make rst support the new fig.alt option because it should supports it I guess as you can separated :alt: from caption

Correct. In a rst "figure" directive you can specify a separate :alt: option from the caption paragraph. Here is the official rst "figure" documentation: https://docutils.sourceforge.io/docs/ref/rst/directives.html#figure e.g.

.. figure:: figure/plot-1.png
    :alt: This is the figure "alt" text option

    This is the figure caption (paragraph)

trevorld avatar Jul 08 '21 15:07 trevorld

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

github-actions[bot] avatar Feb 14 '24 05:02 github-actions[bot]