| 1 | package oEdtk::FixIdx; | ||||||||||
| 2 | 19 | ▶ | use base qw( Exporter ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 19 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 19 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 19 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 19 | ▶ | use feature 'say'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 19 | ▶ | use autodie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 19 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 19 | ▶ | use English '-no_match_vars'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 19 | ▶ | use File::Copy qw( copy ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 19 | ▶ | use File::Temp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 19 | ▶ | use Text::CSV qw( csv ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 19 | ▶ | use Data::Dumper; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 19 | ▶ | use Readonly; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 19 | our $VERSION = 2.1126; | |||||||||
| 17 | 19 | our @EXPORT_OK = qw( make_docs_even_numbered ); | |||||||||
| 18 | |||||||||||
| 19 | # An index (CSV .idx file) is a list of "docs" | ||||||||||
| 20 | # A doc is a list of rows with pages numbered from 1 to max_doc | ||||||||||
| 21 | |||||||||||
| 22 | 19 | Readonly my $IDX_LAST => -1; # magic index of last element of a list | |||||||||
| 23 | 19 | Readonly my $BAD_DOC_ID => -1; # cannot be a valid doc id | |||||||||
| 24 | 19 | Readonly my $IDX_ED_IDLDOC => 1; # id doc of the "lot" | |||||||||
| 25 | 19 | Readonly my $IDX_ED_IDSEQPG => 2; # page sequence in the "lot" | |||||||||
| 26 | 19 | Readonly my $IDX_ED_IDDEST => 6; # page sequence in the "lot" | |||||||||
| 27 | 19 | Readonly my $IDX_ED_SEQDOC => 3; # id of the document in the sequence | |||||||||
| 28 | 19 | Readonly my $IDX_ED_SEQPGDOC => 39; # current number of the page in the doc | |||||||||
| 29 | 19 | Readonly my $IDX_ED_NBPGDOC => 40; # total number of pages in the doc | |||||||||
| 30 | |||||||||||
| 31 | # add one page at the end of a doc and update total number of pages | ||||||||||
| 32 | 0 | ▶ | sub add_one_page { | ||||||||
|
Subroutine
add_one_page: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 33 | 0 | my ($aoa) = @_; | |||||||||
| 34 | 0 | my @res; | |||||||||
| 35 | 0 | my $last_row = $aoa->[$IDX_LAST]; | |||||||||
| 36 | 0 | foreach my $row ( @{$aoa} ) { # NB: will also act on last_row (reference) | |||||||||
| 37 | 0 | $row->[$IDX_ED_NBPGDOC]++; # increment total pages | |||||||||
| 38 | 0 | push @res, [ @{$row} ]; | |||||||||
| 39 | } | ||||||||||
| 40 | 0 | $last_row->[$IDX_ED_IDSEQPG]++; # increment page sequence number | |||||||||
| 41 | 0 | $last_row->[$IDX_ED_SEQPGDOC]++; # increment current page number | |||||||||
| 42 | 0 | push @res, [ @{$last_row} ]; | |||||||||
| 43 | 0 | return \@res; | |||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | # if a doc has an odd number of pages, copy the last index row | ||||||||||
| 47 | # to make it even | ||||||||||
| 48 | 0 | ▶ | sub complete_seq_doc { | ||||||||
|
Subroutine
complete_seq_doc: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 49 | 0 | my ($aoa) = @_; | |||||||||
| 50 | 0 | my $nb_pages = scalar @{$aoa}; | |||||||||
| 51 | 0 | ▶ | return ( $nb_pages % 2 ) ? add_one_page($aoa) : $aoa; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | # flush a doc at the end of a csv file | ||||||||||
| 55 | 0 | ▶ | sub flush_seq_doc { | ||||||||
|
Subroutine
flush_seq_doc: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 56 | 0 | my ( $fh, $csv, $aoa ) = @_; | |||||||||
| 57 | 0 | foreach ( @{ complete_seq_doc($aoa) } ) { | |||||||||
| 58 | 0 | $csv->print( $fh, $_ ); | |||||||||
| 59 | } | ||||||||||
| 60 | 0 | return 0; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | # looping on data rows for make_docs_even_numbered | ||||||||||
| 64 | 0 | ▶ | sub loop_on_data_rows { | ||||||||
|
Subroutine
loop_on_data_rows: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 65 | 0 | my ( $csv_in, $csv_out, $fh_in, $fh_out ) = @_; | |||||||||
| 66 | 0 | my ( $seq_doc_id, @seq_doc ) = ($BAD_DOC_ID); | |||||||||
| 67 | 0 | ▶ | while ( my $row = $csv_in->getline($fh_in) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | ▶ | if ( $seq_doc_id eq "$row->[$IDX_ED_IDLDOC].$row->[$IDX_ED_SEQDOC].$row->[$IDX_ED_IDDEST]" ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | { # still in the same doc | ||||||||||
| 70 | 0 | push @seq_doc, [ @{$row} ]; | |||||||||
| 71 | } | ||||||||||
| 72 | else { # new seq_doc_id, therefore new seq_doc | ||||||||||
| 73 | 0 | flush_seq_doc( $fh_out, $csv_out, \@seq_doc ); | |||||||||
| 74 | 0 | $seq_doc_id = "$row->[$IDX_ED_IDLDOC].$row->[$IDX_ED_SEQDOC].$row->[$IDX_ED_IDDEST]"; | |||||||||
| 75 | 0 | @seq_doc = [ @{$row} ]; | |||||||||
| 76 | } | ||||||||||
| 77 | } | ||||||||||
| 78 | 0 | flush_seq_doc( $fh_out, $csv_out, \@seq_doc ); # do not forget the last doc! | |||||||||
| 79 | 0 | return 0; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | # use a temp file to replace a CSV index file "in place" | ||||||||||
| 83 | # add a page at the end of the docs with an odd number of pages | ||||||||||
| 84 | 0 | ▶ | sub make_docs_even_numbered { | ||||||||
|
Subroutine
make_docs_even_numbered: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 85 | 0 | my ($in_file) = @_; | |||||||||
| 86 | 0 | my ( $fh_out, $tmp_file ) = tmpnam(); | |||||||||
| 87 | 0 | my $csv_in = Text::CSV->new(); # auto diag | |||||||||
| 88 | 0 | my $csv_out = Text::CSV->new( { eol => "\n" } ); | |||||||||
| 89 | 0 | open my $fh_in, q{<}, $in_file; | |||||||||
| 90 | 0 | $csv_out->print( $fh_out, $csv_in->getline($fh_in) ); # reuse headers | |||||||||
| 91 | 0 | loop_on_data_rows( $csv_in, $csv_out, $fh_in, $fh_out ); | |||||||||
| 92 | 0 | close $fh_in; | |||||||||
| 93 | 0 | close $fh_out; | |||||||||
| 94 | 0 | ▶ | copy( $tmp_file, $in_file ) || croak "Cannot copy: $OS_ERROR"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | 0 | unlink $tmp_file; | |||||||||
| 96 | 0 | return 0; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 19 | 1; | |||||||||
| 100 | |||||||||||
| 101 | =head1 NAME | ||||||||||
| 102 | |||||||||||
| 103 | oEdtk::FixIdx - make every document of a CSV index span an even number of pages | ||||||||||
| 104 | |||||||||||
| 105 | =head1 SYNOPSIS | ||||||||||
| 106 | |||||||||||
| 107 | use oEdtk::FixIdx qw(make_docs_even_numbered); | ||||||||||
| 108 | |||||||||||
| 109 | make_docs_even_numbered($idx_file); # rewrites $idx_file in place | ||||||||||
| 110 | |||||||||||
| 111 | =head1 DESCRIPTION | ||||||||||
| 112 | |||||||||||
| 113 | A CSV C<.idx> index is a list of documents; a document is a run of rows whose | ||||||||||
| 114 | pages are numbered from 1 to the total for that document. When a document has | ||||||||||
| 115 | an odd number of pages, this module duplicates its last row so the document ends | ||||||||||
| 116 | on an even number of pages, which is what recto/verso printing needs. Ported | ||||||||||
| 117 | unchanged from the 0.8032 fork at S20 of the fork-merge plan. | ||||||||||
| 118 | |||||||||||
| 119 | =head1 FUNCTIONS | ||||||||||
| 120 | |||||||||||
| 121 | =head2 make_docs_even_numbered | ||||||||||
| 122 | |||||||||||
| 123 | my $rc = make_docs_even_numbered($in_file); | ||||||||||
| 124 | |||||||||||
| 125 | Read C<$in_file>, make every document even, and write the result back over | ||||||||||
| 126 | C<$in_file> through a temporary file. Returns C<0>. The first row (the header) is | ||||||||||
| 127 | copied through untouched. | ||||||||||
| 128 | |||||||||||
| 129 | =head2 loop_on_data_rows | ||||||||||
| 130 | |||||||||||
| 131 | loop_on_data_rows($csv_in, $csv_out, $fh_in, $fh_out); | ||||||||||
| 132 | |||||||||||
| 133 | Group consecutive rows by document (its C<IDLDOC.SEQDOC.IDDEST> triple) and | ||||||||||
| 134 | flush each group with L</flush_seq_doc>. Returns C<0>. | ||||||||||
| 135 | |||||||||||
| 136 | =head2 flush_seq_doc | ||||||||||
| 137 | |||||||||||
| 138 | flush_seq_doc($fh, $csv, $aoa); | ||||||||||
| 139 | |||||||||||
| 140 | Write one document (an array of arrays) to C<$fh> with C<$csv>, after making its | ||||||||||
| 141 | page count even. | ||||||||||
| 142 | |||||||||||
| 143 | =head2 complete_seq_doc | ||||||||||
| 144 | |||||||||||
| 145 | my $aoa = complete_seq_doc($aoa); | ||||||||||
| 146 | |||||||||||
| 147 | Return C<$aoa> unchanged when it already has an even number of rows, or a copy | ||||||||||
| 148 | with one extra row (L</add_one_page>) otherwise. | ||||||||||
| 149 | |||||||||||
| 150 | =head2 add_one_page | ||||||||||
| 151 | |||||||||||
| 152 | my $aoa = add_one_page($aoa); | ||||||||||
| 153 | |||||||||||
| 154 | Return a copy of C<$aoa> whose last row is duplicated: every row's total page | ||||||||||
| 155 | count is incremented, and the duplicated row's page sequence numbers are bumped | ||||||||||
| 156 | too. | ||||||||||
| 157 | |||||||||||
| 158 | =head1 AUTHOR | ||||||||||
| 159 | |||||||||||
| 160 | The oEdtk team. | ||||||||||
| 161 | |||||||||||
| 162 | =cut | ||||||||||