allow case-insensitive relationships on all but orthography
[scpubgit/stemmatology.git] / lib / Text / Tradition / Parser / Self.pm
index bdadce2..5499fc7 100644 (file)
@@ -202,7 +202,8 @@ sub parse {
        # Nodes are added via the call to add_reading above.  We only need
        # add the relationships themselves.
        # TODO check that scoping does trt
-       foreach my $e ( @{$rel_data->{'edges'}} ) {
+       $rel_data->{'edges'} ||= []; # so that the next line doesn't break on no rels
+       foreach my $e ( sort { _layersort_rel( $a, $b ) } @{$rel_data->{'edges'}} ) {
                my $from = $collation->reading( $e->{'source'}->{'id'} );
                my $to = $collation->reading( $e->{'target'}->{'id'} );
                delete $e->{'source'};
@@ -230,6 +231,21 @@ sub parse {
        $collation->text_from_paths();  
 }
 
+## Return the relationship that comes first in priority.
+my %LAYERS = (
+       'collated' => 1,
+       'orthographic' => 2,
+       'spelling' => 3,
+       );
+
+sub _layersort_rel {
+       my( $a, $b ) = @_;
+       my $key = exists $a->{'type'} ? 'type' : 'relationship';
+       my $at = $LAYERS{$a->{$key}} || 99;
+       my $bt = $LAYERS{$b->{$key}} || 99;
+       return $at <=> $bt;
+}
+
 1;
 
 =head1 BUGS / TODO