File Coverage

blib/lib/Locale/Maketext/Extract/Plugin/FormFu.pm
Criterion Covered Total %
statement 74 83 89.1
branch 17 26 65.3
condition 7 9 77.7
subroutine 11 11 100.0
pod 2 2 100.0
total 111 131 84.7


line stmt bran cond sub pod time code
1             package Locale::Maketext::Extract::Plugin::FormFu;
2             $Locale::Maketext::Extract::Plugin::FormFu::VERSION = '1.00';
3 4     4   26 use strict;
  4         8  
  4         165  
4 4     4   21 use base qw(Locale::Maketext::Extract::Plugin::Base);
  4         9  
  4         1650  
5              
6             # ABSTRACT: FormFu format parser
7              
8              
9             sub file_types {
10 18     18 1 73 return qw( yaml yml conf );
11             }
12              
13             sub extract {
14 9     9 1 17 my $self = shift;
15 9         15 my $data = shift;
16              
17 9         82 my $y = Locale::Maketext::Extract::Plugin::FormFu::Extractor->new();
18 9         49 $y->load($data);
19              
20 6         124 foreach my $entry ( @{ $y->found } ) {
  6         28  
21 17         65 $self->add_entry(@$entry);
22             }
23              
24             }
25              
26             package Locale::Maketext::Extract::Plugin::FormFu::Extractor;
27             $Locale::Maketext::Extract::Plugin::FormFu::Extractor::VERSION = '1.00';
28 4     4   24 use base qw(YAML::Loader);
  4         8  
  4         5924  
29              
30             #===================================
31             sub new {
32             #===================================
33 9     9   13 my $class = shift;
34 9         63 my $self = $class->SUPER::new(@_);
35 9         92 $self->{found} = [];
36 9         19 return $self;
37             }
38              
39             #===================================
40             sub _check_key {
41             #===================================
42 28     28   33 my $self = shift;
43 28         46 my ( $key, $value, $line ) = @_;
44 28         29 my ( $str, $vars );
45 28 100       51 if ( ref $value ) {
46 8 100       34 return if ref $value ne 'ARRAY';
47 4         8 $str = shift @$value;
48 4         16 $vars = join( ', ', @$value );
49             }
50             else {
51 20         25 $str = $value;
52             }
53             return
54 24 100 66     216 unless $key
      66        
55             && $key =~ /_loc$/
56             && defined $str;
57 17         19 push @{ $self->{found} }, [ $str, $line, $vars ];
  17         129  
58             }
59              
60             #===================================
61             sub _parse_mapping {
62             #===================================
63 12     12   2804 my $self = shift;
64 12         20 my ($anchor) = @_;
65 12         20 my $mapping = {};
66 12         38 $self->anchor2node->{$anchor} = $mapping;
67 12         84 my $key;
68 12   100     32 while ( not $self->done
69             and $self->indent == $self->offset->[ $self->level ] )
70             {
71              
72             # If structured key:
73 18 50       512 if ( $self->{content} =~ s/^\?\s*// ) {
    50          
    50          
74 0         0 $self->preface( $self->content );
75 0         0 $self->_parse_next_line(YAML::Loader::COLLECTION);
76 0         0 $key = $self->_parse_node();
77 0         0 $key = "$key";
78             }
79              
80             # If "default" key (equals sign)
81             elsif ( $self->{content} =~ s/^\=\s*// ) {
82 0         0 $key = YAML::Loader::VALUE;
83             }
84              
85             # If "comment" key (slash slash)
86             elsif ( $self->{content} =~ s/^\=\s*// ) {
87 0         0 $key = YAML::Loader::COMMENT;
88             }
89              
90             # Regular scalar key:
91             else {
92 18         71 $self->inline( $self->content );
93 18         219 $key = $self->_parse_inline();
94 18         1681 $key = "$key";
95 18         58 $self->content( $self->inline );
96 18         204 $self->inline('');
97             }
98              
99 18 50       173 unless ( $self->{content} =~ s/^:\s*// ) {
100 0         0 $self->die('YAML_LOAD_ERR_BAD_MAP_ELEMENT');
101             }
102 18         52 $self->preface( $self->content );
103 18         196 my $line = $self->line;
104 18         136 $self->_parse_next_line(YAML::Loader::COLLECTION);
105 18         2668 my $value = $self->_parse_node();
106 18 50       2549 if ( exists $mapping->{$key} ) {
107 0         0 $self->warn('YAML_LOAD_WARN_DUPLICATE_KEY');
108             }
109             else {
110 18         46 $mapping->{$key} = $value;
111 18         45 $self->_check_key( $key, $value, $line );
112             }
113             }
114 12         227 return $mapping;
115             }
116              
117             #===================================
118             sub _parse_inline_mapping {
119             #===================================
120 6     6   928 my $self = shift;
121 6         9 my ($anchor) = @_;
122 6         13 my $node = {};
123 6         12 my $start_line = $self->{_start_line};
124              
125 6         18 $self->anchor2node->{$anchor} = $node;
126              
127 6 50       68 $self->die('YAML_PARSE_ERR_INLINE_MAP')
128             unless $self->{inline} =~ s/^\{\s*//;
129 6         35 while ( not $self->{inline} =~ s/^\s*\}// ) {
130 10         37 my $key = $self->_parse_inline();
131 10 50       896 $self->die('YAML_PARSE_ERR_INLINE_MAP')
132             unless $self->{inline} =~ s/^\: \s*//;
133 10         27 my $value = $self->_parse_inline();
134 10 50       854 if ( exists $node->{$key} ) {
135 0         0 $self->warn('YAML_LOAD_WARN_DUPLICATE_KEY');
136             }
137             else {
138 10         27 $node->{$key} = $value;
139 10         29 $self->_check_key( $key, $value, $start_line );
140             }
141 10 100       31 next if $self->inline =~ /^\s*\}/;
142 4 50       67 $self->die('YAML_PARSE_ERR_INLINE_MAP')
143             unless $self->{inline} =~ s/^\,\s*//;
144             }
145 6         92 return $node;
146             }
147              
148             #===================================
149             sub _parse_next_line {
150             #===================================
151 30     30   1433 my $self = shift;
152 30         72 $self->{_start_line} = $self->line;
153 30         258 $self->SUPER::_parse_next_line(@_);
154             }
155              
156             #===================================
157             sub found {
158             #===================================
159 6     6   10 my $self = shift;
160 6         26 return $self->{found};
161             }
162              
163              
164             1;
165              
166             __END__