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 to load external highlighting theme for Pandoc HTML and PDF #2035

Closed
3 tasks done
Francis-Hsu opened this issue Feb 8, 2021 · 5 comments · Fixed by #1941 or #2285
Closed
3 tasks done

Allow to load external highlighting theme for Pandoc HTML and PDF #2035

Francis-Hsu opened this issue Feb 8, 2021 · 5 comments · Fixed by #1941 or #2285
Assignees
Labels
feature a feature request or enhancement next to consider for next release theme: highlight

Comments

@Francis-Hsu
Copy link

Francis-Hsu commented Feb 8, 2021

I have a customized pandoc syntax highlighting theme that I would like to use for my Rmd file: my.theme. For PDF output this can be achieved easily by using the following YAML header:

---
title: "Test"
author: "Me"
date: "2/8/2021"
output:
  pdf_document:
    pandoc_args: "--highlight-style=my.theme"
---

Unfortunately the same wouldn't work for html_document. While the console output shows --highlight-style=my.theme was successfully passed to pandoc, the pandoc_html_highlight_args() utility attached an argument --no-highlight to the end which then overtakes the previous argument. Stating highlight: my.theme is not an option either since pandoc_html_highlight_args() will do a match.arg check. The only work around I can think of is to use trace() to intercept the pandoc_args inside render() before it was passed to pandoc_convert(), and this is obviously a terrible idea.

A fix shouldn't be too complicated though. Maybe an option customized can be added to the output of html_highlighters()? In which case pandoc_html_highlight_args() will stop attaching further arguments so that --highlight-style can be processed correctly.


By filing an issue to this repo, I promise that

  • I have fully read the issue guide at https://yihui.org/issue/.
  • 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('rmarkdown'). 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/rmarkdown').
    • If I have posted the same issue elsewhere, I have also mentioned it in this issue.
  • I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.

@atusy
Copy link
Collaborator

atusy commented Feb 8, 2021

I'd suggest something like this so that users can easily specify custom definition like highlight: my.theme.

highlight <- tryCatch(
	match.arg(highlight, html_highlighters()),
	error = function(e) {
		if (file.exists(highlight)) return(highlight)
		stop(e$message, ", or a custom definition file.")
	}
)

@cderv
Copy link
Collaborator

cderv commented Feb 8, 2021

Thank you for this suggestion !

This is currently a limitation that should be overcome in this (rather large) PR. It is not specific to this issue, but it refactor the whole highlighting logic and thus a .theme file will work if provided in highlight argument once this PR is merged. (see #1941 (comment)).

There is currently not an easy work around due to the order of flags passed to Pandoc. Instead of hacking with trace() you can modify the arguments in a custom format. This is a temporary workaround for your case waiting for the aforementioned PR.

html_doc <- function() {
  base_format <- rmarkdown::html_document(highlight = NULL)
  pandoc_args <- base_format$pandoc$args
  pandoc_args[pandoc_args == "--no-highlight"] <- "--highlight-style=my.theme"
  base_format$pandoc$args <- pandoc_args
  base_format
}

rmarkdown::render("test.Rmd", html_doc())

@cderv cderv linked a pull request Feb 8, 2021 that will close this issue
6 tasks
@cderv cderv self-assigned this Jan 6, 2022
@cderv cderv moved this to Backlog in R Markdown Team Projects Jan 6, 2022
@cderv cderv added feature a feature request or enhancement next to consider for next release labels Jan 6, 2022
@cderv cderv changed the title RMarkdown's utility prevents pandoc from loading external highlighting theme for HTML output. Allow to load external highlighting theme for Pandoc Jan 6, 2022
Repository owner moved this from Backlog to Done in R Markdown Team Projects Jan 14, 2022
cderv added a commit that referenced this issue Jan 14, 2022
* Allow to pass a custom .theme file in highlight argument (close #2035)
* Add two custom themes: arrow.theme and rstudio.theme, the former  optimized for accessibility 
* Add optional support for downlit as a syntax highlighter, using Pandoc's theme for styling (close #1881)

Co-authored-by: Hadley Wickham <[email protected]>
@cderv
Copy link
Collaborator

cderv commented Jan 14, 2022

@royfrancis for HTML you can now use the highlight argument to pass a .theme file. For PDF not yet but you still get your trick for now.

I'll reopen this one and edit title as I have done 50% of the work. I'll do PDF support soon

@cderv cderv reopened this Jan 14, 2022
@cderv cderv changed the title Allow to load external highlighting theme for Pandoc Allow to load external highlighting theme for Pandoc HTML and PDF Jan 14, 2022
@cderv cderv moved this from Done to In Progress in R Markdown Team Projects Jan 14, 2022
Repository owner moved this from In Progress to Done in R Markdown Team Projects Jan 14, 2022
@cderv
Copy link
Collaborator

cderv commented Jan 14, 2022

And support has now been adding pdf_document() - you can use now this

---
title: "Test"
author: "Me"
date: "2/8/2021"
output:
  pdf_document:
    highlight: my.theme
---

@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement next to consider for next release theme: highlight
Projects
Archived in project
3 participants