atusy
April 19, 2019, 1:13am
1
I found line numbers on code blocks in rmarkdown::html_document
is possible by
giving YAML front matter with highlight: pygment
and chunk options with class.source = "numberLines"
(Number lines in code blocks with Rmarkdown )
However, this is not the case for bookdown::html_document2
and because bookdown:::clean_pandoc2_highlight_tags
removes tags required for numbering.
Does anybody know why this process is required?
# remove the <div> tags around <pre>, and clean up <a> on all lines
clean_pandoc2_highlight_tags = function(x) {
if (!pandoc2.0()) return(x)
x = gsub('(</a></code></pre>)</div>', '\\1', x)
x = gsub('<div class="sourceCode"[^>]+>(<pre)', '\\1', x)
x = gsub('<a class="sourceLine"[^>]+>(.*)</a>', '\\1', x)
x
}
Example
A following screenshot is created by html code below.
<div class="sourceCode" id="cb15"><pre class="sourceCode numberSource r numberLines"><code class="sourceCode r"><a class="sourceLine" id="cb15-1" title="1">x <-<span class="st"> </span><span class="kw">seq</span>(<span class="dv">10</span>)</a>
<a class="sourceLine" id="cb15-2" title="2"><span class="kw">mean</span>(x)</a></code></pre></div>
Embedding the above html code directory in .Rmd
file and kniting as bookdown:::html_document2
results in
<div id="cb15" class="sourceCode">
<pre class="sourceCode numberSource r numberLines"><code class="sourceCode r">x <-<span class="st"> </span><span class="kw">seq</span>(<span class="dv">10</span>)
<span class="kw">mean</span>(x)</code></pre>
</div>
which is rendered on browser as
cderv
April 20, 2019, 11:56am
2
I think This process is required in bookdown so that another syntax higlighting that pandoc could be applied. (Gitbook I guess). This function has been created for use in split_chapter
internal fonction but has been added last October in html_document2
committed 06:12PM - 05 Oct 18 UTC
It is difficult to know why when you are not the developper
I think you can open an issue and ask about that and if an argument could be added to optionaly choose to use pandoc syntax highlighting with html_document2
if one wants to. We'll see if the current maintainer of bookdown has the answer to why.
atusy
April 21, 2019, 5:45am
3
Thank you for the answer.
I opened an issue, and hope my feature request will be accepted.
opened 05:44AM - 21 Apr 19 UTC
closed 03:46PM - 01 May 19 UTC
By filing an issue to this repo, I promise that
- [x] I have fully read the i… ssue guide at https://yihui.name/issue/.
- [x] I have provided the necessary information about my issue.
- If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
- If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included `xfun::session_info('bookdown')`. 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('rstudio/bookdown')`.
- If I have posted the same issue elsewhere, I have also mentioned it in this issue.
- [x] I have learned the Github Markdown syntax, and formatted my issue correctly.
<!--
Please keep the above portion in your issue. Your issue will be closed if any of the above boxes is not checked. In certain (rare) cases, you may be exempted if you give a brief explanation (e.g., you are only making a suggestion for improvement). Thanks!
-->
In `html_document2`, I faced a failure in line numbering on code blocks powered by pandoc, and found this is caused by `clean_pandoc2_highlight_tags`.
I asked reasons for the cleaning on RStudio Community, and I got an answer it is probably related to GitBook (https://community.rstudio.com/t/number-lines-on-code-blocks-in-bookdown-htmld-document2-fails-because-of-bookdown-clean-pandoc2-highlight-tags/28985/2).
If this is the case, and cleaning up is not always required, I want an option to toggle the cleaning (e.g., by YAML front matter?).
This feature request enhances other packages depending on `bookdown::html_document2` such as `pagedown` and `blogdown`.
I'd be verry happy if my suggestion is accepted.
# Reproducible example
Please knit followings in `html_document` and `html_document2`.
The latter will remove the line numbering on code blocks.
````
---
output:
html_document:
highlight: pygments
bookdown::html_document2:
highlight: pygments
---
```{r, class.source = "numberLines lineAnchors", eval = FALSE}
iris
```
````
# `xfun::session_info('bookdown')`
```
R version 3.5.3 (2019-03-11)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch), RStudio 1.2.1335
Locale:
LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=C
LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
Package version:
base64enc_0.1.3 bookdown_0.9 digest_0.6.18 evaluate_0.13 glue_1.3.1 graphics_3.5.3 grDevices_3.5.3 highr_0.8 htmltools_0.3.6 jsonlite_1.6 knitr_1.22
magrittr_1.5 markdown_0.9 methods_3.5.3 mime_0.6 Rcpp_1.0.1 rmarkdown_1.12 stats_3.5.3 stringi_1.4.3 stringr_1.4.0 tinytex_0.11 tools_3.5.3
utils_3.5.3 xfun_0.6 yaml_2.2.0
```
system
Closed
May 23, 2019, 1:22am
5
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.