Skip to content

Commit 3a8b98c

Browse files
authored
Merge pull request #120 from pierretotale/feature/dot_output_html_label
Feature/dot output html label
2 parents 815bf2a + 90e1d1c commit 3a8b98c

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

src/persistence.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ function savedot(io::IO, g::AbstractMetaGraph)
3737
end
3838
for p in props(g, v)
3939
key = p[1]
40-
write(io, "$key=\"$(p[2])\",")
40+
if key .=== :label && occursin(r"<+.*>+$", p[2])
41+
# The label is an HTML string, no additional quotes here.
42+
write(io, "$key=$(p[2]),")
43+
else
44+
write(io, "$key=\"$(p[2])\",")
45+
end
4146
end
4247
if length(props(g, v)) > 0
4348
write(io, "];")

test/diagram_ref.dot

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pack=true;
2424
22 [ color="#5DADE2",style="filled",penwidth="2.0",fillcolor="#dddddd",name="cases",label="Flu\nCases",shape="record",];
2525
23 [ color="#5DADE2",style="filled",penwidth="2.0",fillcolor="#dddddd",name="prices",label="Vacc\nPrice",shape="record",];
2626
24 [ color="#5DADE2",style="filled",penwidth="2.0",fillcolor="#dddddd",name="regres",label="Regression",shape="record",];
27+
25 [ color="#000000",style="filled",penwidth="2.0",fillcolor="#dddddd",name="html",label=<<U><B>Title </B></U> <BR/> Some text.>,shape="record",];
2728
1 -> 19 [ color=orange, dir=none, penwidth=4.0, style=solid, ]
2829
3 -> 15 [ color=orange, dir=none, penwidth=4.0, style=solid, ]
2930
3 -> 20 [ color=orange, dir=none, penwidth=4.0, style=solid, ]
@@ -51,4 +52,5 @@ pack=true;
5152
22 -> 24 [ color=orange, dir=none, penwidth=4.0, style=solid, ]
5253
23 -> 24 [ color=missing, dir=none, penwidth=missing, style=missing, ]
5354
24 -> 2 [ color=orange, dir=none, penwidth=4.0, style=solid, ]
55+
25 -> 4 [ color=black, dir=none, penwidth=2.0, style=solid, ]
5456
}

test/dotformat.jl

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ property_set=[
3030
(src="cases",dst="regres",color="orange",penwidth="4.0",style="solid",),
3131
(src="weather",dst="temp",color="orange",penwidth="4.0",style="solid",),
3232
(src="demo",dst="age",color="orange",penwidth="4.0",style="solid",),
33-
(src="regres",dst="cost",color="orange",penwidth="4.0",style="solid",)
33+
(src="regres",dst="cost",color="orange",penwidth="4.0",style="solid",),
34+
(src="html",dst="fed",color="black",penwidth="2.0",style="solid",)
3435
]
3536

3637
# name, label, color
@@ -59,9 +60,10 @@ vprops = [
5960
("cases","Flu\\nCases","#5DADE2"),
6061
("prices","Vacc\\nPrice","#5DADE2"),
6162
("regres", "Regression", "#5DADE2"),
63+
("html", "<<U><B>Title </B></U> <BR/> Some text.>", "#000000"),
6264
]
6365

64-
g = MetaDiGraph(24)
66+
g = MetaDiGraph(25)
6567

6668
set_prop!(g, :pack, true)
6769

@@ -95,11 +97,38 @@ for v in vertices(g)
9597
set_prop!(g, v, :penwidth, 2.0)
9698
end
9799

98-
savegraph("diagram.dot", g, DOTFormat())
99-
open("diagram_ref.dot") do fp
100-
s_ref = read(fp)
101-
open("diagram.dot") do fp
102-
s = read(fp)
103-
@test s == s_ref
100+
@testset "dotio" begin
101+
savegraph("diagram.dot", g, DOTFormat())
102+
open("diagram_ref.dot") do fp
103+
s_ref = read(fp)
104+
open("diagram.dot") do fp
105+
s = read(fp)
106+
@test s == s_ref
107+
end
108+
109+
# Verify that HTML labels are saved without quotes, but that "conventional" (non-html) labels are saved with quotes.
110+
# The label attribute should be either delimited by:
111+
# - "..." : OK
112+
# - <...> : OK
113+
# - both : NOK ("< or >" as bounding characters, resulting HTML in file will not be parsed correctly by dot.)
114+
quote_regex = r"label\s*=\s*\"(?:[^\"\\]|\\.)*\"" # source: https://stackoverflow.com/questions/249791/regex-for-quoted-string-with-escaping-quotes
115+
html_regex = r"label\s*=\s*<.*>"
116+
invalid_quote_regex = r"label\s*=\s*\"<(?:[^\"\\]|\\.)*>\""
117+
for line in eachline(fp)
118+
test_val = false
119+
if !occursin(r"label\s*=",line)
120+
# the line does not contain a "label" attribute
121+
test_val = true
122+
elseif occursin(quote_regex, line)
123+
# check if a "< or >" combination is present as outer delimiter, this shouldn't occur.
124+
test_val = !occursin(invalid_quote_regex, line)
125+
elseif occursin(html_regex, line)
126+
# no worries, proper HTML surrounding brackets found.
127+
test_val = true
128+
end
129+
@test test_val
104130
end
105131
end
132+
133+
134+
end

0 commit comments

Comments
 (0)