=item * sigil - A short code to represent the manuscript. Required.
=item * text - An array of strings (words) that contains the text of the
-manuscript.
+manuscript. This should not change after the witness has been instantiated,
+and the path through the collation should always match it.
+
+=item * layertext - An array of strings (words) that contains the layered text,
+if any, of the manuscript. This should not change after the witness has been
+instantiated, and the path through the collation should always match it.
=item * source - A reference to the text, such as a filename, if it is not
given in the 'text' option.
Accessor method for the general witness description.
-=head2 path
-
-An array of Text::Tradition::Collation::Reading objects which, taken in
-sequence, represent the text.
+=head2 is_layered
-=head2 uncorrected_path
-
-An array of Text::Tradition::Collation::Reading objects which, taken in
-sequence, represent the text before any scribal corrections were made.
+Boolean method to note whether the witness has layers (e.g. pre-correction
+readings) in the collation.
=begin testing
isa => 'ArrayRef[Str]',
predicate => 'has_text',
);
+
+has 'layertext' => (
+ is => 'rw',
+ isa => 'ArrayRef[Str]',
+ predicate => 'has_layertext',
+ );
# Source. This is where we read in the witness, if not from a
# pre-prepared collation. It is probably a filename.
predicate => 'has_source',
);
-# Path. This is an array of Reading nodes that should mirror the
-# text above.
+# Path. This is an array of Reading nodes that can be saved during
+# initialization, but should be cleared before saving in a DB.
has 'path' => (
is => 'rw',
isa => 'ArrayRef[Text::Tradition::Collation::Reading]',
predicate => 'has_path',
+ clearer => 'clear_path',
);
has 'uncorrected_path' => (
is => 'rw',
isa => 'ArrayRef[Text::Tradition::Collation::Reading]',
- predicate => 'has_ante_corr',
+ clearer => 'clear_uncorrected_path',
+ );
+
+has 'is_layered' => (
+ is => 'rw',
+ isa => 'Bool',
);
# Manuscript name or similar
isa => 'Str',
);
+# If we set an uncorrected path, ever, remember that we did so.
+around 'uncorrected_path' => sub {
+ my $orig = shift;
+ my $self = shift;
+
+ $self->is_layered( 1 );
+ $self->$orig( @_ );
+};
sub BUILD {
my $self = shift;
}
}
+=head2 export_as_json
+
+Exports the witness as a JSON structure, with the following keys:
+
+=over 4
+
+=item * id - The witness sigil
+
+=item * name - The witness identifier
+
+=item * tokens - An array of hashes of the form { "t":"WORD" }
+
+=back
+
=begin testing
use Text::Tradition;
-my $simple = 't/data/simple.txt';
-my $s = Text::Tradition->new(
- 'name' => 'inline',
- 'input' => 'Tabular',
- 'file' => $simple,
- );
-my $wit_c = $s->witness( 'C' );
-is( ref( $wit_c ), 'Text::Tradition::Witness' ),;
-if( $wit_c ) {
- ok( !$wit_c->has_text, "Text property not yet set" );
- my $c_arr = $wit_c->text;
- is( $c_arr->[0], 'Je', "Text constructed from path" );
- ok( $wit_c->has_text, "Text property now set" );
+my @text = qw( This is a line of text );
+my $wit = Text::Tradition::Witness->new(
+ 'sigil' => 'A',
+ 'text' => \@text,
+ 'identifier' => 'test witness',
+ );
+my $jsonstruct = $wit->export_as_json;
+is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" );
+is( $jsonstruct->{'name'}, 'test witness', "got the right identifier" );
+is( scalar @{$jsonstruct->{'tokens'}}, 6, "got six text tokens" );
+foreach my $idx ( 0 .. $#text ) {
+ is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $text[$idx], "tokens look OK" );
+}
+
+my @ctext = qw( when april with his showers sweet with fruit the drought of march
+ has pierced unto the root );
+my $trad = Text::Tradition->new(
+ 'input' => 'CollateX',
+ 'file' => 't/data/Collatex-16.xml' );
+
+$jsonstruct = $trad->witness('A')->export_as_json;
+is( $jsonstruct->{'id'}, 'A', "got the right witness sigil" );
+is( $jsonstruct->{'name'}, undef, "got undef for missing identifier" );
+is( scalar @{$jsonstruct->{'tokens'}}, 17, "got all text tokens" );
+foreach my $idx ( 0 .. $#ctext ) {
+ is( $jsonstruct->{'tokens'}->[$idx]->{'t'}, $ctext[$idx], "tokens look OK" );
}
=end testing
=cut
-# If the text is not present, and the path is, and this is a 'get'
-# request, generate text from path.
-around text => sub {
- my $orig = shift;
+sub export_as_json {
my $self = shift;
-
- if( $self->has_path && !$self->has_text && !@_ ) {
- my @words = map { $_->label } grep { !$_->is_meta } @{$self->path};
- $self->$orig( \@words );
- }
-
- $self->$orig( @_ );
-};
+ my @wordlist = map { { 't' => $_ || '' } } @{$self->text};
+ return {
+ 'id' => $self->sigil,
+ 'tokens' => \@wordlist,
+ 'name' => $self->identifier,
+ };
+}
no Moose;
__PACKAGE__->meta->make_immutable;