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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions R/html.R
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,16 @@ clean_pandoc2_highlight_tags = function(x) {
x = gsub('(</code></pre>)</div>', '\\1', x)
x = gsub('<div class="sourceCode"[^>]+>(<pre)', '\\1', x)
x = gsub('<a class="sourceLine"[^>]+>(.*)</a>', '\\1', x)

css_start = which(x == '<style type="text/css" data-origin="pandoc">')
style_end = which(x == '</style>')
css_end = vapply(css_start, function(x) style_end[x < style_end][1L], 1L)
css_range = intersect(
unlist(Map(seq, css_start, css_end)),
which(x %in% c("div.sourceCode { margin: 1em 0; }", "pre.sourceCode { margin: 0; }"))
)
if (length(css_range) > 0) x = x[-css_range]

x
}

Expand Down
17 changes: 17 additions & 0 deletions inst/templates/default.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@
margin-left: auto;
margin-right: auto;
}
pre:not([class]),
pre[class="sourceCode numberSource numberLines"],
pre[class="sourceCode numberSource numberLines lineAnchors"],
pre[class="sourceCode numberSource numberLines line-anchors"],
pre[class="sourceCode numberSource number-lines"],
pre[class="sourceCode numberSource number-lines lineAnchors"],
pre[class="sourceCode numberSource number-lines line-anchors"],
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

background-color: white;
}
pre.sourceCode {
margin-top: 10px;
margin-bottom: 10px;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
Expand Down
6 changes: 6 additions & 0 deletions inst/templates/gitbook.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
</style>
$endif$

<style type="text/css">
.numberLines code.sourceCode {
left: 1.5em;
}
</style>

$for(css)$
<link rel="stylesheet" href="$css$" $if(html5)$$else$type="text/css" $endif$/>
$endfor$
Expand Down