5 # Sorts input correction HTML. Can also filter out corrections for undesired
8 # Assumes that corrections appear one per line. Good practice would be to pipe
9 # the output of corrtohtml.pl to this function:
11 # corrtohtml.pl corrections | sortcorrhtml.pl
13 ################################################################################
17 my $progName = "sortcorrhtml";
18 my $usage = "$progName [options]\n" .
19 "\t-b bookcode exclude corrections specified for other books\n" .
20 "\t-s strip book tags\n" .
21 "\t-v verbose reporting\n";
23 my $optsProcessed = 0;
25 my $bookCodeReport = "";
26 my $stripBookInfo = 0;
27 my $includeUnspecifiedBook = 0;
30 while( $#ARGV > -1 && not $optsProcessed ) {
31 my $commandLineItem = shift @ARGV;
32 if( $commandLineItem eq "-b" ) {
33 $bookCode = shift @ARGV or die $usage;
35 elsif( $commandLineItem eq "-s" ) {
38 elsif( $commandLineItem eq "-v" ) {
41 elsif( $commandLineItem eq "--help" ) {
42 print $usage and exit;
45 unshift @ARGV, $commandLineItem;
51 $bookCodeReport = " [$bookCode]";
54 my @bookSortOrder = &getBookSortOrder( );
55 my @sectSortOrder = &getSectSortOrder( );
59 my $corrRegex = qr{^.+?(<!--[[:space:]]*([^-]+?)[[:space:]]*-->)?<a href="([^.]+)[^#]+#([[:digit:]]*)};
62 if( $bookCode ne "" ) {
63 $bookRegex = qr{^.+?(<!--[[:space:]]*($bookCode)[[:space:]]*-->)<a href="([^.]+).htm};
67 if( $bookCode ne "" ) {
68 $stripRegex = qr{<!--[[:space:]]*$bookCode[[:space:]]*-->};
71 $stripRegex = qr{<!--[[:space:]]*[^-]+?[[:space:]]*-->};
77 foreach my $line (@lines) {
78 if( $bookCode ne "" ) {
79 ($line =~ m{$bookRegex}) or next; # skip other books
82 ($line =~ m{$corrRegex}) or die( "Error ($progName)$bookCodeReport: unrecognized correction: $line\n" );
83 my( $book, $sect, $issue ) = ($2, $3, $4);
84 $book = "unknown" unless defined $book;
85 $issue = "unassigned" unless defined $issue && $issue ne "";
86 $buckets{$book} = { } unless exists $buckets{$book};
87 $buckets{$book}->{$sect} = { } unless exists $buckets{$book}->{$sect};
88 $buckets{$book}->{$sect}->{$issue} = [ ] unless exists $buckets{$book}->{$sect}->{$issue};
90 if( $stripBookInfo ) {
91 $line =~ s/$stripRegex//;
94 if( $issue ne "unassigned" && $issue > $maxIssue ) { $maxIssue = $issue; }
96 push @{$buckets{$book}->{$sect}->{$issue}}, $line;
99 foreach my $bookKey (keys %buckets) {
101 foreach my $book (@bookSortOrder) {
102 if( $bookKey eq $book ) { $found = 1; }
104 unless( $found ) { die( "Error ($progName)$bookCodeReport: unknown book code: $bookKey\n" ); }
106 foreach my $sectKey (keys %{$buckets{$bookKey}}) {
108 foreach my $sect (@sectSortOrder) {
109 if( $sectKey eq $sect ) { $found = 1; }
111 unless( $found ) { die( "Error ($progName)$bookCodeReport: unknown section: $sectKey\n" ); }
115 for( my $i = 0; $i <= $#bookSortOrder; ++$i ) {
116 for( my $j = 0; $j <= $#sectSortOrder; ++$j ) {
117 for( my $k = 0; $k <= $maxIssue; ++$k ) {
118 print @{$buckets{$bookSortOrder[$i]}->{$sectSortOrder[$j]}->{$k}} if exists $buckets{$bookSortOrder[$i]} && exists $buckets{$bookSortOrder[$i]}->{$sectSortOrder[$j]} && exists $buckets{$bookSortOrder[$i]}->{$sectSortOrder[$j]}->{$k};
120 while( $#{$buckets{$bookSortOrder[$i]}->{$sectSortOrder[$j]}->{'unassigned'}} > -1 ) {
121 print shift @{$buckets{$bookSortOrder[$i]}->{$sectSortOrder[$j]}->{'unassigned'}};
126 ################################################################################
128 sub getBookSortOrder {
172 sub getSectSortOrder {