package Text::Tradition::Collation::Reading;
use Moose;
-use overload '""' => \&_stringify;
-use Text::Tradition::Collation;
+use overload '""' => \&_stringify, 'fallback' => 1;
=head1 NAME
=item is_lacuna - The 'reading' represents a known gap in the text.
+=item is_ph - A temporary placeholder for apparatus parsing purposes. Do not use unless you know what you are doing.
+
=item rank - The sequence number of the reading. This should probably not be set manually.
=back
is => 'ro',
isa => 'Str',
required => 1,
+ writer => 'alter_text',
);
-
+
has 'is_start' => (
is => 'ro',
isa => 'Bool',
isa => 'Bool',
default => undef,
);
+
+has 'is_ph' => (
+ is => 'ro',
+ isa => 'Bool',
+ default => undef,
+ );
has 'rank' => (
is => 'rw',
$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'} ) {
- $args->{'text'} = sprintf( "#LACUNA_%s#", $args->{'id'} );
+ if( exists $args->{'is_lacuna'} && !exists $args->{'text'} ) {
+ $args->{'text'} = '#LACUNA#';
} elsif( exists $args->{'is_start'} ) {
$args->{'id'} = '#START#'; # Change the ID to ensure we have only one
$args->{'text'} = '#START#';
} elsif( exists $args->{'is_end'} ) {
$args->{'id'} = '#END#'; # Change the ID to ensure we have only one
$args->{'text'} = '#END#';
+ } elsif( exists $args->{'is_ph'} ) {
+ $args->{'text'} = $args->{'id'};
}
$class->$orig( $args );
};
+# 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
A meta attribute (ha ha), which should be true if any of our 'special'
sub is_meta {
my $self = shift;
- return $self->is_start || $self->is_end || $self->is_lacuna;
+ return $self->is_start || $self->is_end || $self->is_lacuna || $self->is_ph;
}
# Some syntactic sugar
return $self->collation->related_readings( $self, @_ );
}
+sub predecessors {
+ my $self = shift;
+ my @pred = $self->collation->sequence->predecessors( $self->id );
+ return map { $self->collation->reading( $_ ) } @pred;
+}
+
+sub successors {
+ my $self = shift;
+ my @succ = $self->collation->sequence->successors( $self->id );
+ return map { $self->collation->reading( $_ ) } @succ;
+}
+
+sub set_identical {
+ my( $self, $other ) = @_;
+ return $self->collation->add_relationship( $self, $other,
+ { 'type' => 'transposition' } );
+}
+
sub _stringify {
my $self = shift;
return $self->id;