| 1 | package oEdtk::FixIdx; | ||
| 2 | use base qw( Exporter ); | ||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use utf8; | ||
| 6 | use feature 'say'; | ||
| 7 | use autodie; | ||
| 8 | use Carp; | ||
| 9 | use English '-no_match_vars'; | ||
| 10 | use File::Copy qw( copy ); | ||
| 11 | use File::Temp; | ||
| 12 | use Text::CSV qw( csv ); | ||
| 13 | use Data::Dumper; | ||
| 14 | use Readonly; | ||
| 15 | |||
| 16 | our $VERSION = 2.1129; | ||
| 17 | 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 | Readonly my $IDX_LAST => -1; # magic index of last element of a list | ||
| 23 | Readonly my $BAD_DOC_ID => -1; # cannot be a valid doc id | ||
| 24 | Readonly my $IDX_ED_IDLDOC => 1; # id doc of the "lot" | ||
| 25 | Readonly my $IDX_ED_IDSEQPG => 2; # page sequence in the "lot" | ||
| 26 | Readonly my $IDX_ED_IDDEST => 6; # page sequence in the "lot" | ||
| 27 | Readonly my $IDX_ED_SEQDOC => 3; # id of the document in the sequence | ||
| 28 | Readonly my $IDX_ED_SEQPGDOC => 39; # current number of the page in the doc | ||
| 29 | 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 | sub add_one_page { | ||
| 33 | my ($aoa) = @_; | ||
| 34 | my @res; | ||
| 35 | my $last_row = $aoa->[$IDX_LAST]; | ||
| 36 | foreach my $row ( @{$aoa} ) { # NB: will also act on last_row (reference) | ||
| 37 | $row->[$IDX_ED_NBPGDOC]++; # increment total pages | ||
| 38 | push @res, [ @{$row} ]; | ||
| 39 | } | ||
| 40 | $last_row->[$IDX_ED_IDSEQPG]++; # increment page sequence number | ||
| 41 | $last_row->[$IDX_ED_SEQPGDOC]++; # increment current page number | ||
| 42 | push @res, [ @{$last_row} ]; | ||
| 43 | 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 | sub complete_seq_doc { | ||
| 49 | my ($aoa) = @_; | ||
| 50 | my $nb_pages = scalar @{$aoa}; | ||
| 51 | return ( $nb_pages % 2 ) ? add_one_page($aoa) : $aoa; | ||
| 52 | } | ||
| 53 | |||
| 54 | # flush a doc at the end of a csv file | ||
| 55 | sub flush_seq_doc { | ||
| 56 | my ( $fh, $csv, $aoa ) = @_; | ||
| 57 | foreach ( @{ complete_seq_doc($aoa) } ) { | ||
| 58 | $csv->print( $fh, $_ ); | ||
| 59 | } | ||
| 60 | return 0; | ||
| 61 | } | ||
| 62 | |||
| 63 | # looping on data rows for make_docs_even_numbered | ||
| 64 | sub loop_on_data_rows { | ||
| 65 | my ( $csv_in, $csv_out, $fh_in, $fh_out ) = @_; | ||
| 66 | my ( $seq_doc_id, @seq_doc ) = ($BAD_DOC_ID); | ||
| 67 | while ( my $row = $csv_in->getline($fh_in) ) { | ||
| 68 | if ( $seq_doc_id eq "$row->[$IDX_ED_IDLDOC].$row->[$IDX_ED_SEQDOC].$row->[$IDX_ED_IDDEST]" ) | ||
| 69 | { # still in the same doc | ||
| 70 | push @seq_doc, [ @{$row} ]; | ||
| 71 | } | ||
| 72 | else { # new seq_doc_id, therefore new seq_doc | ||
| 73 | flush_seq_doc( $fh_out, $csv_out, \@seq_doc ); | ||
| 74 | $seq_doc_id = "$row->[$IDX_ED_IDLDOC].$row->[$IDX_ED_SEQDOC].$row->[$IDX_ED_IDDEST]"; | ||
| 75 | @seq_doc = [ @{$row} ]; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | flush_seq_doc( $fh_out, $csv_out, \@seq_doc ); # do not forget the last doc! | ||
| 79 | 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 | sub make_docs_even_numbered { | ||
| 85 | my ($in_file) = @_; | ||
| 86 | my ( $fh_out, $tmp_file ) = tmpnam(); | ||
| 87 | my $csv_in = Text::CSV->new(); # auto diag | ||
| 88 | my $csv_out = Text::CSV->new( { eol => "\n" } ); | ||
| 89 | open my $fh_in, q{<}, $in_file; | ||
| 90 | $csv_out->print( $fh_out, $csv_in->getline($fh_in) ); # reuse headers | ||
| 91 | loop_on_data_rows( $csv_in, $csv_out, $fh_in, $fh_out ); | ||
| 92 | close $fh_in; | ||
| 93 | close $fh_out; | ||
| 94 | copy( $tmp_file, $in_file ) || croak "Cannot copy: $OS_ERROR"; | ||
| 95 | unlink $tmp_file; | ||
| 96 | return 0; | ||
| 97 | } | ||
| 98 | |||
| 99 | 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 |