Skip to content

Commit 0c91678

Browse files
committed
🐛 [HTML] Fix support to styled strings
1 parent 78885fe commit 0c91678

File tree

4 files changed

+107
-5
lines changed

4 files changed

+107
-5
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ julia = "1.10"
3232

3333
[extras]
3434
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
35+
StyledStrings = "f489334b-da3d-4c2e-b8f0-e476e12c162b"
3536
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3637

3738
[targets]

src/backends/html/render_cell.jl

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ function _html__cell_to_str(cell::Any, context::IOContext, ::Val{:show})
2929
end
3030

3131
function _html__cell_to_str(cell::AbstractString, context::IOContext, ::Val{:show})
32-
return string(cell)
32+
if showable(MIME("text/html"), cell)
33+
# This code handles, for example, StyledStrings.jl objects.
34+
cell_str = sprint(show, MIME("text/html"), cell; context)
35+
else
36+
cell_str = string(cell)
37+
end
38+
39+
return cell_str
3340
end
3441

3542
_html__cell_to_str(cell::HTML, context::IOContext, ::Val{:print}) = cell.content
@@ -63,8 +70,27 @@ function _html__render_cell(
6370
# Check if we need to replace `\n` with `<br>`.
6471
replace_newline = line_breaks
6572

66-
# If the cell type is `HTML`, we should not escape the string.
67-
cell isa HTML && return cell_str
73+
# If the user wants HTML code inside cell, we must not escape the HTML characters.
74+
return _html__escape_str(cell_str, replace_newline, !allow_html_in_cells)
75+
end
76+
77+
function _html__render_cell(
78+
cell::AbstractString,
79+
context::IOContext,
80+
renderer::Union{Val{:print}, Val{:show}};
81+
allow_html_in_cells::Bool = false,
82+
line_breaks::Bool = false,
83+
)
84+
cell_str = _html__cell_to_str(cell, context, renderer)
85+
86+
# Check if we need to replace `\n` with `<br>`.
87+
replace_newline = line_breaks
88+
89+
# If the string is showable as HTML, we assume it contains HTML code and we do not
90+
# escape it.
91+
if showable(MIME("text/html"), cell)
92+
allow_html_in_cells = true
93+
end
6894

6995
# If the user wants HTML code inside cell, we must not escape the HTML characters.
7096
return _html__escape_str(cell_str, replace_newline, !allow_html_in_cells)

test/backends/html/issues.jl

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
## Description #############################################################################
2+
#
3+
# HTML Back End: Issues.
4+
#
5+
############################################################################################
6+
7+
@testset "Issues" verbose = true begin
8+
@testset "StyledStrings Support" begin
9+
A = [
10+
(1, 1) styled"({red:2}, {blue:3})"
11+
styled"({green:2}, {yellow:3})" "(2, 4)"
12+
]
13+
14+
expected = """
15+
<table>
16+
<thead>
17+
<tr class = "columnLabelRow">
18+
<th style = "text-align: right; font-weight: bold;"><Column 1></th>
19+
<th style = "text-align: right; font-weight: bold;"><Column 2></th>
20+
</tr>
21+
</thead>
22+
<tbody>
23+
<tr class = "dataRow">
24+
<td style = "text-align: right;">(1, 1)</td>
25+
<td style = "text-align: right;">(2, 3)</td>
26+
</tr>
27+
<tr class = "dataRow">
28+
<td style = "text-align: right;">(2, 3)</td>
29+
<td style = "text-align: right;">(2, 4)</td>
30+
</tr>
31+
</tbody>
32+
</table>
33+
"""
34+
35+
result = pretty_table(
36+
String,
37+
A;
38+
backend = :html,
39+
column_labels = [styled"<{red:Column 1}>", "<Column 2>"]
40+
)
41+
42+
@test result == expected
43+
44+
expected = """
45+
<table>
46+
<thead>
47+
<tr class = "columnLabelRow">
48+
<th style = "text-align: right; font-weight: bold;">&lt;<span style="color: #a51c2c">Column 1</span>&gt;</th>
49+
<th style = "text-align: right; font-weight: bold;">&lt;Column 2&gt;</th>
50+
</tr>
51+
</thead>
52+
<tbody>
53+
<tr class = "dataRow">
54+
<td style = "text-align: right;">(1, 1)</td>
55+
<td style = "text-align: right;">(<span style="color: #a51c2c">2</span>, <span style="color: #195eb3">3</span>)</td>
56+
</tr>
57+
<tr class = "dataRow">
58+
<td style = "text-align: right;">(<span style="color: #25a268">2</span>, <span style="color: #e5a509">3</span>)</td>
59+
<td style = "text-align: right;">(2, 4)</td>
60+
</tr>
61+
</tbody>
62+
</table>
63+
"""
64+
65+
result = pretty_table(
66+
String,
67+
A;
68+
backend = :html,
69+
column_labels = [styled"<{red:Column 1}>", "<Column 2>"],
70+
renderer = :show
71+
)
72+
end
73+
end

test/runtests.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using Test
22
using PrettyTables
33

4+
using LaTeXStrings
45
using Markdown
56
using OffsetArrays
6-
using LaTeXStrings
7+
using StyledStrings
78
using Tables
89

910
############################################################################################
@@ -24,14 +25,15 @@ end
2425

2526
@testset "HTML Back End Tests" verbose = true begin
2627
include("./backends/html/alignment.jl")
27-
include("./backends/html/circular_reference.jl")
2828
include("./backends/html/cell_titles.jl")
29+
include("./backends/html/circular_reference.jl")
2930
include("./backends/html/column_width.jl")
3031
include("./backends/html/cropping.jl")
3132
include("./backends/html/default.jl")
3233
include("./backends/html/divs.jl")
3334
include("./backends/html/full.jl")
3435
include("./backends/html/highlighters.jl")
36+
include("./backends/html/issues.jl")
3537
include("./backends/html/minify.jl")
3638
include("./backends/html/offset_arrays.jl")
3739
include("./backends/html/renderers.jl")

0 commit comments

Comments
 (0)