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