Merge branch 'master' of github.com:tla/stemmatology
[scpubgit/stemmatology.git] / lib / Text / Tradition / Collation / Reading.pm
index 0b57ff8..5dfa552 100644 (file)
@@ -82,22 +82,6 @@ has 'text' => (
        writer => 'alter_text',
        );
        
-has 'punctuation' => (
-       traits => ['Array'],
-       isa => 'ArrayRef[HashRef[Str]]',
-       default => sub { [] },
-       handles => {
-                       punctuation => 'elements',
-                       add_punctuation => 'push',
-                       },
-       );
-
-has 'separate_punctuation' => (
-       is => 'ro',
-       isa => 'Bool',
-       default => 1,
-       );
-
 has 'is_start' => (
        is => 'ro',
        isa => 'Bool',
@@ -139,9 +123,21 @@ around BUILDARGS => sub {
                $args = { @_ };
        }
        
+       # Did we get a JSON token to parse into a reading?  If so, massage it.
+       if( exists $args->{'json'} ) {
+               my $j = delete $args->{'json'};
+
+               # If we have separated punctuation, restore it.
+               if( exists $j->{'punctuation'} ) {
+                       $args->{'text'} = _restore_punct( $j->{'t'}, $j->{'punctuation'} );
+               } else {
+                       $args->{'text'} = $j->{'t'};
+                       # we don't use comparison or canonical forms yet
+               }
+       }
+               
        # If one of our special booleans is set, we change the text and the
        # ID to match.
-       
        if( exists $args->{'is_lacuna'} && !exists $args->{'text'} ) {
                $args->{'text'} = '#LACUNA#';
        } elsif( exists $args->{'is_start'} ) {
@@ -158,32 +154,14 @@ around BUILDARGS => sub {
        $class->$orig( $args );
 };
 
-# Post-process the given text, stripping punctuation if we are asked.
-sub BUILD {
-       my $self = shift;
-       if( $self->separate_punctuation && !$self->is_meta ) {
-               my $pos = 0;
-               my $wspunct = '';  # word sans punctuation
-               foreach my $char ( split( //, $self->text ) ) {
-                       if( $char =~ /^[[:punct:]]$/ ) {
-                               $self->add_punctuation( { 'char' => $char, 'pos' => $pos } );
-                       } else {
-                               $wspunct .= $char;
-                       }
-                       $pos++;
-               }
-               $self->alter_text( $wspunct );
-       }
-}
-
-sub punctuated_form {
-       my $self = shift;
-       my $word = $self->text;
-       foreach my $p ( sort { $a->{pos} <=> $b->{pos} } $self->punctuation ) {
+# Utility function for parsing JSON from nCritic
+sub _restore_punct {
+       my( $word, @punct ) = @_;
+       foreach my $p ( sort { $a->{pos} <=> $b->{pos} } @punct ) {
                substr( $word, $p->{pos}, 0, $p->{char} );
        }
        return $word;
-}
+}      
 
 =head2 is_meta