b4b6b21ed985404beceb9dfa23ab0d3c99448a74
[project-aon.git] / scripts / mergecorrhtml.pl
1 #!/usr/bin/perl -w
2 #
3 # mergecorrhtml.pl
4 #
5 # mergecorrhtml [options] -i inputHTML [inputCorrections]
6 #            -b bookcode
7 #            -u include with unspecified bookcode
8 #            -v verbose reporting
9 #
10 # Merges _sorted_ HTML correction lists: one in a HTML file, one bare list. It
11 # will dump any remaining corrections in the lists after completion. The chief
12 # reasons that this should happen is if the lists aren't sorted. The correction
13 # list in the input HTML should be surrounded by the following markers on lines
14 # by themselves:
15 #
16 #  <!--mergecorrhtml:BEGIN-->
17 #  [list goes here]
18 #  <!--mergecorrhtml:END-->
19 #
20 # Typical usage would be in concert with corrtohtml and sortcorrhtml:
21 #
22 #  corrtohtml <correctionFile> | sorcorrhtml | mergecorrhtml -b <book> <html>
23 #
24 # Output will appear on standard out which would usually be redirected to file.
25 #
26 ################################################################################
27
28 use strict;
29
30 my $programName = 'mergecorrhtml';
31 my $usage = "$programName [options] inputHTML\n" .
32             "\t-b bookcode\n" .
33             "\t-u include unspecified book\n" .
34             "\t-v verbose reporting\n";
35
36 my $htmlRegex;
37 my $corrRegex;
38 my $issueRegex;
39 my $markerRegex;
40
41 ################################################################################
42 # Process command line
43
44 my $optsProcessed = 0;
45 my $inFile;
46 my $bookCode = "";
47 my $bookCodeReport = "";
48 my $includeUnspecifiedBook = 0;
49 my $verbose = 0;
50
51 while( $#ARGV > -1 && not $optsProcessed ) {
52   my $commandLineItem = shift @ARGV;
53   if( $commandLineItem eq "-b" ) {
54     $bookCode = shift @ARGV or die $usage;
55   }
56   elsif( $commandLineItem eq "-u" ) {
57     $includeUnspecifiedBook = 1;
58   }
59   elsif( $commandLineItem eq "-v" ) {
60     $verbose = 1;
61   }
62   elsif( $commandLineItem eq "--help" ) {
63     print $usage and exit;
64   }
65   else {
66     unshift @ARGV, $commandLineItem;
67     $optsProcessed = 1;
68   }
69 }
70
71 if( $verbose ) {
72     $bookCodeReport = " [$bookCode]";
73 }
74
75 $inFile = shift @ARGV or die $usage;
76
77 $issueRegex = qr{[^#]+?(?:#([[:digit:]]+))};
78
79 $htmlRegex = qr{^(<div.*?>)()<a(.*?)href="}; # unused capture to match other regex below
80 if( $bookCode eq "" ) {
81   $corrRegex = $htmlRegex;
82 }
83 elsif( $includeUnspecifiedBook ) {
84   $corrRegex = qr{^(<div.*?>)(<!--[[:space:]]*${bookCode}[[:space:]]*-->)?<a(.*?)href="};
85 }
86 else {
87   $corrRegex = qr{^(<div.*?>)(<!--[[:space:]]*${bookCode}[[:space:]]*-->)<a(.*?)href="};
88 }
89 $markerRegex = qr{^<div[[:space:]]+?class="section".*$};
90
91 ################################################################################
92 # Read in HTML into which we're merging and correction HTML
93
94 open( INFILE, "<$inFile" ) or die( "Error ($programName)$bookCodeReport: unable to open \"$inFile\" for read: $!\n" );
95 my @lines = <INFILE>;
96 close INFILE;
97
98 #### Consume preamble
99
100 while( $#lines > -1 && $lines[ 0 ] !~ m{^[[:space:]]*<!--mergecorrhtml:BEGIN-->[[:space:]]*$} ) {
101   print shift @lines;
102 }
103 print shift @lines if( $#lines > -1 );
104
105 my @inHTML;
106
107 #### Get good stuff
108
109 while( $#lines > -1 && $lines[ 0 ] !~ m{^[[:space:]]*<!--mergecorrhtml:END-->[[:space:]]*$} ) {
110   if( $lines[ 0 ] =~ m/$htmlRegex/ ) {
111     push( @inHTML, shift @lines );
112   }
113   elsif( $lines[ 0 ] =~ m/$markerRegex/ ) {
114     shift @lines;
115   }
116   elsif( $lines[ 0 ] =~ m/^[[:space:]]*$/ ) {
117     shift @lines;
118   }
119   else {
120     die( "Error ($programName)$bookCodeReport: unrecognized input HTML: " . $lines[ 0 ] . "\n" );
121   }
122 }
123
124 my @inCorr;
125 while( my $corr = <> ) {
126   push( @inCorr, $corr ) if( $corr =~ m{$corrRegex} );
127 }
128
129 ################################################################################
130 # Merge!
131
132 my @sectSortOrder = &getSectSortOrder( );
133
134 foreach my $section (@sectSortOrder) {
135   my $issue;
136   print "<div class=\"section\"><a name=\"$section\">$section</a></div>\n";
137   while( $#inHTML > -1 && $inHTML[ 0 ] =~ m/$htmlRegex$section\.htm${issueRegex}/ ) {
138     $issue = $4;
139     while( $#inCorr > -1 && $inCorr[ 0 ] =~ m/$corrRegex$section\.htm${issueRegex}/ && $issue eq $4 ) {
140       my $corr = shift @inCorr;
141       my $comm = "";
142       if( $corr !~ m{^.+?:[[:space:]]*<div[^>]+?class="[^"]*cm} ) { warn( "Warning ($programName)$bookCodeReport: discarding data in issue comment: $corr" ); }
143       while( $corr =~ s{^.*?(<div[^>]+?class="[^"]*cm[^>]+>.*?</div>)}{} ) {
144         $comm .= $1;
145       }
146       $inHTML[ 0 ] =~ s{</div>$}{$comm</div>}
147     }
148     print shift @inHTML;
149   }
150   while( $#inCorr > -1 && $inCorr[ 0 ] =~ m/$corrRegex$section\.htm/ ) {
151     my $corr = shift @inCorr;
152     $corr =~ s{$corrRegex}{$1<a$3href="};
153     ++$issue;
154     $corr =~ s{#:}{#$issue:};
155     print $corr;
156   }
157 }
158
159 ################################################################################
160 # Print the remainder of the input HTML and corrections
161
162 if( $#inHTML > -1 ) {
163   warn( "Warning ($programName)$bookCodeReport: input HTML probably out of order\n\tor unrecognized section--error near:\n\t" . $inHTML[ 0 ] . "\n" );
164   print @inHTML;
165 }
166 if( $#inCorr > -1 ) {
167   warn( "Warning ($programName)$bookCodeReport: input corrections probably out of order\n\tor unrecognized section--error near:\n\t" . $inCorr[ 0 ] . "\n" );
168   print @inCorr;
169 }
170
171 print @lines;
172
173
174 ################################################################################
175 ################################################################################
176 # Subroutines
177
178 sub getSectSortOrder {
179   return qw{
180     _unknown
181     toc
182     title
183     dedicate
184     acknwldg
185     coming
186     tssf
187     gamerulz
188     discplnz
189     powers
190     equipmnt
191     cmbtrulz
192     lorecrcl
193     levels
194     imprvdsc
195     kaiwisdm
196     sage
197     numbered
198     part1
199     sect1
200     sect2
201     sect3
202     sect4
203     sect5
204     sect6
205     sect7
206     sect8
207     sect9
208     sect10
209     sect11
210     sect12
211     sect13
212     sect14
213     sect15
214     sect16
215     sect17
216     sect18
217     sect19
218     sect20
219     sect21
220     sect22
221     sect23
222     sect24
223     sect25
224     sect26
225     sect27
226     sect28
227     sect29
228     sect30
229     sect31
230     sect32
231     sect33
232     sect34
233     sect35
234     sect36
235     sect37
236     sect38
237     sect39
238     sect40
239     sect41
240     sect42
241     sect43
242     sect44
243     sect45
244     sect46
245     sect47
246     sect48
247     sect49
248     sect50
249     sect51
250     sect52
251     sect53
252     sect54
253     sect55
254     sect56
255     sect57
256     sect58
257     sect59
258     sect60
259     sect61
260     sect62
261     sect63
262     sect64
263     sect65
264     sect66
265     sect67
266     sect68
267     sect69
268     sect70
269     sect71
270     sect72
271     sect73
272     sect74
273     sect75
274     sect76
275     sect77
276     sect78
277     sect79
278     sect80
279     sect81
280     sect82
281     sect83
282     sect84
283     sect85
284     sect86
285     sect87
286     sect88
287     sect89
288     sect90
289     sect91
290     sect92
291     sect93
292     sect94
293     sect95
294     sect96
295     sect97
296     sect98
297     sect99
298     sect100
299     sect101
300     sect102
301     sect103
302     sect104
303     sect105
304     sect106
305     sect107
306     sect108
307     sect109
308     sect110
309     sect111
310     sect112
311     sect113
312     sect114
313     sect115
314     sect116
315     sect117
316     sect118
317     sect119
318     sect120
319     sect121
320     sect122
321     sect123
322     sect124
323     sect125
324     sect126
325     sect127
326     sect128
327     sect129
328     sect130
329     sect131
330     sect132
331     sect133
332     sect134
333     sect135
334     sect136
335     sect137
336     sect138
337     sect139
338     sect140
339     sect141
340     sect142
341     sect143
342     sect144
343     sect145
344     sect146
345     sect147
346     sect148
347     sect149
348     sect150
349     sect151
350     sect152
351     sect153
352     sect154
353     sect155
354     sect156
355     sect157
356     sect158
357     sect159
358     sect160
359     sect161
360     sect162
361     sect163
362     sect164
363     sect165
364     sect166
365     sect167
366     sect168
367     sect169
368     sect170
369     sect171
370     sect172
371     sect173
372     sect174
373     sect175
374     sect176
375     sect177
376     sect178
377     sect179
378     sect180
379     sect181
380     sect182
381     sect183
382     sect184
383     sect185
384     sect186
385     sect187
386     sect188
387     sect189
388     sect190
389     sect191
390     sect192
391     sect193
392     sect194
393     sect195
394     sect196
395     sect197
396     sect198
397     sect199
398     part2
399     sect200
400     sect201
401     sect202
402     sect203
403     sect204
404     sect205
405     sect206
406     sect207
407     sect208
408     sect209
409     sect210
410     sect211
411     sect212
412     sect213
413     sect214
414     sect215
415     sect216
416     sect217
417     sect218
418     sect219
419     sect220
420     sect221
421     sect222
422     sect223
423     sect224
424     sect225
425     sect226
426     sect227
427     sect228
428     sect229
429     sect230
430     sect231
431     sect232
432     sect233
433     sect234
434     sect235
435     sect236
436     sect237
437     sect238
438     sect239
439     sect240
440     sect241
441     sect242
442     sect243
443     sect244
444     sect245
445     sect246
446     sect247
447     sect248
448     sect249
449     sect250
450     sect251
451     sect252
452     sect253
453     sect254
454     sect255
455     sect256
456     sect257
457     sect258
458     sect259
459     sect260
460     sect261
461     sect262
462     sect263
463     sect264
464     sect265
465     sect266
466     sect267
467     sect268
468     sect269
469     sect270
470     sect271
471     sect272
472     sect273
473     sect274
474     sect275
475     sect276
476     sect277
477     sect278
478     sect279
479     sect280
480     sect281
481     sect282
482     sect283
483     sect284
484     sect285
485     sect286
486     sect287
487     sect288
488     sect289
489     sect290
490     sect291
491     sect292
492     sect293
493     sect294
494     sect295
495     sect296
496     sect297
497     sect298
498     sect299
499     sect300
500     sect301
501     sect302
502     sect303
503     sect304
504     sect305
505     sect306
506     sect307
507     sect308
508     sect309
509     sect310
510     sect311
511     sect312
512     sect313
513     sect314
514     sect315
515     sect316
516     sect317
517     sect318
518     sect319
519     sect320
520     sect321
521     sect322
522     sect323
523     sect324
524     sect325
525     sect326
526     sect327
527     sect328
528     sect329
529     sect330
530     sect331
531     sect332
532     sect333
533     sect334
534     sect335
535     sect336
536     sect337
537     sect338
538     sect339
539     sect340
540     sect341
541     sect342
542     sect343
543     sect344
544     sect345
545     sect346
546     sect347
547     sect348
548     sect349
549     sect350
550     sect351
551     sect352
552     sect353
553     sect354
554     sect355
555     sect356
556     sect357
557     sect358
558     sect359
559     sect360
560     sect361
561     sect362
562     sect363
563     sect364
564     sect365
565     sect366
566     sect367
567     sect368
568     sect369
569     sect370
570     sect371
571     sect372
572     sect373
573     sect374
574     sect375
575     sect376
576     sect377
577     sect378
578     sect379
579     sect380
580     sect381
581     sect382
582     sect383
583     sect384
584     sect385
585     sect386
586     sect387
587     sect388
588     sect389
589     sect390
590     sect391
591     sect392
592     sect393
593     sect394
594     sect395
595     sect396
596     sect397
597     sect398
598     sect399
599     sect400
600     ill1
601     ill2
602     ill3
603     ill4
604     ill5
605     ill6
606     ill7
607     ill8
608     ill9
609     ill10
610     ill11
611     ill12
612     ill13
613     ill14
614     ill15
615     ill16
616     ill17
617     ill18
618     ill19
619     ill20
620     passing
621     map
622     action
623     crsumary
624     crtable
625     random
626     errata
627     footnotz
628     illstrat
629     license
630   };
631 }