lib/Book/Bilingual/Reader.pm

filter: Statementstmt 92.7% 115 / 124 Branchbran 82.5% 33 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 15 / 18 Podpod 33.3% 1 / 3 totaltotal 88.6% 164 / 185 CC 23 SCAR 12.2
CC
23
Cov
90%
_render
6.0
_load_dlineset
5.1
_render_pointed
4.0
CRAP 3.4
« blib/lib/Book/Bilingual/Dline.pm ↑ Summary blib/lib/Book/Bilingual/Dlineset.pm »
1 package Book::Bilingual::Reader;
2 # ABSTRACT: A book reader class
3 1 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Book::Bilingual;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Book::Bilingual::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Path::Tiny qw/path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶has 'book' => sub { Book::Bilingual->new}; # Book::Bilingual
Subroutine __ANON__: not called CC=1 SCAR=1.2
10 1 has 'file'; # Book File
11 1 has '_ptr'; # Current pointer
12 1 has '_chapter_dlines'; # Href { loc => Dline } in chapter
13 1 has '_curr_dlineset'; # Href { loc => Dline } of current dlineset
14  
15 sub new { ## ($path :Path)
16 14 ▶ croak 'Need args ($path)' unless @_ > 1;
Subroutine new: called CC=2 SCAR=7.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless @_ > 1 0 14 14
17 14 my $self = $_[0]->SUPER::new({ file => path($_[1]) });
18  
19 # Initialize pointer
20 14 $self->_ptr([0,0,0]);
21  
22 # Setup book
23 14 $self->book(Book::Bilingual::File->new($_[1])->book);
24  
25 # Load current chapter
26 14 $self->_load_chapter();
27  
28 # Load current Dlineset
29 14 $self->_load_dlineset();
30  
31 14 return $self;
32 }
33 sub _load_chapter { ## () :> Self
34 14 ▶ my ($self) = @_;
Subroutine _load_chapter: called CC=2 SCAR=6.9
35  
36 # Fetch chapter being pointed to
37 14 my $ch_idx = $self->_ptr->[0];
38 14 my $chapter = $self->book->chapters->[$ch_idx];
39  
40 # Empty the target hashref
41 14 $self->{_chapter_dlines} = {};
42  
43 # Iterate over all Dlinesets in chapter
44 14 my $dset_idx = 0;
45 14 foreach my $dset ($chapter->number, $chapter->title, @{$chapter->body}) {
46 # Store the default/target dline into _chapter_dlines
47 84 my $dline_idx = $dset->dline_count - 1;
48 # say "$ch_idx.$dset_idx.$dline_idx: ". $dset->target->str;
49 84 $self->{_chapter_dlines}{"$ch_idx.$dset_idx.$dline_idx"} = $dset->target;
50  
51 84 $dset_idx++;
52 }
53  
54 14 return $self;
55 }
56 sub _load_dlineset { ## () :> Self
57 14 ▶ my ($self) = @_;
Subroutine _load_dlineset: called CC=5 SCAR=16.2
58  
59 # Fetch dlineset being pointed to
60 14 my $ch_idx = $self->_ptr->[0];
61 14 my $chapter = $self->book->chapters->[$ch_idx];
62  
63 # Empty the target hashref
64 14 $self->{_curr_dlineset} = {};
65  
66 # Iterate over all Dlinesets in chapter
67 14 my $dset_idx = $self->_ptr->[1];
68 14 my $dset;
69 14 ▶ $dset = $chapter->number if $dset_idx eq 0;
Branch: 1 branch
BranchTrueFalseTotal
if $dset_idx eq 0 14 0 14
70 14 ▶ $dset = $chapter->title if $dset_idx eq 1;
Branch: 1 branch
BranchTrueFalseTotal
if $dset_idx eq 1 0 14 14
71 14 ▶ $dset = $chapter->body->[$dset_idx-2] if $dset_idx > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $dset_idx > 1 0 14 14
72  
73 14 my $dline_idx = 0;
74 14 foreach my $dline (@{$dset->set}) {
75 # say "$ch_idx.$dset_idx.$dline_idx: ". $dline->str;
76 42 $self->{_curr_dlineset}{"$ch_idx.$dset_idx.$dline_idx"} = $dline;
77 42 $dline_idx++;
78 }
79  
80 14 return $self;
81 }
82  
83 sub html { ## () :> HTML
84 1 ▶ my ($self) = @_;
Subroutine html: called CC=4 SCAR=13.9
Pod undocumented
85  
86 1 my $ch_idx = $self->_ptr->[0];
87 1 my $dset_idx = $self->_ptr->[1];
88  
89 # Regex that matches other locations in the same dlineset
90 # e.g. '0.1.2' will match qr/^0\.1\.\d+$/
91 1 my $curr_dlineset_re = qr/^$ch_idx\.$dset_idx\.\d+$/;
92  
93 # Use the chapter Dline, except if the Dline is in the same Dlineset
94 # as the pointer, use the pointed to Dline
95 my @html = map {
96 6 my $loc = $_;
97 6 ▶ my $pointed = $loc =~ $curr_dlineset_re ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$loc =~ /$curr_dlineset_re/u ? : 1 5 6
98 my $dline = $pointed
99 1 ? $self->_curr_dlineset->{join('.',@{$self->_ptr})}
100 6 ▶ : $self->_chapter_dlines->{$loc};
Branch: 1 branch
BranchTrueFalseTotal
$pointed ? : 1 5 6
101  
102 6 my $ptr = $loc;
103 6 $ptr =~ s/\.(\d+)$/\.0/;
104  
105 # Render Dline depending on whether it is pointed to
106 6 ▶ $pointed ? _render_pointed($dline->class, $dline->str, $ptr)
Branch: 1 branch
BranchTrueFalseTotal
$pointed ? : 1 5 6
107 : _render_normal($dline->class, $dline->str, $ptr);
108 1 } sort keys %{$self->_chapter_dlines};
109  
110 # Render to HTML
111 1 my $html = join '', @html;
112  
113 # say "\n$html\n";
114 1 return $html;
115 }
116 sub _render {
117 0 ▶ my ($dline) = @_;
Subroutine _render: not called CC=2 SCAR=17.9
118  
119 0 my $seg_idx = 0;
120 my @spans = map {
121 0 ▶ my $class = $seg_idx++ % 2 ? 'tgt-lang' : 'src-lang';
Branch: 1 branch
BranchTrueFalseTotal
$seg_idx++ % 2 ? : 0 0 0
122 0 "<span class=\"$class\"> $_</span>";
123 } split(' /', $dline->str);
124  
125 0 return ' <div class="'.$dline->class.'">'.join('',@spans).'</div>';
126 }
127 sub _render_normal { ## ($class,$str,$ptr)
128 12 ▶ my ($class,$str,$ptr) = @_;
Subroutine _render_normal: called CC=4 SCAR=13.9
129  
130 12 $str =~ s/^ \///; # Remove language mark
131 12 $str .= ' '; # Add a space at the end of the line
132  
133 12 ▶ return "\n <h1 data-ptr=\"$ptr\" class=\"".$class.'">'.$str."</h1>\n"
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "chapter-number" 4 8 12
134 if $class eq 'chapter-number';
135  
136 8 ▶ return "\n <h2 data-ptr=\"$ptr\" class=\"".$class.'">'.$str."</h2>"
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "chapter-title" 2 6 8
137 if $class eq 'chapter-title';
138  
139 6 ▶ return "\n\n <br/><span data-ptr=\"$ptr\" class=\"".$class.'">'.$str.'</span>'
Branch: 1 branch
BranchTrueFalseTotal
if $class eq "paragraph-start" 3 3 6
140 if $class eq 'paragraph-start';
141  
142 3 return "<span data-ptr=\"$ptr\">$str</span>";
143 }
144 sub _render_pointed { ## ($class,$str)
145 3 ▶ my ($class,$str,$ptr) = @_;
Subroutine _render_pointed: called CC=4 SCAR=13.9
146  
147 3 my $seg_class = '';
148 my @spans = map {
149 # Toggles segment class.
150 # One of: 'src-lang'|''. Always start as 'src-lang'.
151 3 ▶ $seg_class = $seg_class eq '' ? 'src-lang' : '';
Branch: 1 branch
BranchTrueFalseTotal
$seg_class eq '' ? : 3 1 4
152  
153 # If segment class is src-lang, return a span else as is
154 4 ▶ my $span = $seg_class ? "<span class=\"$seg_class\">$_ </span>" : $_;
Branch: 1 branch
BranchTrueFalseTotal
$seg_class ? : 3 1 4
155  
156 # Return span only if segment is non-empty, else empty string
157 4 ▶ $_ ? $span : '';
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 4 0 4
158 } split(' /', $str);
159  
160 3 my $wrapped = "<span id=\"Ptr\" class=\"pointed\">".join('',@spans)."</span>";
161  
162 3 return _render_normal($class,$wrapped,$ptr);
163 }
164  
165 sub _next_ptr { ## () :> undef | [Ptr,diff_chapter,diff_dlineset,diff_dline]
166 5 ▶ my ($self) = @_;
Subroutine _next_ptr: called CC=4 SCAR=13.9
167  
168 5 my $ptr_0 = $self->_ptr->[0];
169 5 my $ptr_1 = $self->_ptr->[1];
170 5 my $ptr_2 = $self->_ptr->[2];
171  
172 5 my $max_chapter_idx
173 = $self->book->chapter_count - 1;
174 5 my $max_chapter_dlineset_idx
175 = $self->book->chapter_dlineset_count($ptr_0) - 1;
176 5 my $max_chapter_dlineset_dline_idx
177 = $self->book->chapter_dlineset_dline_len($ptr_0, $ptr_1) - 1;
178  
179 # Case 1: NOT end of current dlineset
180 # Return pointer to next dline in dlineset
181 5 ▶ return [join('.', $ptr_0, $ptr_1, $ptr_2+1), 0, 0, 1]
Branch: 1 branch
BranchTrueFalseTotal
if $ptr_2 < $max_chapter_dlineset_dline_idx 2 3 5
182 if $ptr_2 < $max_chapter_dlineset_dline_idx;
183  
184 # Case 2: End of current dlineset, NOT end of dlinesets in chapter
185 # Return pointer to first dline in next dlineset
186 3 ▶ return [join('.', $ptr_0, $ptr_1+1, 0), 0, 1, 1]
Branch: 1 branch
BranchTrueFalseTotal
if $ptr_1 < $max_chapter_dlineset_idx 1 2 3
187 if $ptr_1 < $max_chapter_dlineset_idx;
188  
189 # Case 3: End of chapter dlineset, NOT end of chapters in book
190 # Return pointer to first dline in next chapter
191 2 ▶ return [join('.', $ptr_0+1, 0, 0), 0, 1, 1]
Branch: 1 branch
BranchTrueFalseTotal
if $ptr_0 < $max_chapter_idx 1 1 2
192 if $ptr_0 < $max_chapter_idx;
193  
194 # Case 4: End of book chapters
195 # Return undef
196 1 return undef;
197 }
198 sub _prev_ptr { ## () :> undef | [PtrStr,diff_chapter,diff_dlineset,diff_dline]
199 4 ▶ my ($self) = @_;
Subroutine _prev_ptr: called CC=4 SCAR=13.9
200  
201 4 my $ptr_0 = $self->_ptr->[0]; # Chapter_idx
202 4 my $ptr_1 = $self->_ptr->[1]; # Dlineset_idx
203 4 my $ptr_2 = $self->_ptr->[2]; # Dline_idx
204  
205 4 my $max_chapter_idx
206 = $self->book->chapter_count - 1;
207  
208 # Case 1: NOT at first Dline in current Dlineset
209 # Return pointer to prev dline in Dlineset
210 4 ▶ return [join('.', $ptr_0, $ptr_1, $ptr_2-1), 0, 0, 1]
Branch: 1 branch
BranchTrueFalseTotal
if $ptr_2 > 0 1 3 4
211 if $ptr_2 > 0;
212  
213 # Case 2: At first Dline in current Dlineset, NOT at first Dlineset in Chapter
214 # Return pointer to prev Dlineset in Chapter
215 3 ▶ if ($ptr_1 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ptr_1 > 0) 1 2 3
216 1 my $Curr_chapter_Prev_dlineset_Max_dline_idx
217 = $self->book->chapter_dlineset_dline_len($ptr_0, $ptr_1-1) - 1;
218 1 return [join('.', $ptr_0,
219 $ptr_1-1,
220 $Curr_chapter_Prev_dlineset_Max_dline_idx), 0, 0, 1]
221 }
222  
223 # Case 3: At first Dlineset in chapter, NOT at first Chapter
224 # Return pointer to last Dline in last Dlineset in prev Chapter
225 2 ▶ if ($ptr_0 > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ptr_0 > 0) 1 1 2
226 1 my $Prev_chapter_Max_dlineset_idx
227 = $self->book->chapter_dlineset_count($ptr_0-1) - 1;
228 1 my $Prev_chapter_Max_dlineset_Max_dline_idx
229 = $self->book->chapter_dlineset_dline_len(
230 $ptr_0-1, $Prev_chapter_Max_dlineset_idx) - 1;
231 1 return [join('.', $ptr_0-1,
232 $Prev_chapter_Max_dlineset_idx,
233 $Prev_chapter_Max_dlineset_Max_dline_idx), 0, 0, 1]
234 }
235  
236 # Case 4: At start of book
237 return undef
238 1 }
239 sub _max_loc {
240 1 ▶ (sort _cmp_loc (keys %{$_[0]}))[-1]
Subroutine _max_loc: called CC=1 SCAR=0.0
241 }
242 sub _cmp_loc ($$) {
243 5 ▶ my ($a,$b) = @_;
Subroutine _cmp_loc: called CC=1 SCAR=0.0
244  
245 # Convert each element to a zero-prefixed 5 digit string
246 5 my $A = sprintf("%05d%05d%05d",split('\.',$a));
247 5 my $B = sprintf("%05d%05d%05d",split('\.',$b));
248  
249 5 return $A cmp $B;
250 }
251  
252 0 ▶sub ptr { join '.', @{shift->_ptr} }
Subroutine ptr: not called CC=1 SCAR=6.9
Pod undocumented
253  
254 =encoding utf8
255 =cut
256 =head1 ATTRIBUTES
257 =cut
258 =head2 _chapter_dlines
259  
260 The _chapter_dlines attribute stores the default Dline for each Dlineset
261 that makes up the chapter. In other words it has the Dline that should
262 be visible. It is a Hashref of a location to Dline. Example:
263  
264 _chapter_dlines = {
265 '0.0.2' => Dline({ class => 'chapter-number', str => ' /Chapter One' });
266 '0.1.2' => Dline({ class => 'chapter-title', str => ' /A Great Surprise' });
267 '0.2.13' => Dline({ class => 'paragraph-start',str => ' /"Mother, have ...' });
268 ...
269 }
270 =cut
271 =head2 _curr_dlineset
272  
273 The _curr_dlineset attribute stores the Dlineset that is currently being
274 pointed to. In other words, it is the sentence that is currently being
275 read and being transformed from the native language to the target
276 language. Example:
277  
278 _curr_dlineset = {
279 '0.1.0' => Dline({ class => 'chapter-title', str => 'ความประหลาดใจที่ยอดเยี่ยม /' });
280 '0.1.1' => Dline({ class => 'chapter-title', str => 'ความประหลาดใจที่ /Great' });
281 '0.1.2' => Dline({ class => 'chapter-title', str => ' /A Great Surprise' });
282 }
283 =cut
284 =head1 METHODS
285 =cut
286 =head2 _load_chapter() :> Self
287  
288 The _load_chapter() private method loads the chapter's default/targer
289 Dlines into the _chapter_dlines attribute. It loads the chapter
290 currently pointed to by the objects '_ptr' attribute.
291  
292 =cut
293 =head2 html() :> HTML
294  
295 The html() public method renders the current chapter into HTML.
296  
297 =cut
298 =head2 _render(Dline) :> HTML
299  
300 The _render(Dline) private function renders the given Dline into a
301 suitable HTML string.
302  
303 =cut
304 =head2 _render_normal(Dline) :> HTML
305  
306 The _render_normal(Dline) private method renders the given Dline into
307 the normal HTML output.
308  
309 This method reads the class of the given Dline and renders the
310 associated element. The Dline class is responsible for font-size and
311 white-space associated with the line but not colors.
312  
313 =cut
314 =head2 _render_pointed(Dline) :> HTML
315  
316 The _render_pointed(Dline) private method renders the pointed-to line.
317 The pointed-to line entire broken line is displayed with a background.
318 The source language has one set of font-color and background-color while
319 the target language has another set of font-color and background-color.
320  
321 We implement this by first wrapping the entire line in a span with class
322 "current-line". Then each segment is wrapped in a span as well. Segments
323 in the source language is has a class of src-lang. Segments in the
324 target language are unlabeled.
325  
326 Then create a new Dline with the same class as the input, but with the
327 generated HTML as the str. Then call _render_normal on this new Dline to
328 generate the final HTML.
329  
330 =cut
331  
332 =head2 _next_ptr() :> undef | [Ptr,diff_chapter,diff_dlineset,diff_dline]
333  
334 The _next_ptr() private method returns undef if no next pointer is
335 available. Otherwise, it returns the next pointer (Ptr), several boolean
336 flags indicating whether the next pointer points to a location in a
337 different chapter, a different dlineset or a different dline.
338  
339 =cut
340 =head2 _prev_ptr() :> undef | [PtrStr,diff_chapter,diff_dlineset,diff_dline]
341  
342 The _prev_ptr() private method returns undef if no previous pointer is
343 available. Otherwise, it returns the previous pointer (Ptr), several boolean
344 flags indicating whether the previous pointer points to a location in a
345 different chapter, a different dlineset or a different dline.
346  
347 =cut
348 =head2 _max_loc($href)
349  
350 Returns the max location of a href that has locations as key.
351  
352 =cut
353 =head2 _cmp_loc($a,$b)
354  
355 Returns -1, 0 or 1 depending on whether the left argument is stringwise
356 less than, equal to or greater than the right argument.
357  
358 $a < $b : -1
359 $a = $b : 0
360 $a > $b : 1
361  
362 =cut
363  
364 1 1;
« blib/lib/Book/Bilingual/Dline.pm ↑ Summary blib/lib/Book/Bilingual/Dlineset.pm »