Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: styles for code blocks #738

Closed
wants to merge 1 commit into from
Closed

Conversation

atusy
Copy link
Collaborator

@atusy atusy commented Jul 4, 2019

This PR improves styles for code blocks.

html_document2 / html_chapter / html_book

This part is related to #733 and #706.

When Pandoc's syntax highlighting is enabled and clean_highlight_tags: TRUE, Pandoc embeds CSS and cause code blocks to have no margins. Exceptionally, the matter does not happen when highlight: default in html_document2

div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }

For the html_document2, Pandoc embeds CSS starting from <style type="text/css" data-origin="pandoc">.
Thus, I decided to remove unwanted styles by updating clean_pandoc2_tags.

For the others, Pandoc embeds CSS starting from <style type="text/css">, which cannot be telled if the CSS is added by Pandoc or the user.
Thus, I decided to add CSS to template (default.html) to override the one embedded by Pandoc.
I also updated CSS in the similar way as rstudio/rmarkdown#1596, so that output code blocks have white background when line numbering is enabled.

Before

image

After

image

html_gitbook

When line nubering is enabled for code blocks, they are positioned too left, and may overflow.
Thus I updated html_gitbook's template.

Before

image

After

image

Source code for examples above

The above output is rendered from the source code below with

---
output: 
  bookdown::html_book: 
    highlight: default
  bookdown::html_document2:
    highlight: tango
  bookdown::gitbook: 
    highlight: tango
---

Numbered

```{r hello, class.source = "numberLines", class.output="numberLines", attr.output='startFrom="10"'}
"Hello World"
```

Unnumbered

```{r hello2}
"Hello World"
```

@atusy
Copy link
Collaborator Author

atusy commented Jul 4, 2019

For the html_document2, Pandoc embeds CSS starting from <style type="text/css" data-origin="pandoc">.
Thus, I decided to remove unwanted styles by updating clean_pandoc2_tags.

This approach can be vulnerable to the updates of Pandoc.
Another solution is to add external CSS by using htmltools::html_dependency.
However, this makes output html complex and heavier.
Updating the template is not preferred because html_document2 shares the template with rmarkdown::html_document.

@atusy
Copy link
Collaborator Author

atusy commented Jul 6, 2019

I close this PR and come back late because I want some rethinking.

  • For html_document2, 52b71f9 removed clean_pandoc2_highlight_tags
  • For html_book and gitbook, I feel I have better places to embed additional CSS

@atusy atusy closed this Jul 6, 2019
pre[class="sourceCode numberSource lineAnchors numberLines"],
pre[class="sourceCode numberSource lineAnchors number-Lines"],
pre[class="sourceCode numberSource line-anchors numberLines"],
pre[class="sourceCode numberSource line-anchors number-lines"] {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of enumerating all classes with the CSS selector [] (which seems to be cumbersome), will something like .numberLines work?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this seems to be cumbersome.
However, if

pre.numberLines { background-color: white; }

is applied, then we cannot use chunk option and bootstrap's CSS (e.g., output.classs='.bg-warning') to change background color of output.

https://github.com/yihui/knitr-examples/blob/b6937427954e4bceafd9a112237d1aa80215cd2d/116-html-class.Rmd#L16

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants