@@ -84,6 +84,8 @@ sub _read_configuration {
8484 File::Find::Rule-> file()-> name( ' *.yaml' )-> in($path .' /countries' );
8585
8686 $self -> {templates } = {};
87+ $self -> {component_aliases } = {};
88+
8789 foreach my $filename ( sort @a_filenames ){
8890 try {
8991 my $rh_templates = LoadFile($filename );
@@ -102,16 +104,23 @@ sub _read_configuration {
102104
103105 try {
104106 my @c = LoadFile($path . ' /components.yaml' );
105- # warn Dumper \@c;
107+
108+ foreach my $rh_c (@c ){
109+ if (defined ($rh_c -> {aliases })){
110+ foreach my $alias (@{$rh_c -> {aliases }}){
111+ $self -> {component_aliases }{$alias } = $rh_c -> {name };
112+ }
113+ }
114+ }
115+ # warn Dumper $self->{component_aliases};
116+ # warn Dumper \@c;
106117 $self -> {ordered_components } =
107118 [ map { $_ -> {name } => ($_ -> {aliases } ? @{$_ -> {aliases }} : ()) } @c ];
108119 }
109120 catch {
110121 warn " error parsing component configuration: $_ " ;
111122 };
112123
113- # print STDERR Dumper $self->{ordered_components};
114-
115124 $self -> {state_codes } = {};
116125 if ( -e $path . ' /state_codes.yaml' ){
117126 try {
@@ -148,31 +157,61 @@ sub format_address {
148157 || $self -> _determine_country_code($rh_components )
149158 || ' ' ;
150159
160+ # set the aliases
161+ foreach my $alias (keys %{$self -> {component_aliases }}){
162+ if (defined ($rh_components -> {$alias })){
163+ $rh_components -> {$self -> {component_aliases }-> {$alias }} =
164+ $rh_components -> {$alias };
165+ }
166+ }
167+
168+ # determine the template
151169 my $rh_config = $self -> {templates }{uc ($cc )} || $self -> {templates }{default };
152170 my $template_text = $rh_config -> {address_template };
153171
154172 # print STDERR "t text " . Dumper $template_text;
155173 # print STDERR "comp " . Dumper $rh_components;
156174
157175 # do we have the minimal components for an address?
158- # or should we instead fall back?
159- $self -> _apply_replacements($rh_components , $rh_config -> {replace });
160- $self -> _add_state_code($rh_components );
161-
176+ # or should we instead use the fallback template?
162177 if (!$self -> _minimal_components($rh_components )){
163178 $template_text =
164179 $rh_config -> {fallback_template }
165180 || $self -> {templates }{default }{fallback_template }
166181 || $rh_config -> {address_template }; # if there is no fallback
167182 }
168183
169- $rh_components -> {attention } = join (' , ' , map { $rh_components -> {$_ } } @{ $self -> _find_unknown_components($rh_components )} );
184+ # clean up the components
185+ $self -> _apply_replacements($rh_components , $rh_config -> {replace });
186+ $self -> _add_state_code($rh_components );
170187
188+ # add the attention
189+ $rh_components -> {attention } = join (' , ' , map { $rh_components -> {$_ } } @{ $self -> _find_unknown_components($rh_components )} );
190+ # warn Dumper $rh_components;
191+ # render it
171192 my $text = $self -> _render_template($template_text , $rh_components );
193+ $text = $self -> _postformat($text ,$rh_config -> {postformat_replace });
172194 $text = $self -> _clean($text );
173195 return $text ;
174196}
175197
198+ sub _postformat {
199+ my $self = shift ;
200+ my $text = shift ;
201+ my $raa_rules = shift ;
202+
203+ foreach my $ra_fromto ( @$raa_rules ){
204+ try {
205+ my $regexp = qr /$ra_fromto ->[0]/ ;
206+ $text =~ s / $regexp/ $ra_fromto ->[1]/ ;
207+ }
208+ catch {
209+ warn " invalid replacement: " . join (' , ' , @$ra_fromto )
210+ };
211+ }
212+ return $text ;
213+ }
214+
176215sub _minimal_components {
177216 my $self = shift ;
178217 my $rh_components = shift || return ;
@@ -273,7 +312,6 @@ sub _render_template {
273312 my $selected = first { length ($_ ) } split (/ \s *\|\|\s */ , $text );
274313 return $selected ;
275314 };
276-
277315 $template_content =~ s /\n / , / sg ;
278316 my $output = $tache -> render($template_content , $context );
279317
@@ -290,35 +328,8 @@ sub _find_unknown_components {
290328 my %h_known = map { $_ => 1 } @{ $self -> {ordered_components } };
291329 my @a_unknown = grep { !exists ($h_known {$_ }) } keys %$components ;
292330
331+ # warn Dumper \@a_unknown;
293332 return \@a_unknown ;
294333}
295334
296- sub _default_algo { # the ultimate fallback
297- my $self = shift ;
298- my $cs = shift || return ;
299-
300- my @values = ();
301-
302- # upper case country code
303- if ( my $ccode = $cs -> {country_code } ){
304- $cs -> {country_code } = uc ($ccode );
305- }
306-
307- # now do the location pieces
308- foreach my $k (@{ $self -> {ordered_components } }){
309- next unless ( exists ($cs -> {$k }) );
310- next if ( $k eq ' country_code' && $cs -> {' country' } );
311- push (@values , $cs -> {$k });
312- }
313-
314- # get the ones we missed previously
315- # FIXME - this is bad, we're just shoving stuff to the start
316- foreach my $k ( @{ $self -> _find_unknown_components($cs ) } ) {
317- warn " not sure where to put this: $k " ;
318- # # add to the front
319- unshift (@values , $cs -> {$k });
320- }
321- return join (' , ' , @values );
322- }
323-
3243351;
0 commit comments