| 1 | package Net::Amazon::MechanicalTurk::Template::ReplacementTemplate; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use IO::File; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use Net::Amazon::MechanicalTurk::Template; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Net::Amazon::MechanicalTurk::DataStructure; | ||
| 8 | |||
| 9 | our $VERSION = '1.00'; | ||
| 10 | |||
| 11 | our @ISA = qw{ Net::Amazon::MechanicalTurk::Template }; | ||
| 12 | |||
| 13 | Net::Amazon::MechanicalTurk::Template::ReplacementTemplate->attributes(qw{ | ||
| 14 | tokens | ||
| 15 | }); | ||
| 16 | |||
| 17 | # | ||
| 18 | # The java command line tools for MechanicalTurk use Velocity for | ||
| 19 | # question templates. | ||
| 20 | # | ||
| 21 | # The class should handle some of the simple samples used for bulk loading | ||
| 22 | # in the java command line tools. | ||
| 23 | # | ||
| 24 | # For more powerful features PerlTemplate may be used. | ||
| 25 | # | ||
| 26 | |||
| 27 | sub compileSource { | ||
| 28 | my ($self, $text) = @_; | ||
| 29 | |||
| 30 | $self->templateSource($text); | ||
| 31 | |||
| 32 | # The loop keeps chopping of chunks of text from the front. | ||
| 33 | # It may pull out a chunk of text in the first regex group | ||
| 34 | # and the variable name to be replaced, will be in the 3rd or 4th. | ||
| 35 | # Variable names appear as ${variableName} or $variableName. | ||
| 36 | # If a nested variable is needed it must be in the bracket syntax | ||
| 37 | # with dots seperating keys or array indices (array indices | ||
| 38 | # start at 1). | ||
| 39 | |||
| 40 | my @tokens; | ||
| 41 | while ($text =~ s/^(.*?)(\${([^}]+)}|\$([a-zA-Z0-9\-_]+))//s) { | ||
| 42 | my $subText = $1; | ||
| 43 | my $var = $3; | ||
| 44 | if (!defined($var) or $var eq "") { | ||
| 45 | $var = $4; | ||
| 46 | } | ||
| 47 | my $varToken = $2; | ||
| 48 | $var =~ s/^\s+//; | ||
| 49 | $var =~ s/\s+$//; | ||
| 50 | if (defined($subText) and length($subText) > 0) { | ||
| 51 | $subText =~ s/\\{/{/g; | ||
| 52 | $subText =~ s/\\}/}/g; | ||
| 53 | push(@tokens, { type => 'text', text => $subText }); | ||
| 54 | } | ||
| 55 | push(@tokens, { type => 'var', var => $var, varToken => $varToken }); | ||
| 56 | } | ||
| 57 | |||
| 58 | if (length($text) > 0) { | ||
| 59 | my $subText = $text; | ||
| 60 | $subText =~ s/\\{/{/g; | ||
| 61 | $subText =~ s/\\}/}/g; | ||
| 62 | push(@tokens, { type => 'text', text => $subText }); | ||
| 63 | } | ||
| 64 | |||
| 65 | $self->tokens(\@tokens); | ||
| 66 | $self->compiled(1); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub merge { | ||
| 70 | my ($self, $params) = @_; | ||
| 71 | my $out = ''; | ||
| 72 | foreach my $token (@{$self->tokens}) { | ||
| 73 | if ($token->{type} eq 'text') { | ||
| 74 | $out .= $token->{text}; | ||
| 75 | } | ||
| 76 | else { | ||
| 77 | my $value = Net::Amazon::MechanicalTurk::DataStructure->getFirst($params, $token->{var}); | ||
| 78 | if (defined($value) and length($value) > 0) { | ||
| 79 | $out .= $value; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | return $out; | ||
| 84 | } | ||
| 85 | |||
| 86 | return 1; | ||