Skip to content

Commit 8965a72

Browse files
committed
refactor: improvements to how @icon is parsed
- allows only one `@icon` per model; throws an error ow. - Although wrapping inlined `@icon` within `begin...end` is still valid; it is no longer required; aka SVGs with just quotes are allowed. - Validity of SVG-string is verified too.
1 parent 45dfe62 commit 8965a72

File tree

3 files changed

+12
-19
lines changed

3 files changed

+12
-19
lines changed

src/systems/model_parsing.jl

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ function parse_model!(exprs, comps, ext, eqs, icon, vs, ps, sps,
259259
elseif mname == Symbol("@equations")
260260
parse_equations!(exprs, eqs, dict, body)
261261
elseif mname == Symbol("@icon")
262-
parse_icon!(icon, dict, mod, body)
262+
isassigned(icon) && error("This model has more than one icon.")
263+
parse_icon!(icon, dict, body)
263264
else
264265
error("$mname is not handled.")
265266
end
@@ -471,7 +472,7 @@ function parse_equations!(exprs, eqs, dict, body)
471472
dict[:equations] = readable_code.(eqs)
472473
end
473474

474-
function parse_icon!(icon, dict, mod, body::String)
475+
function parse_icon!(icon, dict, body::String)
475476
icon_dir = get(ENV, "MTK_ICONS_DIR", joinpath(DEPOT_PATH[1], "mtk_icons"))
476477
dict[:icon] = icon[] = if isfile(body)
477478
URI("file:///" * abspath(body))
@@ -483,17 +484,13 @@ function parse_icon!(icon, dict, mod, body::String)
483484
false
484485
end
485486
URI(body)
487+
elseif (_body = lstrip(body); startswith(_body, r"<\?xml|<svg"))
488+
String(_body) # With Julia-1.10 promoting `SubString{String}` to `String` can be dropped.
486489
else
487-
error("$body is not a valid icon")
490+
error("\n$body is not a valid icon")
488491
end
489492
end
490493

491-
function parse_icon!(icon, dict, mod, body::Expr)
492-
_icon = body.args[end]
493-
dict[:icon] = icon[] = MLStyle.@match _icon begin
494-
::Symbol => get_var(mod, _icon)
495-
::String => _icon
496-
Expr(:call, read, a...) => eval(_icon)
497-
_ => error("$_icon isn't a valid icon")
498-
end
494+
function parse_icon!(icon, dict, body::Expr)
495+
parse_icon!(icon, dict, eval(body))
499496
end

test/icons/resistor.svg

Lines changed: 1 addition & 1 deletion
Loading

test/model_parsing.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,7 @@ end
6262
@components begin
6363
g = Pin()
6464
end
65-
@icon begin
66-
read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
67-
end
65+
@icon read(abspath(ENV["MTK_ICONS_DIR"], "ground.svg"), String)
6866
@equations begin
6967
g.v ~ 0
7068
end
@@ -76,10 +74,9 @@ resistor_log = "$(@__DIR__)/logo/resistor.svg"
7674
@parameters begin
7775
R, [unit = u""]
7876
end
79-
@icon begin
80-
"""<?xml version="1.0" encoding="UTF-8"?>
77+
@icon """<?xml version="1.0" encoding="UTF-8"?>
8178
<svg xmlns="http://www.w3.org/2000/svg" width="80" height="30">
82-
<path d="M10 15
79+
<path d="M10 15
8380
l15 0
8481
l2.5 -5
8582
l5 10
@@ -91,7 +88,6 @@ l2.5 -5
9188
l15 0" stroke="black" stroke-width="1" stroke-linejoin="bevel" fill="none"></path>
9289
</svg>
9390
"""
94-
end
9591
@equations begin
9692
v ~ i * R
9793
end

0 commit comments

Comments
 (0)