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
WIP: fix rmarkdown regarding new theme behaviour in pandoc #1489
Conversation
Perhaps we should check if the parent node is
If we are inside |
Thanks for pointing this out. |
Small customisation pandoc css introduce div.sourceCode {
margin-top: 1em;
margin-right: 0px;
margin-bottom: 1em;
margin-left: 0px;
} As we insert div.row {
/* to reduce the space introduce in pandoc div.sourceCode */
margin-bottom: -0.8em;
} 🤔 not sure of that change... |
@@ -45,7 +45,9 @@ window.initializeCodeFolding = function(show) { | |||
buttonCol.append(showCodeButton); | |||
buttonRow.append(buttonCol); | |||
|
|||
div.before(buttonRow); | |||
var sourceCodeBlock = $('div.sourceCode'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this also work with Pandoc 1.9.x? i.e. Does Pandoc 1.9 also generate div.sourceCode
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is part of the test I need to make. Yes pandoc 1.9.x have a div.sourceCode
but the css applied to those part have changed.
So yes this line work and the button is added above without issue, but the margin trick does not work. Will change that.
Also the main fix
pre.sourceCode {
color: inherit;
background-color: transparent;
}
does not work with pandoc 1.9 because in this pandoc version the background is applied to pre.sourceCode
so it erase it... -
if we want something that work with old and new, I need to find another one... 😕
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok - there is not div.sourceCode
inserted if we use highlight = 'default'
, whatever the version of pandoc. This is because highlight default
and textmate
does not use pandoc but uses highlightjs
and this does not insert the div. I did not know about that and I need to deal with that... More complicated that I thought 🤔
@yihui here is a proposition to make the fix conditional to pandoc > 2. As the issue is due to change from pandoc 2, we can activate a variable for pandoc template based on the version. This way the changes are only applied if used with pandoc 2. This was for me the more obvious fix right now, rather than going with css mastery. (I did not find one that could work both with pandoc < 2 and pandoc > 2. What do you think ? |
I just found that some |
this should now work. I will try to test thoroughly. You can give it a try also.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds like there will be more work to do... :(
inst/rmd/h/default.html
Outdated
$if(ispandoc2)$ | ||
pre.sourceCode { | ||
color: inherit; | ||
background-color: transparent; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually this will break themes that don't have a background color, such as pygments
#654 (comment) Such themes should inherit the background color from Bootstrap's pre
.
My head is starting to hurt...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it was not clear to me and I wanted to test it further. Thanks for the confirmation. This blog post shows the result of pandoc styling, and indeed pygment as a white background, but with bootstrap it is not the case. As we need to
- override the
pre
style of bootstrap because pandoc apply the background color and color to the parent - inherits for the
pre
style of bootstrap to keep thepre
css rule from bootstrap for theme that don't have background and color
I think we should also do something conditional.
Of pandoc highlighting style, we have frome the src code
pygments
with nothing ➡️ should inherit boostrap color and background-colortango
with no default color ➡️ should inherit boostrap color onlyhaddock
is likepygments
monochrome
is likepygments
I'll come up with something... 🤔 and yes 🤯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made a proposition by manually setting which style has background and color or not. Then with two more template variable, the correct rule is activated.
In the same idea, with pandoc2 we can do something more dynamic. I hesitate to introduce this in rmarkdown but it could be interesting. We can get highlight information with
if (pandoc2.0()) {
arg <- c("--print-highlight-style", pandoc_highlight)
json_res <- system2(pandoc(), arg, stdout = TRUE)
properties <- jsonlite::fromJSON(paste0(json_res, collapse = "\n"))
}
we then get for pygments
> properties$`background-color`
NULL
> properties$`text-color`
NULL
and for tango
> properties$`text-color`
NULL
> properties$`background-color`
[1] "#f8f8f8"
We could introduce a function get_higligthing_styles
in rmarkdown (that we could export) and use this here to test if is.null
or not. This could even mean that we could fill in the value in
pre.sourceCode
css rule, instead of inherits or transparent
I run this by you before pushing anything. I have a working code locally though - I can share then revert.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in fact you can see it in another branch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought for another hour on this, and I tend to use JavaScript to replace div.sourceCode{...}
with pre.sourceCode{...}
in CSS if div.sourceCode
contains rules for color
or background-color
: https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet
e.g. change
div.sourceCode
{ color: #cccccc; background-color: #303030; }
to
pre.sourceCode
{ color: #cccccc; background-color: #303030; }
Initially, I did not want to touch what pandoc is inserting. Rather than modified, could'nt we add div.sourceCode, pre.sourceCode
{ color: #cccccc; background-color: #303030; } kind of the same but maybe more compatible with pandoc ? |
Yes you certainly can. I guess the risk is minimal if we only modify this single rule from pre.sourceCode onlyboth div.sourceCode and pre.sourceCodeDid you notice the differences in the four corners? That's because Bootstrap defined rounded corners for Implementation-wise, you probably have to loop through |
oh ! I did not notice before reading what the difference was. I read the doc you linked to and some others, I agree that this require iteration. I'll work on that ! thanks for pointing the direction. |
I reverted back everything and deal with this using javascript code now. So just one file changed 😄 Do you find this better and easier to maintain ? I put the script at the end of the template but I wonder if it should be elsewhere... 🤔 Normally, if no pandoc highlighting is chosen, no need to insert this script. I hesitated to put it in this part, that i believe is activated only by using pandoc highlighting Also, I think there is another small change. New pandoc 2.0 added the div.sourceCode { margin: 1em 0; } this create a difference in rendered rmarkdown. I think it is not really impacting but I let you decide if it should be dealt with. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Many thanks! I'll take it from here. I don't care about the margin issue.
inst/rmd/h/default.html
Outdated
(function() { | ||
var stylesheets = document.styleSheets; | ||
for(var i=0; i<stylesheets.length; i++){ | ||
var rules = stylesheets[i].cssRules; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need try/catch
here since the cssRules
property is not necessarily accessible: https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet#Notes
- This allows to deal with the issue in html_notebook format. - A css rule is added for style to reduce the space introduce in pandoc css for div.sourceCode
for easier maintenance as it is related
it was introduced in rstudio#1399 initially
if exists, change div.sourceCode css rule about background-color and color by applying to pre.sourceCode
3f2399d
to
a4a144a
Compare
also add an identifier on Pandoc's CSS to save time on looking for the right <style> sheet to modify
Perfect ! So, my intuition was correct to move the hack. For rest, I am always learning - nice JS improvement. Thanks @yihui for your review! |
You did 99% of the work, and I only did the rest 1% :) |
This is a proposition to fix #1471. The issue resumes the problem introduced by recent changes in pandoc 2. The way pandoc > 2 insert css styling for highligting when converting to html conflicts with boostrap and theme.
This proposition tries to adapt rmarkdown default html template to insert css rules that deals with that.
This may be temporary if we try to report this impact to pandoc and find a clever solution that does not break this.
This is work in progress to get feedback and iterate. Also because I think the impact is rather broad among different format that rmarkdown convert to thanks to pandoc.
This change is