Skip to content

Commit da42a01

Browse files
committed
🐛 [Markdown] Missing support for colors in column labels
1 parent ae50ec4 commit da42a01

File tree

7 files changed

+131
-12
lines changed

7 files changed

+131
-12
lines changed

docs/src/man/markdown/markdown_backend.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ that contains the following fields:
9090
- `stubhead_label::MarkdownStyle`: Style for the stubhead label.
9191
- `row_label::MarkdownStyle`: Style for the row label.
9292
- `row_group_label::MarkdownStyle`: Style for the row group label.
93-
- `first_column_label::MarkdownStyle`: Style for the first line of the column labels.
94-
- `column_label::MarkdownStyle`: Style for the column label.
93+
- `first_line_column_label::Union{MarkdownStyle, Vector{MarkdownStyle}}`: Style for the
94+
first line of the column label. If a vector of `MarkdownStyle` is provided, each column
95+
label in the first line will use the corresponding style.
96+
- `column_label::Union{MarkdownStyle, Vector{MarkdownStyle}}`: Style for the column label.
97+
If a vector of `MarkdownStyle` is provided, each column label will use the corresponding
98+
style.
9599
- `summary_row_label::MarkdownStyle`: Style for the summary row label.
96100
- `summary_row_cell::MarkdownStyle`: Style for the summary row cell.
97101
- `footnote::MarkdownStyle`: Style for the footnote.

src/backends/markdown/documentation.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,12 @@ that contains the following fields:
9999
- `first_column_label::MarkdownStyle`: Style for the first line of the column labels.
100100
- `row_label::MarkdownStyle`: Style for the row label.
101101
- `row_group_label::MarkdownStyle`: Style for the row group label.
102-
- `first_column_label::MarkdownStyle`: Style for the first line of the column
103-
labels.
104-
- `column_label::MarkdownStyle`: Style for the column label.
102+
- `first_line_column_label::Union{MarkdownStyle, Vector{MarkdownStyle}}`: Style for the
103+
first line of the column label. If a vector of `MarkdownStyle` is provided, each column
104+
label in the first line will use the corresponding style.
105+
- `column_label::Union{MarkdownStyle, Vector{MarkdownStyle}}`: Style for the column label.
106+
If a vector of `MarkdownStyle` is provided, each column label will use the corresponding
107+
style.
105108
- `summary_row_label::MarkdownStyle`: Style for the summary row label.
106109
- `summary_row_cell::MarkdownStyle`: Style for the summary row cell.
107110
- `footnote::MarkdownStyle`: Style for the footnote.

src/backends/markdown/markdown_backend.jl

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ function _markdown__print(
2727
num_printed_data_rows = _number_of_printed_data_rows(table_data)
2828
num_summary_rows = _has_summary_rows(table_data) ? length(table_data.summary_rows) : 0
2929

30+
# Check the style variables.
31+
if style.first_line_column_label isa Vector{MarkdownStyle}
32+
length(style.first_line_column_label) != table_data.num_columns &&
33+
throw(ArgumentError(
34+
"The length of `first_line_column_label` in `style` must be equal to the number of columns ($(table_data.num_columns))."
35+
))
36+
end
37+
38+
if style.column_label isa Vector{MarkdownStyle}
39+
length(style.column_label) != table_data.num_columns &&
40+
throw(ArgumentError(
41+
"The length of `column_label` in `style` must be equal to the number of columns ($(table_data.num_columns))."
42+
))
43+
end
44+
3045
# == Render the Table ==================================================================
3146

3247
# For Markdown, we need to render the entire table before printing to take into account
@@ -96,7 +111,19 @@ function _markdown__print(
96111
if table_data.show_column_labels && (action == :column_label)
97112
# Apply the style to the column label.
98113
rendered_cell = _markdown__apply_style(
99-
ir == 1 ? style.first_column_label : style.column_label,
114+
if ir == 1
115+
if style.first_line_column_label isa Vector{MarkdownStyle}
116+
style.first_line_column_label[jr]
117+
else
118+
style.first_line_column_label
119+
end
120+
else
121+
if style.column_label isa Vector{MarkdownStyle}
122+
style.column_label[jr]
123+
else
124+
style.column_label
125+
end
126+
end,
100127
rendered_cell
101128
)
102129

src/backends/markdown/types.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,23 +125,29 @@ Define the style of the tables printed with the markdown back end.
125125
- `stubhead_label::MarkdownStyle`: Style for the stubhead label.
126126
- `row_label::MarkdownStyle`: Style for the row label.
127127
- `row_group_label::MarkdownStyle`: Style for the row group label.
128-
- `first_column_label::MarkdownStyle`: Style for the first line of the column
129-
labels.
130-
- `column_label::MarkdownStyle`: Style for the column label.
128+
- `first_line_column_label::Union{MarkdownStyle, Vector{MarkdownStyle}}`: Style for the
129+
first line of the column label. If a vector of `MarkdownStyle` is provided, each column
130+
label in the first line will use the corresponding style.
131+
- `column_label::Union{MarkdownStyle, Vector{MarkdownStyle}}`: Style for the column label.
132+
If a vector of `MarkdownStyle` is provided, each column label will use the corresponding
133+
style.
131134
- `summary_row_label::MarkdownStyle`: Style for the summary row label.
132135
- `summary_row_cell::MarkdownStyle`: Style for the summary row cell.
133136
- `footnote::MarkdownStyle`: Style for the footnote.
134137
- `source_note::MarkdownStyle`: Style for the source note.
135138
- `omitted_cell_summary::MarkdownStyle`: Style for the omitted cell summary.
136139
"""
137-
@kwdef struct MarkdownTableStyle
140+
@kwdef struct MarkdownTableStyle{
141+
TFCL<:Union{MarkdownStyle, Vector{MarkdownStyle}},
142+
TCL<:Union{MarkdownStyle, Vector{MarkdownStyle}}
143+
}
138144
row_number_label::MarkdownStyle = _MARKDOWN__BOLD
139145
row_number::MarkdownStyle = _MARKDOWN__BOLD
140146
stubhead_label::MarkdownStyle = _MARKDOWN__BOLD
141147
row_label::MarkdownStyle = _MARKDOWN__BOLD
142148
row_group_label::MarkdownStyle = _MARKDOWN__BOLD
143-
first_column_label::MarkdownStyle = _MARKDOWN__BOLD
144-
column_label::MarkdownStyle = _MARKDOWN__CODE
149+
first_line_column_label::TFCL = _MARKDOWN__BOLD
150+
column_label::TCL = _MARKDOWN__CODE
145151
summary_row_label::MarkdownStyle = _MARKDOWN__BOLD
146152
summary_row_cell::MarkdownStyle = _MARKDOWN__NO_DECORATION
147153
footnote::MarkdownStyle = _MARKDOWN__NO_DECORATION

src/precompile.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,33 @@ PrecompileTools.@setup_workload begin
257257

258258
pretty_table(types; backend = :markdown)
259259

260+
# .. Markdown Table Styles .........................................................
261+
262+
pretty_table(
263+
matrix;
264+
backend = :markdown,
265+
style = MarkdownTableStyle(
266+
first_line_column_label = [MarkdownStyle(bold = true) for i = 1:10]
267+
)
268+
)
269+
270+
pretty_table(
271+
matrix;
272+
backend = :markdown,
273+
style = MarkdownTableStyle(
274+
column_label = [MarkdownStyle(italic = true) for i = 1:10]
275+
)
276+
)
277+
278+
pretty_table(
279+
matrix;
280+
backend = :markdown,
281+
style = MarkdownTableStyle(
282+
first_line_column_label = [MarkdownStyle(bold = true) for i = 1:10],
283+
column_label = [MarkdownStyle(italic = true) for i = 1:10]
284+
)
285+
)
286+
260287
# == Input: Tables.jl ==============================================================
261288

262289
pretty_table(html_buf, table)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## Description #############################################################################
2+
#
3+
# Markdown Back End: Tests related with decorations.
4+
#
5+
############################################################################################
6+
7+
@testset "Decorations" verbose = true begin
8+
@testset "Decoration of Column Labels" begin
9+
matrix = ones(3, 3)
10+
11+
expected = """
12+
| *Col. 1* | *Col. 2* | *Col. 3* |
13+
|---------:|---------:|---------:|
14+
| 1.0 | 1.0 | 1.0 |
15+
| 1.0 | 1.0 | 1.0 |
16+
| 1.0 | 1.0 | 1.0 |
17+
"""
18+
19+
result = pretty_table(
20+
String,
21+
matrix;
22+
backend = :markdown,
23+
style = MarkdownTableStyle(; first_line_column_label = MarkdownStyle(italic = true))
24+
)
25+
26+
@test result == expected
27+
28+
expected = """
29+
| *Col. 1* | **Col. 2** | `Col. 3` |
30+
|---------:|-----------:|---------:|
31+
| 1.0 | 1.0 | 1.0 |
32+
| 1.0 | 1.0 | 1.0 |
33+
| 1.0 | 1.0 | 1.0 |
34+
"""
35+
36+
result = pretty_table(
37+
String,
38+
matrix;
39+
backend = :markdown,
40+
style = MarkdownTableStyle(; first_line_column_label = [
41+
MarkdownStyle(italic = true),
42+
MarkdownStyle(bold = true),
43+
MarkdownStyle(code = true)
44+
])
45+
)
46+
47+
@test result == expected
48+
end
49+
end
50+
51+

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ end
6060
include("./backends/markdown/circular_reference.jl")
6161
include("./backends/markdown/compact.jl")
6262
include("./backends/markdown/cropping.jl")
63+
include("./backends/markdown/decorations.jl")
6364
include("./backends/markdown/default.jl")
6465
include("./backends/markdown/full.jl")
6566
include("./backends/markdown/highlighters.jl")

0 commit comments

Comments
 (0)