X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FText%2FTradition.pm;h=5f8ad83da10c803aa6b6b2359daa7a5b2d929d78;hb=fae52efdaeb4b67f58e47ec8ebe110537f2535ea;hp=0bbc76b442c32f012edcff255872fe6d3220fd79;hpb=335a62ef9e58093170ab3e2da0945fc545e9f982;p=scpubgit%2Fstemmatology.git diff --git a/lib/Text/Tradition.pm b/lib/Text/Tradition.pm index 0bbc76b..5f8ad83 100644 --- a/lib/Text/Tradition.pm +++ b/lib/Text/Tradition.pm @@ -1,5 +1,6 @@ package Text::Tradition; +use JSON qw / decode_json /; use Module::Load; use Moose; use Text::Tradition::Collation; @@ -37,6 +38,7 @@ has 'name' => ( has 'language' => ( is => 'rw', isa => 'Str', + predicate => 'has_language', ); has 'stemmata' => ( @@ -57,7 +59,11 @@ around 'add_witness' => sub { my $orig = shift; my $self = shift; # TODO allow add of a Witness object? - my $new_wit = Text::Tradition::Witness->new( @_ ); + my %args = @_ == 1 ? %{$_[0]} : @_; + $args{'tradition'} = $self; + $args{'language'} = $self->language + if( $self->language && !exists $args{'language'} ); + my $new_wit = Text::Tradition::Witness->new( %args ); $self->$orig( $new_wit->sigil => $new_wit ); return $new_wit; }; @@ -238,34 +244,14 @@ is( scalar $s->witnesses, 3, "object has three witnesses again" ); sub BUILD { my( $self, $init_args ) = @_; + + # First, make a collation object. This will use only those arguments in + # init_args that apply to the collation. + my $collation = Text::Tradition::Collation->new( %$init_args, + 'tradition' => $self ); + $self->_save_collation( $collation ); - if( exists $init_args->{'witnesses'} ) { - # We got passed an uncollated list of witnesses. Make a - # witness object for each witness, and then send them to the - # collator. - my $autosigil = 0; - foreach my $wit ( %{$init_args->{'witnesses'}} ) { - # Each item in the list is either a string or an arrayref. - # If it's a string, it is a filename; if it's an arrayref, - # it is a tuple of 'sigil, file'. Handle either case. - my $args; - if( ref( $wit ) eq 'ARRAY' ) { - $args = { 'sigil' => $wit->[0], - 'file' => $wit->[1] }; - } else { - $args = { 'sigil' => chr( $autosigil+65 ), - 'file' => $wit }; - $autosigil++; - } - $self->witnesses->add_witness( $args ); - # TODO Now how to collate these? - } - } else { - # Else we need to parse some collation data. Make a Collation object - my $collation = Text::Tradition::Collation->new( %$init_args, - 'tradition' => $self ); - $self->_save_collation( $collation ); - + if( exists $init_args->{'input'} ) { # Call the appropriate parser on the given data my @format_standalone = qw/ Self CollateText CollateX CTE JSON TEI Tabular /; my @format_basetext = qw/ KUL /; @@ -295,6 +281,26 @@ sub BUILD { $mod->can('parse')->( $self, $init_args ); } } + return $self; +} + +=head2 add_json_witnesses( $jsonstring, $options ) + +Adds a set of witnesses from a JSON array specification. This is a wrapper +to parse the JSON and call add_witness (with the specified $options) for +each element therein. + +=cut + +sub add_json_witnesses { + my( $self, $jsonstr, $extraopts ) = @_; + my $witarray = decode_json( $jsonstr ); + foreach my $witspec ( @$witarray ) { + my $opts = $extraopts || {}; + $opts->{'sourcetype'} = 'json'; + $opts->{'object'} = $witspec; + $self->add_witness( $opts ); + } } =head2 add_stemma( $dotfile )