From: Tara L Andrews Date: Fri, 6 Apr 2012 21:40:36 +0000 (+0200) Subject: do proper quoting of unusual entity names in the dot X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=986bbd1b2baa1c0b52b5295279f6bfc07c6d806c;p=scpubgit%2Fstemmatology.git do proper quoting of unusual entity names in the dot --- diff --git a/lib/Text/Tradition/Stemma.pm b/lib/Text/Tradition/Stemma.pm index 16dad09..61ecb52 100644 --- a/lib/Text/Tradition/Stemma.pm +++ b/lib/Text/Tradition/Stemma.pm @@ -192,12 +192,13 @@ sub as_dot { push( @dotlines, _make_dotline( $n, 'label' => $ltext ) ); } else { # Use the default display settings. + $n = _dotquote( $n ); push( @dotlines, " $n;" ); } } # Add each of our edges. foreach my $e ( $self->graph->edges ) { - my( $from, $to ) = @$e; + my( $from, $to ) = map { _dotquote( $_ ) } @$e; push( @dotlines, " $from -> $to;" ); } push( @dotlines, '}' ); @@ -233,7 +234,7 @@ sub editable { push( @dotlines, _make_dotline( $n, 'class' => 'extant' ) ); } foreach my $e ( sort _by_vertex $self->graph->edges ) { - my( $from, $to ) = @$e; + my( $from, $to ) = map { _dotquote( $_ ) } @$e; push( @dotlines, " $from -> $to;" ); } push( @dotlines, '}' ); @@ -244,13 +245,20 @@ sub _make_dotline { my( $obj, %attr ) = @_; my @pairs; foreach my $k ( keys %attr ) { - my $v = $attr{$k}; - $v =~ s/\"/\\\"/g; - push( @pairs, "$k=\"$v\"" ); + my $v = _dotquote( $attr{$k} ); + push( @pairs, "$k=$v" ); } - return sprintf( " %s [ %s ];", $obj, join( ', ', @pairs ) ); + return sprintf( " %s [ %s ];", _dotquote( $obj ), join( ', ', @pairs ) ); } +sub _dotquote { + my( $str ) = @_; + return $str if $str =~ /^[A-Za-z0-9]+$/; + $str =~ s/\"/\\\"/g; + $str = '"' . $str . '"'; + return $str; +} + sub _by_vertex { return $a->[0].$a->[1] cmp $b->[0].$b->[1]; }