Skip to content

Commit 20ff237

Browse files
renderings as document option
fixes #12381
1 parent 9d75330 commit 20ff237

File tree

4 files changed

+157
-3
lines changed

4 files changed

+157
-3
lines changed

src/resources/filters/normalize/flags.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ function compute_flags()
195195
elseif lightbox_auto == false then
196196
flags.has_lightbox = false
197197
end
198+
if el.renderings then
199+
flags.has_renderings = true
200+
end
198201
end,
199202
}}
200203
end

src/resources/filters/quarto-post/cell-renderings.lua

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,31 @@ function choose_cell_renderings()
88
return {json}
99
end
1010
end
11+
12+
local documentRenderings
1113

1214
return {
15+
traverse = "topdown",
16+
Meta = function(meta)
17+
if meta.renderings then
18+
documentRenderings = {}
19+
for _, inlines in ipairs(meta.renderings) do
20+
table.insert(documentRenderings, inlines[1].text)
21+
end
22+
end
23+
end,
1324
Div = function(div)
1425
-- Only process cell div with renderings attr
15-
if not div.classes:includes("cell") or not div.attributes["renderings"] then
26+
if not div.classes:includes("cell") or (not documentRenderings and not div.attributes["renderings"]) then
1627
return nil
1728
end
18-
local renderingsJson = div.attributes['renderings']
19-
local renderings = jsonDecodeArray(renderingsJson)
29+
local renderings
30+
if div.attributes['renderings'] then
31+
local renderingsJson = div.attributes['renderings']
32+
renderings = jsonDecodeArray(renderingsJson)
33+
else
34+
renderings = documentRenderings
35+
end
2036
if not type(renderings) == "table" or #renderings == 0 then
2137
quarto.log.warning("renderings expected array of rendering names, got", renderings)
2238
return nil

src/resources/schema/document-options.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
description: The dark theme name, theme scss file, or a mix of both.
3535
description: Theme name, theme scss file, or a mix of both.
3636

37+
- name: renderings
38+
schema:
39+
arrayOf: string
40+
description: "Array of rendering names, e.g. `[light, dark]`"
41+
3742
- name: body-classes
3843
tags:
3944
formats: [$html-doc]
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
title: "knitr dark mode - ggplot"
3+
brand:
4+
light: united-brand.yml
5+
dark: slate-brand.yml
6+
execute:
7+
echo: false
8+
warning: false
9+
renderings: [light, dark]
10+
_quarto:
11+
tests:
12+
html:
13+
ensureHtmlElements:
14+
-
15+
- 'body.quarto-light'
16+
- 'div.cell div.light-content'
17+
- 'div.cell div.dark-content'
18+
- 'div.cell div.cell-code pre.code-with-copy'
19+
- []
20+
---
21+
22+
```{r}
23+
#| echo: false
24+
#| warning: false
25+
library(ggplot2)
26+
27+
ggplot_theme <- function(bgcolor, fgcolor) {
28+
theme_minimal(base_size = 11) %+%
29+
theme(
30+
panel.border = element_blank(),
31+
panel.grid.major.y = element_blank(),
32+
panel.grid.minor.y = element_blank(),
33+
panel.grid.major.x = element_blank(),
34+
panel.grid.minor.x = element_blank(),
35+
text = element_text(colour = fgcolor),
36+
axis.text = element_text(colour = fgcolor),
37+
rect = element_rect(colour = bgcolor, fill = bgcolor),
38+
plot.background = element_rect(fill = bgcolor, colour = NA),
39+
axis.line = element_line(colour = fgcolor),
40+
axis.ticks = element_line(colour = fgcolor)
41+
)
42+
}
43+
44+
brand_ggplot <- function(brand_yml) {
45+
brand <- yaml::yaml.load_file(brand_yml)
46+
ggplot_theme(brand$color$background, brand$color$foreground)
47+
}
48+
49+
united_theme <- brand_ggplot("united-brand.yml")
50+
slate_theme <- brand_ggplot("slate-brand.yml")
51+
52+
colour_scale <- scale_colour_manual(values = c("darkorange", "purple", "cyan4"))
53+
```
54+
55+
### no crossref, no caption
56+
57+
```{r}
58+
ggplot(mtcars, aes(mpg, wt)) +
59+
geom_point(aes(colour = factor(cyl))) +
60+
united_theme +
61+
colour_scale
62+
ggplot(mtcars, aes(mpg, wt)) +
63+
geom_point(aes(colour = factor(cyl))) +
64+
slate_theme +
65+
colour_scale
66+
```
67+
68+
### with crossref but no caption
69+
70+
and `echo: true`
71+
72+
::: {#fig-ggplot}
73+
74+
```{r}
75+
#| echo: true
76+
#| renderings: [dark, light]
77+
78+
# override renderings order
79+
ggplot(mtcars, aes(mpg, disp)) +
80+
geom_point(aes(colour = factor(cyl))) +
81+
slate_theme +
82+
colour_scale
83+
ggplot(mtcars, aes(mpg, disp)) +
84+
geom_point(aes(colour = factor(cyl))) +
85+
united_theme +
86+
colour_scale
87+
```
88+
89+
:::
90+
91+
92+
### with caption but no crossref
93+
94+
<div>
95+
96+
```{r}
97+
#| renderings: [dark]
98+
99+
# override number of renderings
100+
ggplot(mtcars, aes(mpg, disp)) +
101+
geom_point(aes(colour = factor(cyl))) +
102+
slate_theme +
103+
colour_scale
104+
```
105+
106+
ggplot - dark only
107+
108+
</div>
109+
110+
111+
### with crossref and caption
112+
113+
::: {#fig-ggplot-mpg-hp}
114+
```{r}
115+
ggplot(mtcars, aes(mpg, hp)) +
116+
geom_point(aes(colour = factor(cyl))) +
117+
united_theme +
118+
colour_scale
119+
ggplot(mtcars, aes(mpg, hp)) +
120+
geom_point(aes(colour = factor(cyl))) +
121+
slate_theme +
122+
colour_scale
123+
```
124+
125+
mtcars - mpg vs hp
126+
:::
127+
128+
Here's a [link](https://example.com).
129+
130+
{{< lipsum 3 >}}

0 commit comments

Comments
 (0)