From 2168edc4f1725f59ba93dd83a709e088043154c1 Mon Sep 17 00:00:00 2001 From: Carl Masak Date: Mon, 22 Jun 2009 10:20:57 +0200 Subject: [PATCH] =?UTF-8?q?[SVG]=20various=20improvements,=20by=20DanielSc?= =?UTF-8?q?hr=C3=B6er++?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SVG.pm now handles plain text DOM nodes. Also, the 'serialize' method has been simplified, and the Pod synopsis has been updated. --- lib/SVG.pm | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/SVG.pm b/lib/SVG.pm index c3ba7cd..b7931b4 100644 --- a/lib/SVG.pm +++ b/lib/SVG.pm @@ -3,9 +3,7 @@ use v6; class SVG { method serialize($tree) { - my @attrs = find_attributes($tree); - my @children = find_elements($tree); - return [~] gather element('svg', @attrs, @children); + return [~] gather visit(['svg' => $tree]); } sub find_attributes(@list) { @@ -13,7 +11,7 @@ class SVG { } sub find_elements(@list) { - return grep { $_ ~~ Pair && $_.value ~~ Array }, @list; + return grep { $_ ~~ Str || ($_ ~~ Pair && $_.value ~~ Array) }, @list; } sub element($name, @attrs, @children) { @@ -45,10 +43,15 @@ class SVG { sub visit(@list) { for find_elements(@list) -> $element { - my ($name, $subtree) = $element.kv; - my @attrs = find_attributes($subtree); - my @children = find_elements($subtree); - element($name, @attrs, @children); + if $element ~~ Str { + take $element; + } + else { + my ($name, $subtree) = $element.kv; + my @attrs = find_attributes($subtree); + my @children = find_elements($subtree); + element($name, @attrs, @children); + } } } } @@ -68,6 +71,9 @@ my $svg = circle => [ :cx(100), :cy(100), :r(50) ], + text => [ + :x(10), :y(20), "hello" + ] ; say SVG.serialize($svg);