Skip to content

Commit 903c2c4

Browse files
committed
🐛 [HTML] Missing support for colors in column labels
1 parent 0c91678 commit 903c2c4

File tree

7 files changed

+180
-14
lines changed

7 files changed

+180
-14
lines changed

docs/src/man/html/html_backend.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,12 @@ contains the following fields:
123123
- `stubhead_label::Vector{HtmlPair}`: Style for the stubhead label.
124124
- `row_label::Vector{HtmlPair}`: Style for the row label.
125125
- `row_group_label::Vector{HtmlPair}`: Style for the row group label.
126-
- `first_line_column_label::Vector{HtmlPair}`: Style for the first line of the column
127-
labels.
128-
- `column_label::Vector{HtmlPair}`: Style for the column label.
126+
- `first_line_column_label::Union{Vector{HtmlPair}, Vector{Vector{HtmlPair}}}`: Style for
127+
the first line of the column labels. If a vector of `Vector{HtmlPair}}` is provided, each
128+
column label in the first line will use the corresponding style.
129+
- `column_label::Union{Vector{HtmlPair}, Vector{Vector{HtmlPair}}}`: Style for the rest of
130+
the column labels. If a vector of `Vector{HtmlPair}}` is provided, each column label will
131+
use the corresponding style.
129132
- `first_line_merged_column_label::Vector{HtmlPair}`: Style for the merged cells at the
130133
first column label line.
131134
- `merged_column_label::Vector{HtmlPair}`: Style for the merged cells at the rest of the

src/backends/html/documentation.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,12 @@ contains the following fields:
132132
- `stubhead_label::Vector{HtmlPair}`: Style for the stubhead label.
133133
- `row_label::Vector{HtmlPair}`: Style for the row label.
134134
- `row_group_label::Vector{HtmlPair}`: Style for the row group label.
135-
- `first_line_column_label::Vector{HtmlPair}`: Style for the first line of the column
136-
labels.
137-
- `column_label::Vector{HtmlPair}`: Style for the column label.
135+
- `first_line_column_label::Union{Vector{HtmlPair}, Vector{Vector{HtmlPair}}}`: Style for
136+
the first line of the column labels. If a vector of `Vector{HtmlPair}}` is provided,
137+
each column label in the first line will use the corresponding style.
138+
- `column_label::Union{Vector{HtmlPair}, Vector{Vector{HtmlPair}}}`: Style for the rest of
139+
the column labels. If a vector of `Vector{HtmlPair}}` is provided, each column label
140+
will use the corresponding style.
138141
- `first_line_merged_column_label::Vector{HtmlPair}`: Style for the merged cells at the
139142
first column label line.
140143
- `merged_column_label::Vector{HtmlPair}`: Style for the merged cells at the rest of the

src/backends/html/html_backend.jl

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,21 @@ function _html__print(
5454
end
5555
end
5656

57+
# Check the style variables.
58+
if style.first_line_column_label isa Vector{Vector{HtmlPair}}
59+
length(style.first_line_column_label) != table_data.num_columns &&
60+
throw(ArgumentError(
61+
"The length of `first_line_column_label` in `style` must be equal to the number of columns ($(table_data.num_columns))."
62+
))
63+
end
64+
65+
if style.column_label isa Vector{Vector{HtmlPair}}
66+
length(style.column_label) != table_data.num_columns &&
67+
throw(ArgumentError(
68+
"The length of `column_label` in `style` must be equal to the number of columns ($(table_data.num_columns))."
69+
))
70+
end
71+
5772
# == Variables to Store Information About Indentation ==================================
5873

5974
il = 0 # ..................................................... Current indentation level
@@ -432,9 +447,23 @@ function _html__print(
432447

433448
elseif action == :column_label
434449
if ps.i == 1
435-
append!(vstyle, style.first_line_column_label)
450+
append!(
451+
vstyle,
452+
if style.first_line_column_label isa Vector{Vector{HtmlPair}}
453+
style.first_line_column_label[ps.j]
454+
else
455+
style.first_line_column_label
456+
end
457+
)
436458
else
437-
append!(vstyle, style.column_label)
459+
append!(
460+
vstyle,
461+
if style.column_label isa Vector{Vector{HtmlPair}}
462+
style.column_label[ps.j]
463+
else
464+
style.column_label
465+
end
466+
)
438467
end
439468

440469
elseif action == :summary_row_cell

src/backends/html/types.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,12 @@ Define the style of the tables printed with the HTML back end.
189189
- `stubhead_label::Vector{HtmlPair}`: Style for the stubhead label.
190190
- `row_label::Vector{HtmlPair}`: Style for the row label.
191191
- `row_group_label::Vector{HtmlPair}`: Style for the row group label.
192-
- `first_line_column_label::Vector{HtmlPair}`: Style for the first line of the column
193-
labels.
194-
- `column_label::Vector{HtmlPair}`: Style for the column label.
192+
- `first_line_column_label::Union{Vector{HtmlPair}, Vector{Vector{HtmlPair}}}`: Style for
193+
the first line of the column labels. If a vector of `Vector{HtmlPair}}` is provided,
194+
each column label in the first line will use the corresponding style.
195+
- `column_label::Union{Vector{HtmlPair}, Vector{Vector{HtmlPair}}}`: Style for the rest of
196+
the column labels. If a vector of `Vector{HtmlPair}}` is provided, each column label
197+
will use the corresponding style.
195198
- `first_line_merged_column_label::Vector{HtmlPair}`: Style for the merged cells at the
196199
first column label line.
197200
- `merged_column_label::Vector{HtmlPair}`: Style for the merged cells at the rest of the
@@ -201,7 +204,10 @@ Define the style of the tables printed with the HTML back end.
201204
- `footnote::Vector{HtmlPair}`: Style for the footnote.
202205
- `source_notes::Vector{HtmlPair}`: Style for the source notes.
203206
"""
204-
@kwdef struct HtmlTableStyle
207+
@kwdef struct HtmlTableStyle{
208+
TFCL<:Union{Vector{HtmlPair}, Vector{Vector{HtmlPair}}},
209+
TCL<:Union{Vector{HtmlPair}, Vector{Vector{HtmlPair}}}
210+
}
205211
top_left_string::Vector{HtmlPair} = _HTML__NO_DECORATION
206212
top_right_string::Vector{HtmlPair} = _HTML__ITALIC
207213
table::Vector{HtmlPair} = _HTML__NO_DECORATION
@@ -212,8 +218,8 @@ Define the style of the tables printed with the HTML back end.
212218
stubhead_label::Vector{HtmlPair} = _HTML__BOLD
213219
row_label::Vector{HtmlPair} = _HTML__BOLD
214220
row_group_label::Vector{HtmlPair} = _HTML__BOLD
215-
first_line_column_label::Vector{HtmlPair} = _HTML__BOLD
216-
column_label::Vector{HtmlPair} = _HTML__NO_DECORATION
221+
first_line_column_label::TFCL = _HTML__BOLD
222+
column_label::TCL = _HTML__NO_DECORATION
217223
first_line_merged_column_label::Vector{HtmlPair} = _HTML__MERGED_CELL
218224
merged_column_label::Vector{HtmlPair} = _HTML__MERGED_CELL
219225
summary_row_cell::Vector{HtmlPair} = _HTML__NO_DECORATION

src/precompile.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,36 @@ PrecompileTools.@setup_workload begin
172172

173173
pretty_table(html_buf, types; backend = :html)
174174

175+
# .. HTML Table Styles .............................................................
176+
177+
pretty_table(
178+
html_buf,
179+
matrix;
180+
backend = :html,
181+
style = HtmlTableStyle(
182+
first_line_column_label = [["color" => "red"] for i = 1:10]
183+
)
184+
)
185+
186+
pretty_table(
187+
html_buf,
188+
matrix;
189+
backend = :html,
190+
style = HtmlTableStyle(
191+
column_label = [["color" => "red"] for i = 1:10]
192+
)
193+
)
194+
195+
pretty_table(
196+
html_buf,
197+
matrix;
198+
backend = :html,
199+
style = HtmlTableStyle(
200+
first_line_column_label = [["color" => "red"] for i = 1:10],
201+
column_label = [["color" => "red"] for i = 1:10]
202+
)
203+
)
204+
175205
# -- LaTeX -------------------------------------------------------------------------
176206

177207
pretty_table(matrix; backend = :latex)

test/backends/html/colors.jl

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
## Description #############################################################################
2+
#
3+
# HTML Back End: Tests related with colors.
4+
#
5+
############################################################################################
6+
7+
@testset "Colors" verbose = true begin
8+
@testset "Decoration of Column Labels" begin
9+
matrix = ones(3, 3)
10+
11+
expected = """
12+
<table>
13+
<thead>
14+
<tr class = "columnLabelRow">
15+
<th style = "text-align: right; color: yellow;">Col. 1</th>
16+
<th style = "text-align: right; color: yellow;">Col. 2</th>
17+
<th style = "text-align: right; color: yellow;">Col. 3</th>
18+
</tr>
19+
</thead>
20+
<tbody>
21+
<tr class = "dataRow">
22+
<td style = "text-align: right;">1.0</td>
23+
<td style = "text-align: right;">1.0</td>
24+
<td style = "text-align: right;">1.0</td>
25+
</tr>
26+
<tr class = "dataRow">
27+
<td style = "text-align: right;">1.0</td>
28+
<td style = "text-align: right;">1.0</td>
29+
<td style = "text-align: right;">1.0</td>
30+
</tr>
31+
<tr class = "dataRow">
32+
<td style = "text-align: right;">1.0</td>
33+
<td style = "text-align: right;">1.0</td>
34+
<td style = "text-align: right;">1.0</td>
35+
</tr>
36+
</tbody>
37+
</table>
38+
"""
39+
40+
result = pretty_table(
41+
String,
42+
matrix;
43+
backend = :html,
44+
color = true,
45+
style = HtmlTableStyle(; first_line_column_label = ["color" => "yellow"])
46+
)
47+
48+
@test result == expected
49+
50+
expected = """
51+
<table>
52+
<thead>
53+
<tr class = "columnLabelRow">
54+
<th style = "text-align: right; color: yellow;">Col. 1</th>
55+
<th style = "text-align: right; color: blue;">Col. 2</th>
56+
<th style = "text-align: right; color: red;">Col. 3</th>
57+
</tr>
58+
</thead>
59+
<tbody>
60+
<tr class = "dataRow">
61+
<td style = "text-align: right;">1.0</td>
62+
<td style = "text-align: right;">1.0</td>
63+
<td style = "text-align: right;">1.0</td>
64+
</tr>
65+
<tr class = "dataRow">
66+
<td style = "text-align: right;">1.0</td>
67+
<td style = "text-align: right;">1.0</td>
68+
<td style = "text-align: right;">1.0</td>
69+
</tr>
70+
<tr class = "dataRow">
71+
<td style = "text-align: right;">1.0</td>
72+
<td style = "text-align: right;">1.0</td>
73+
<td style = "text-align: right;">1.0</td>
74+
</tr>
75+
</tbody>
76+
</table>
77+
"""
78+
79+
result = pretty_table(
80+
String,
81+
matrix;
82+
backend = :html,
83+
color = true,
84+
style = HtmlTableStyle(; first_line_column_label = [
85+
["color" => "yellow"],
86+
["color" => "blue"],
87+
["color" => "red"]
88+
])
89+
)
90+
91+
@test result == expected
92+
end
93+
end
94+

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ end
2727
include("./backends/html/alignment.jl")
2828
include("./backends/html/cell_titles.jl")
2929
include("./backends/html/circular_reference.jl")
30+
include("./backends/html/colors.jl")
3031
include("./backends/html/column_width.jl")
3132
include("./backends/html/cropping.jl")
3233
include("./backends/html/default.jl")

0 commit comments

Comments
 (0)