Skip to content

Commit 2df72ef

Browse files
committed
✨ [HTML] Add column label titles
1 parent 611c1a8 commit 2df72ef

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

docs/src/man/html/html_backend.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ the output.
1010
This can be useful to render tables with more complex content, but it can also be a
1111
security risk if the content is not sanitized.
1212
(**Default**: `false`)
13+
- `column_label_titles::Union{Nothing, AbstractVector}`: Titles for the column labels. If
14+
`nothing`, no titles are added. If a vector is passed, it must have the same length as the
15+
number of column label rows. Each element in the vector can be `nothing` (no title for
16+
that row) or an element with the title for that row. Notice that this element will be
17+
converted to string using the function `string`.
18+
(**Default**: `nothing`)
1319
- `highlighters::Vector{HtmlHighlighter}`: Highlighters to apply to the table. For more
1420
information, see the section [HTML Highlighters](@ref).
1521
- `line_breaks::Bool`: If `true`, line breaks in the content of the cells (`\\n`) are

src/backends/html/documentation.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ the output.
1717
This can be useful to render tables with more complex content, but it can also be a
1818
security risk if the content is not sanitized.
1919
(**Default**: `false`)
20+
- `column_label_titles::Union{Nothing, AbstractVector}`: Titles for the column labels. If
21+
`nothing`, no titles are added. If a vector is passed, it must have the same length as
22+
the number of column label rows. Each element in the vector can be `nothing` (no title
23+
for that row) or an element with the title for that row. Notice that this element will
24+
be converted to string using the function `string`.
25+
(**Default**: `nothing`)
2026
- `highlighters::Vector{HtmlHighlighter}`: Highlighters to apply to the table. For more
2127
information, see the section **HTML Highlighters** in the **Extended Help**.
2228
- `line_breaks::Bool`: If `true`, line breaks in the content of the cells (`\\n`) are

src/backends/html/html_backend.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
function _html__print(
88
pspec::PrintingSpec;
99
allow_html_in_cells::Bool = false,
10+
column_label_titles::Union{Nothing, AbstractVector} = nothing,
1011
highlighters::Vector{HtmlHighlighter} = HtmlHighlighter[],
1112
is_stdout::Bool = false,
1213
line_breaks::Bool = false,
@@ -35,6 +36,22 @@ function _html__print(
3536
vproperties = Pair{String, String}[]
3637
vstyle = Pair{String, String}[]
3738

39+
# Check the dimensions of header cell titles.
40+
if !isnothing(column_label_titles)
41+
num_column_label_rows = length(table_data.column_labels)
42+
num_data_columns = size(table_data.data, 2)
43+
44+
if length(column_label_titles) < num_column_label_rows
45+
error("The number of vectors in `column_label_titles` must be equal or greater than that in `column_labels`.")
46+
end
47+
48+
for k in eachindex(column_label_titles)
49+
if !isnothing(column_label_titles[k]) && (length(column_label_titles[k]) != num_data_columns)
50+
error("The number of elements in each row of `column_label_titles` must match the number of columns in the table.")
51+
end
52+
end
53+
end
54+
3855
# == Variables to Store Information About Indentation ==================================
3956

4057
il = 0 # ..................................................... Current indentation level
@@ -281,6 +298,12 @@ function _html__print(
281298

282299
cell === _IGNORE_CELL && continue
283300

301+
# If we are in a column label, check for cell titles.
302+
if !isnothing(column_label_titles) && (action == :column_label)
303+
title = column_label_titles[ps.i]
304+
!isnothing(title) && push!(vproperties, "title" => string(title[ps.j]))
305+
end
306+
284307
# If we are in a column label, check if we must merge the cell.
285308
if (action == :column_label) && (cell isa MergeCells)
286309
# Check if we have enough data columns to merge the cell.

test/backends/html/cell_titles.jl

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

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ end
2525
@testset "HTML Back End Tests" verbose = true begin
2626
include("./backends/html/alignment.jl")
2727
include("./backends/html/circular_reference.jl")
28+
include("./backends/html/cell_titles.jl")
2829
include("./backends/html/column_width.jl")
2930
include("./backends/html/cropping.jl")
3031
include("./backends/html/default.jl")

0 commit comments

Comments
 (0)