X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FText%2FTradition%2FCollation%2FReading.pm;h=9d964213171c5b5b56aa08510a3ad7e264d58c84;hb=10e4b1acc8fc6607456481c568da930983efac33;hp=5a60823e4b5728b82064beede67019cdfb1ce8ba;hpb=aff524fcaed9a44206cd32d84ec7e2126fe11968;p=scpubgit%2Fstemmatology.git diff --git a/lib/Text/Tradition/Collation/Reading.pm b/lib/Text/Tradition/Collation/Reading.pm index 5a60823..9d96421 100644 --- a/lib/Text/Tradition/Collation/Reading.pm +++ b/lib/Text/Tradition/Collation/Reading.pm @@ -1,12 +1,21 @@ package Text::Tradition::Collation::Reading; use Moose; +use Moose::Util::TypeConstraints; use JSON qw/ from_json /; use Module::Load; use Text::Tradition::Error; +use XML::Easy::Syntax qw( $xml10_name_rx $xml10_namestartchar_rx ); use YAML::XS; use overload '""' => \&_stringify, 'fallback' => 1; +subtype 'ReadingID', + as 'Str', + where { $_ =~ /\A$xml10_name_rx\z/ }, + message { 'Reading ID must be a valid XML attribute string' }; + +no Moose::Util::TypeConstraints; + =head1 NAME Text::Tradition::Collation::Reading - represents a reading (usually a word) @@ -79,7 +88,7 @@ has 'collation' => ( has 'id' => ( is => 'ro', - isa => 'Str', + isa => 'ReadingID', required => 1, ); @@ -184,16 +193,25 @@ around BUILDARGS => sub { 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->{'id'} = '__START__'; # Change the ID to ensure we have only one $args->{'text'} = '#START#'; $args->{'rank'} = 0; } elsif( exists $args->{'is_end'} ) { - $args->{'id'} = '#END#'; # Change the ID to ensure we have only one + $args->{'id'} = '__END__'; # Change the ID to ensure we have only one $args->{'text'} = '#END#'; } elsif( exists $args->{'is_ph'} ) { $args->{'text'} = $args->{'id'}; } + # Backwards compatibility for non-XMLname IDs + my $rid = $args->{'id'}; + $rid =~ s/\#/__/g; + $rid =~ s/[\/,]/./g; + if( $rid !~ /^$xml10_namestartchar_rx/ ) { + $rid = 'r'.$rid; + } + $args->{'id'} = $rid; + $class->$orig( $args ); };