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

allow for bookdown hacking? #53

Merged
merged 7 commits into from
Sep 19, 2020
Merged

allow for bookdown hacking? #53

merged 7 commits into from
Sep 19, 2020

Conversation

maelle
Copy link
Contributor

@maelle maelle commented Sep 17, 2020

alternative title: make downlit_html_path() more flexible. :-)

Context: I am working on a bookdown gitbook and was trying to find a hack to use downlit before it gets really supported (so patient 🙈 ). I set bookdown's highlight parameter to NULL then I use downlit::downlit_html_path() on all resulting HTML but the HTML nodes don't have the classes downlit would expect e.g.

<pre class="r"><code>summary(cars)</code></pre>
<pre><code>##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00</code></pre>

Therefore I tweaked the xpath and now I only need to add a CSS and my gitbook will get autolinking today.

I reckon tweaking the package out of impatience is maybe not a good idea.

R/downlit-html.R Outdated Show resolved Hide resolved
@maelle
Copy link
Contributor Author

maelle commented Sep 17, 2020

My bookdown _output.yml contains

bookdown::gitbook:
  css: style.css
  highlight: NULL

where style.css contains styling for Chroma classes.

Then I run

bookdown::render_book(".")
htmls <- file.path(
  getwd(), 
  dir("docs", full.names = TRUE)
)
htmls <- htmls[grepl("\\.html$", htmls)]
purrr::walk2(
  htmls, htmls,
  downlit::downlit_html_path,
  classes = downlit::classes_chroma()
  )

And the gitbook looks really nice.

But maybe I'll have to wait for actual downlit support in html_document() :-)

R/downlit-html.R Show resolved Hide resolved
R/downlit-html.R Outdated Show resolved Hide resolved
R/downlit-html.R Outdated Show resolved Hide resolved
@maelle
Copy link
Contributor Author

maelle commented Sep 19, 2020

I am now questioning my approach after seeing the use of a knitr hook in distill.

At the moment I use tweaks of downlit and the tweaked build workflow (knitting, then highlighting all HTML) described above.

Maybe with a hook I could do without asking for tweaks of downlit and instead my work would be a) writing the hook, saving it in a chunk in an R file b) referring to it in before_chapter_script but adding logic to only execute the chunk for the gitbook output and I only know knitr::is_latex_output() for PDF vs HTML, not useful if there's an EPUB too.) 🤔 I could try that next week... (especially as it'd mean I get Pandoc highlighting for other languages)

@maelle
Copy link
Contributor Author

maelle commented Sep 19, 2020

I take it back, with a hook there'd be no autolinking of inline code.

@hadley hadley merged commit 63f235e into r-lib:master Sep 19, 2020
@hadley
Copy link
Member

hadley commented Sep 19, 2020

Thanks!

@cderv
Copy link
Contributor

cderv commented Sep 19, 2020

with a hook there'd be no autolinking of inline code.

@maelle, out of curiosity, you mean with a source hook or any hooks?
Because there is a separate hook for inline R code - It is not the source hook which is used for inline R code.

@hadley
Copy link
Member

hadley commented Sep 19, 2020

@cderv inline linking applies to `foo()` not `R foo()`

@cderv
Copy link
Contributor

cderv commented Sep 19, 2020

Oh I see. 😄 Thanks ! That is obvious when you write it. 😅

@hadley
Copy link
Member

hadley commented Sep 19, 2020

It's only obvious to me because I tried using the inline code hook and I couldn't figure out why it didn't work 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants