Skip to content

Commit

Permalink
[SVG] various improvements, by DanielSchröer++
Browse files Browse the repository at this point in the history
SVG.pm now handles plain text DOM nodes. Also, the 'serialize' method
has been simplified, and the Pod synopsis has been updated.
  • Loading branch information
Carl Masak committed Jun 22, 2009
1 parent 1309250 commit 2168edc
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions lib/SVG.pm
Expand Up @@ -3,17 +3,15 @@ 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) {
return grep { $_ ~~ Pair && $_.value !~~ Array }, @list;
}

sub find_elements(@list) {
return grep { $_ ~~ Pair && $_.value ~~ Array }, @list;
return grep { $_ ~~ Str || ($_ ~~ Pair && $_.value ~~ Array) }, @list;
}

sub element($name, @attrs, @children) {
Expand Down Expand Up @@ -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);
}
}
}
}
Expand All @@ -68,6 +71,9 @@ my $svg =
circle => [
:cx(100), :cy(100), :r(50)
],
text => [
:x(10), :y(20), "hello"
]
;
say SVG.serialize($svg);
Expand Down

0 comments on commit 2168edc

Please sign in to comment.