blib/lib/WWW/Dict/Zdic.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 82 CC 3 SCAR 14.2
CC
3
Cov
0%
parse_content
12.0
define
2.0
BEGIN
2.0
CRAP 4.1
1 package WWW::Dict::Zdic;
2  
3 use warnings;
4 use strict;
5 use v5.8.0;
6 our $VERSION = '0.0.4';
7  
8 use base 'WWW::Dict';
9  
10 use WWW::Mechanize;
11 use Encode;
12 use URI::Escape;
13 use HTML::TagParser;
14 use HTML::Entities;
15 use Class::Field qw'field const';
16  
17 # Module implementation here
18  
19 field ua => -init => 'WWW::Mechanize->new()';
20 const dict_url => 'http://www.zdic.net';
21 field word => '';
22  
23 sub define {
24 my $self = shift;
25 my $word = shift;
26 $self->word($word);
27 $word = URI::Escape::uri_escape_utf8($word);
28 $word =~ s/%/Zdic/g;
29 my $url = $self->dict_url . "/zd/zi/${word}.htm";
30 $self->ua->get($url);
31 $self->parse_content( $self->ua->content() );
32 }
33  
34 sub parse_content {
35 my $self = shift;
36 my $html = HTML::TagParser->new;
37 $html->parse(shift);
38 my @def;
39 for my $elem ( $html->getElementsByTagName("div") ) {
40 my $attr = $elem->attributes;
41 next unless ( 'tab-page' eq ( $attr->{class} || '' ) );
42 my $innerText = $elem->innerText;
43 if (! Encode::is_utf8($innerText)) {
44 $innerText = Encode::decode('utf8', $innerText);
45 $innerText = decode_entities( $innerText );
46 }
47 my @d = grep { !/tabPane|^\s*$/ }
48 map {
49 s/--.*?Zdic.net.*?--//g;
50 s/\t//g;
51 $_;
52 }
53 split( /\r?\n/, $innerText );
54 push @def,
55 {
56 category => $d[0],
57 definition => join( "\n", @d[ 1 .. $#d ] )
58 };
59 }
60 return \@def;
61 }
62  
63 1; # Magic true value required at end of module
64 __END__