File Coverage

blib/lib/Data/QuickMemoPlus/Reader.pm
Criterion Covered Total %
statement 78 82 95.1
branch 20 22 90.9
condition 1 2 50.0
subroutine 12 12 100.0
pod 2 4 50.0
total 113 122 92.6


line stmt bran cond sub pod time code
1             package Data::QuickMemoPlus::Reader;
2 3     3   154268 use 5.010;
  3         32  
3 3     3   17 use strict;
  3         5  
  3         60  
4 3     3   16 use warnings;
  3         4  
  3         68  
5 3     3   14 use Carp;
  3         6  
  3         180  
6 3     3   2104 use JSON;
  3         31691  
  3         18  
7 3     3   2532 use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
  3         255062  
  3         561  
8              
9             our $VERSION = "0.12";
10              
11 3     3   31 use Exporter qw(import);
  3         8  
  3         2609  
12            
13             our @EXPORT_OK = qw( lqm_to_str lqm_to_txt );
14             our $IncludeHeader = 1;
15              
16             sub lqm_to_txt {
17 2     2 1 5340 my ( $lqm ) = @_;
18 2         9 my $files_converted = 0;
19 2 100       36 if (-d $lqm){
20 1         8 $lqm =~ s![/\\]+$!!;
21 1         122 foreach(glob qq("$lqm/*.lqm")){
22 6         29 $files_converted += _lqm_file_to_txt($_);
23             }
24             } else {
25 1         6 $files_converted = _lqm_file_to_txt($lqm);
26             }
27 2         17 return $files_converted;
28             }
29             sub _lqm_file_to_txt {
30 7     7   23 my ( $lqm_file ) = @_;
31 7         22 my $text = lqm_to_str($lqm_file);
32            
33 7 100       32 return 0 if not length($text);
34            
35 4         12 my $outfile = $lqm_file;
36 4         27 $outfile =~ s/lqm$/txt/i;
37 4 50       493 open my $fh, ">", $outfile or do {
38 0         0 carp "couldn't open $outfile\n";
39            
40 0         0 return 0;
41             };
42 4         40 print $fh $text;
43            
44 4         223 return 1;
45             }
46              
47             sub lqm_to_str {
48 15     15 1 7614 my ( $lqm_file ) = @_;
49 15 100       366 if (not -f $lqm_file){
50 1         40 carp "$lqm_file is not a file";
51            
52 1         577 return '';
53             }
54 14         54 my $note_created_time = "";
55 14 100       83 if ( $lqm_file =~ /(QuickMemo\+_(\d{6}_\d{6})(\(\d+\))?)/i) {
56 3         16 $note_created_time = $2;
57             }
58 14         57 my $json_str = extract_json_from_lqm( $lqm_file );
59            
60 14 100       108 return '' if not $json_str;
61            
62 8         37 my ($extracted_text, $note_category) = extract_text_from_json($json_str);
63 8         35 my $header = "Created date: $note_created_time\n";
64 8         27 $header .= "Category: $note_category\n";
65 8         23 $header .= "-"x79 . "\n";
66 8 100       29 $header = '' if not $IncludeHeader;
67            
68 8         50 return $header . $extracted_text;
69             }
70              
71             #####################################
72             #
73             sub extract_json_from_lqm {
74 15     15 0 966 my $lqm_file = shift;
75 15         120 my $lqm_zip = Archive::Zip->new();
76 15 100       624 unless ( $lqm_zip->read( $lqm_file ) == AZ_OK ) {
77 2         4072 carp "Error reading $lqm_file";
78             ####### to do: add the zip error to the warning?
79            
80 2         588 return "";
81             }
82 13         54077 my $jlqm_filename = "memoinfo.jlqm";
83 13         84 my $member = $lqm_zip->memberNamed( $jlqm_filename );
84 13 100       1048 if( not $member ){
85 2         267 carp "File not found: $jlqm_filename in archive $lqm_file";
86            
87 2         558 return "";
88             }
89 11         101 my ( $string, $status ) = $member->contents();
90 11 100       19317 if(not $status == AZ_OK){
91 2         135 carp "Error extracting $jlqm_filename from $lqm_file : Status = $status";
92            
93 2         547 return "";
94             }
95            
96 9         211 return $string;
97             }
98              
99             ###############################################
100             #
101             sub extract_text_from_json {
102 8     8 0 24 my $json_string = shift;
103            
104             ############# To do: eval this and trap errors.
105 8         476 my $href_memo = decode_json $json_string;
106 8 50       38 if (not $href_memo){
107 0         0 carp "Error decoding JSON file in lqm archive.";
108 0         0 return '','';
109             }
110 8         22 my $text = "";
111 8         17 foreach( @{$href_memo->{MemoObjectList}} ) {
  8         32  
112 8         28 $text .= $_->{DescRaw};
113 8         24 $text .= "\n";
114             }
115 8         24 my $category = $href_memo->{Category}->{CategoryName};
116 8   50     24 $category //= '';
117 8         67 $category =~ s/[^\w-]/_/g;
118 8         153 return $text, $category;
119             }
120             1;
121             __END__