Fix de errata reported by tonib in the forum (https://www.projectaon.org/es/foro3...
[project-aon.git] / common / scripts / gblint.pl
1 #!/usr/bin/perl -Tw
2 #
3 # Each new section id requires adding it to the list (e.g. improved
4 # disciplines).
5 #
6 ###############################################################################
7 use strict;
8
9 my $endOfDTD = 0;
10
11 my %sectionDocLookup = (
12   '_unknown' => '_unknown',
13   'toc'      => 'toc',
14   'title'    => 'title',
15   'dedicate' => 'dedicate',
16   'acknwldg' => 'acknwldg',
17   'credits'  => 'acknwldg',
18   'coming'   => 'coming',
19   'tssf'     => 'tssf',
20   'gamerulz' => 'gamerulz',
21   'discplnz' => 'discplnz',
22   'camflage' => 'discplnz',
23   'hunting'  => 'discplnz',
24   'sixthsns' => 'discplnz',
25   'tracking' => 'discplnz',
26   'healing'  => 'discplnz',
27   'wepnskll' => 'discplnz',
28   'mndshld'  => 'discplnz',
29   'mndblst'  => 'discplnz',
30   'anmlknsp' => 'discplnz',
31   'mindomtr' => 'discplnz',
32   'mksumary' => 'discplnz',
33   'anmlctrl' => 'discplnz',
34   'curing'   => 'discplnz',
35   'invsblty' => 'discplnz',
36   'psisurge' => 'discplnz',
37   'psiscrn'  => 'discplnz',
38   'dvnation' => 'discplnz',
39   'wpnmstry' => 'discplnz',
40   'anmlmstr' => 'discplnz',
41   'deliver'  => 'discplnz',
42   'assimila' => 'discplnz',
43   'hntmstry' => 'discplnz',
44   'pthmnshp' => 'discplnz',
45   'kaisurge' => 'discplnz',
46   'kaiscrn'  => 'discplnz',
47   'nexus'    => 'discplnz',
48   'gnosis'   => 'discplnz',
49   'magi'     => 'discplnz',
50   'kalchemy' => 'discplnz',
51   'powers'   => 'powers',
52   'lessmcks' => 'powers',
53   'alchemy'  => 'powers',
54   'sorcery'  => 'powers',
55   'enchant'  => 'powers',
56   'elementl' => 'powers',
57   'prophecy' => 'powers',
58   'psycmncy' => 'powers',
59   'evcation' => 'powers',
60   'highmcks' => 'powers',
61   'thamtrgy' => 'powers',
62   'telergy'  => 'powers',
63   'physirgy' => 'powers',
64   'theurgy'  => 'powers',
65   'visionry' => 'powers',
66   'necrmncy' => 'powers',
67   'staff'    => 'powers',
68   'moonston' => 'powers',
69   'equipmnt' => 'equipmnt',
70   'howcarry' => 'equipmnt',
71   'howmuch'  => 'equipmnt',
72   'howuse'   => 'equipmnt',
73   'backpack' => 'equipmnt',
74   'ammpouch' => 'equipmnt',
75   'medikit'  => 'equipmnt',
76   'canteen'  => 'equipmnt',
77   'weapons'  => 'weapons',
78   'close'    => 'weapons',
79   'missile'  => 'weapons',
80   'cmbtrulz' => 'cmbtrulz',
81   'evasion'  => 'cmbtrulz',
82   'lorecrcl' => 'lorecrcl',
83   'lcbonus'  => 'lorecrcl',
84   'levels'   => 'levels',
85   'primate'  => 'levels',
86   'tutelary' => 'levels',
87   'mentora'  => 'levels',
88   'scion'    => 'levels',
89   'archmstr' => 'levels',
90   'prncpln'  => 'levels',
91   'imprvdsc' => 'imprvdsc',
92   'guardian' => 'imprvdsc',
93   'sunkght'  => 'imprvdsc',
94   'sunlord'  => 'imprvdsc',
95   'kaiwisdm' => 'kaiwisdm',
96   'sage'     => 'sage',
97   'numbered' => 'numbered',
98   'passing'  => 'passing',
99   'part1'    => 'part1',
100   'part2'    => 'part2',
101   'map'      => 'map',
102   'action'   => 'action',
103   'crsumary' => 'crsumary',
104   'smevazn'  => 'crsumary',
105   'crtable'  => 'crtable',
106   'random'   => 'random',
107   'errata'   => 'errata',
108   'errintro' => 'errata',
109   'errerr'   => 'errata',
110   'footnotz' => 'footnotz',
111   'illstrat' => 'illstrat',
112   'primill'  => 'illstrat',
113   'secill'   => 'illstrat',
114   'license'  => 'license',
115   'lic-pre'  => 'license',
116   'lic-1'    => 'license',
117   'lic-1-0'  => 'license',
118   'lic-1-1'  => 'license',
119   'lic-1-2'  => 'license',
120   'lic-1-3'  => 'license',
121   'lic-1-4'  => 'license',
122   'lic-1-5'  => 'license',
123   'lic-1-6'  => 'license',
124   'lic-1-7'  => 'license',
125   'lic-2'    => 'license',
126   'lic-2-0'  => 'license',
127   'lic-2-1'  => 'license',
128   'lic-2-2'  => 'license',
129   'lic-2-3'  => 'license',
130   'lic-2-4'  => 'license',
131   'lic-2-5'  => 'license',
132   'lic-3'    => 'license',
133   'lic-3-0'  => 'license',
134   'lic-3-1'  => 'license',
135   'lic-4'    => 'license',
136   'lic-4-0'  => 'license',
137   'lic-5'    => 'license',
138   'lic-5-0'  => 'license',
139   'lic-6'    => 'license',
140   'lic-6-0'  => 'license',
141   'lic-6-1'  => 'license',
142 );
143
144 my $errorCount = 0;
145 my $maxErrorCount = 0;
146 my $skipLines = 0;
147 my $initials = "??";
148 my $useCorr = 0;
149 my $checkNonASCII = 1;
150 my $language = 'en';
151
152 while( $#ARGV > -1 && $ARGV[ 0 ] =~ /^-/ ) {
153   if( $ARGV[ 0 ] eq "-e" && $#ARGV > 0 ) {
154     shift @ARGV;
155     $maxErrorCount = shift @ARGV;
156   }
157   elsif( $ARGV[ 0 ] eq "-s" && $#ARGV > 0 ) {
158     shift @ARGV;
159     $skipLines = shift @ARGV;
160   }
161   elsif( $ARGV[ 0 ] eq "-i" && $#ARGV > 0 ) {
162     shift @ARGV;
163     $initials = shift @ARGV;
164   }
165   elsif( $ARGV[ 0 ] eq "--use-corr" ) {
166     shift @ARGV;
167     $useCorr = 1;
168   }
169   elsif( $ARGV[ 0 ] eq '--skip-ASCII-check' ) {
170     shift @ARGV;
171     $checkNonASCII = 0;
172   }
173   elsif( $ARGV[ 0 ] eq '--language' ) {
174     shift @ARGV;
175     $language = shift @ARGV;
176   }
177 }
178
179 my $lineNumber = 1;
180 my $currentSection = '';
181
182 while( my $line = <> ) {
183   my @section = ( $line =~ /<section[^>]+id="([^"]*)"/g );
184   if( $#section > 0 ) { die( "Multiple sections begin at line $lineNumber\n" ); }
185   elsif( $#section == 0 ) {
186     if( $section[ 0 ] =~ /^sect[[:digit:]]+$/ ) {
187       $currentSection = $section[ 0 ];
188     }
189     else {
190       $currentSection = $sectionDocLookup{$section[ 0 ]};
191     }
192     $currentSection = '_unknown' unless defined $currentSection;
193   }
194
195   if( $skipLines >= $lineNumber ) {
196     ++$lineNumber;
197     next;
198   }
199
200   ##### Unescaped Characters
201   if( $line =~ /[\200-\377]/ ) {
202     if( $line =~ /\221/ ) { &printError( "ne", $currentSection, $lineNumber, "unescaped left single quotation mark(s)", "\221", "<quote>...</quote> or <ch.apos/>" ); }
203     if( $line =~ /\222/ ) { &printError( "ne", $currentSection, $lineNumber, "unescaped right single quotation mark(s)", "\222", "<quote>...</quote> or <ch.apos/>" ); }
204     if( $line =~ /\223/ ) { &printError( "ne", $currentSection, $lineNumber, "unescaped left double quotation mark(s)", "\223", "<quote>...</quote>" ); }
205     if( $line =~ /\224/ ) { &printError( "ne", $currentSection, $lineNumber, "unescaped right double quotation mark(s)", "\224", "<quote>...</quote>" ); }
206     if( $line =~ /\226/ ) { &printError( "ne", $currentSection, $lineNumber, "unescaped endash(es)", "\226", "<ch.endash/>" ); }
207     if( $line =~ /\227/ ) { &printError( "ne", $currentSection, $lineNumber, "unescaped emdash(es)", "\227", "<ch.emdash/>" ); }
208 #    if( $checkNonASCII && $line =~ /(.{0,4}?)?([\x80-\xff])(.{0,4})?/ ) {
209 #      &printError( "ne", $currentSection, $lineNumber, "unescaped non-ASCII character(s) in \"${1}[[HERE]]${3}\"; first found only", "$2" );
210 #    }
211     if( $line =~ /(.{0,4}?)?([\x80-\x9f])(.{0,4})?/ ) {
212       &printError( "ne", $currentSection, $lineNumber, "unsafe non-ASCII character(s) in \"${1}[[HERE]]${3}\"; first found only", "$2" );
213     }
214 }
215   if( $line =~ /'/ ) { &printError( "ne", $currentSection, $lineNumber, "unescaped apostrophe(s)", "'", "<ch.apos/> or <quote>...</quote>" ); }
216   if( $line =~ /`/ ) { &printError( "ne", $currentSection, $lineNumber, "backtick(s)", "`", "<ch.apos/> or <quote>...</quote>" ); }
217
218   # tab
219
220   if( $line =~ /\t/ ) { &printError( "ne", $currentSection, $lineNumber, "TAB character found; convert to equivalent SPACEs" ); }
221
222   # ampersand
223   if( $line =~ /\&\s/ ) { &printError( "ne", $currentSection, $lineNumber, "possible malformed ampersand or escape sequence", "&", "<ch.ampersand/>" ); }
224
225   # emdash
226   if( $line =~ /\s-\s/ ) { &printError( "ne", $currentSection, $lineNumber, "probable malformed emdash", " - ", "<ch.emdash/>" ); }
227   if( $line =~ /(?<!\!)--(?!>)/ ) { &printError( "ne", $currentSection, $lineNumber, "probable malformed emdash", "--", "<ch.emdash/>" ); }
228
229   # endash
230   if( $line =~ /([0-9])-([0-9]+)(?![^<]+>)/ ) { &printError( "ne", $currentSection, $lineNumber, "possible malformed endash", "$1-$2", "$1<ch.endash/>$2" ); }
231
232   # ellipsis
233   if( $line =~ /(\.\s*\.(\s*\.)?)/ ) { &printError( "ne", $currentSection, $lineNumber, "possible malformed ellipsis", "$1", "<ch.ellips/> or <ch.lellips/>" ); }
234   if( $line =~ m{([[:space:]]<ch\.ellips/>)} || $line =~ m{(<ch\.ellips/>[[:space:]])} ) { &printError( "ne", $currentSection, $lineNumber, "<ch.ellips/> with extraneous surrounding space", "$1", "<ch.ellips/>" ); }
235   if( $line =~ m{([^>])(<ch\.lellips/>)} ) { &printError( "ne", $currentSection, $lineNumber, "possible <ch.lellips/> used in place of <ch.ellips/>", "$1$2", "$1<ch.ellips/>" ); }
236   if( $line =~ m{(><ch\.ellips/>)} ) { &printError( "ne", $currentSection, $lineNumber, "possible <ch.ellips/> used in place of <ch.lellips/>", "$1", "><ch.lellips/>" ); }
237
238   # thinspace
239   if( $line =~ m{(</?quote>)\1} ) { &printError( "ne", $currentSection, $lineNumber, "probable candidate for thinspace", "$1$1", "$1<ch.thinspace/>$1" ); }
240   if( $line =~ m{(<quote>)(<ch.apos/>)} || $line =~ m{(<ch.apos/>)(</quote>)} ) { &printError( "ne", $currentSection, $lineNumber, "probable canidate for thinspace", "$1$2", "$1<ch.thinspace/>$2" ); }
241
242   # blankline
243   if( $line =~ /(__+)/ ) { &printError( "ne", $currentSection, $lineNumber, "probable candidate for blankline", "$1", "<ch.blankline/>" ); }
244
245   # percent
246   #  It should be safe to assume that there will be a "]>" at the end of
247   #  internal DTD subset. Previous to the end of the internal DTD subset
248   #  "%" has special meaning and shouldn't be detected.
249   if( $line =~ /]>/ ) { $endOfDTD = 1; }
250   if( $endOfDTD && $line =~ /\%/ ) { &printError( "ne", $currentSection, $lineNumber, "possible candidate for percent", "\%", "<ch.percent/>" ); }
251
252   ##### OCR Errors
253
254   if( $line !~ m{^[[:space:]]*</((p)|(choice))>} && $line =~ m{(([^.?!:);>]</((p)|(choice)))>)} ) { &printError( "??", $currentSection, $lineNumber, "possible missing punctuation", "$1" ); }
255   if( $line =~ /((?<![iIeE]\.[eg])[.?!]\s+[a-z])/ ) { &printError( "??", $currentSection, $lineNumber, "possible bad initial capitalization", "$1" ); }
256   if( $line =~ /([a-zA-Z][0-9][a-zA-Z])/ ) { &printError( "??", $currentSection, $lineNumber, "probable replacement of number for letter", "$1" ); }
257   if( $line =~ />[^<]*-[[:space:]]/ ) { &printError( "??", $currentSection, $lineNumber, "possible retained end-of-line hyphen(s)" ); }
258
259   ##### Obsolete Markup
260
261   if( $line =~ /\&lsquot;/ ) { &printError( "ne", $currentSection, $lineNumber, "probable obsolete markup", "\&lsquot;", "<quote>" ); }
262   if( $line =~ /\&rsquot;/ ) { &printError( "ne", $currentSection, $lineNumber, "probable obsolete markup", "\&rsquot;", "</quote>" ); }
263   if( $line =~ /\&ldquot;/ ) { &printError( "ne", $currentSection, $lineNumber, "probable obsolete markup", "\&ldquot;", "<quote>" ); }
264   if( $line =~ /\&rdquot;/ ) { &printError( "ne", $currentSection, $lineNumber, "probable obsolete markup", "\&rdquot;", "</quote>" ); }
265   if( $line =~ /\&quot;/ ) { &printError( "ne", $currentSection, $lineNumber, "possible obsolete markup", "\&quot;", "<quote> or </quote>" ); }
266   if( $line =~ /(\&link.[^;]+;)/ && ($1 ne '&link.staff.contact;') && ($1 ne '&link.project.website;')) { &printError( "ne", $currentSection, $lineNumber, "possibe obsolete markup", "$1", "use <bookref.../> instead" ); }
267   if( $line =~ /\&([^[:space:]]+);/ ) {
268     unless( $1 =~ /^(?:link|inclusion)/ ) {
269       &printError( "ne", $currentSection, $lineNumber, "possible obsolete markup", "\&$1\;", "<ch.$1/>" );
270     }
271   }
272   if( $line =~ /(<a([^>]*) class="footnote"(.*?)>)/ )  { &printError( "ne", $currentSection, $lineNumber, "obsolete markup", "$1", "<footref$2$3>" ); }
273
274   ##### Character Attributes
275   # The following test uses negative lookback (?<! ... )
276   if( $line =~ /[^>]((?<!CLOSE\s)COMBAT\s+SKILL)/ || $line =~ /((?<!CLOSE\s)COMBAT\s+SKILL)[^<]/ ) {
277     &printError( "ne", $currentSection, $lineNumber, "possible missing markup", "$1", "<typ class=\"attribute\">$1</typ>" );
278   }
279   if( $line =~ /[^>](CLOSE\sCOMBAT\sSKILL)/ || $line =~ /(CLOSE\s+COMBAT\s+SKILL)[^<]/ ) {
280     &printError( "ne", $currentSection, $lineNumber, "possible missing markup", "$1", "<typ class=\"attribute\">$1</typ>" );
281   }
282   if( $line =~ /[^>](ENDURANCE)/ || $line =~ /(ENDURANCE)[^<]/ ) {
283     &printError( "ne", $currentSection, $lineNumber, "possible missing markup", "ENDURANCE", "<typ class=\"attribute\">ENDURANCE</typ>" );
284   }
285   if( $line =~ /[^>](WILLPOWER)/ || $line =~ /(WILLPOWER)[^<]/ ) {
286     &printError( "ne", $currentSection, $lineNumber, "possible missing markup", "WILLPOWER", "<typ class=\"attribute\">WILLPOWER</typ>" );
287   }
288
289   ##### Links
290   if( $line =~ /[^>](random[[:space:]]+number[[:space:]]+table)/i ) {
291     &printError( "ne", $currentSection, $lineNumber, "possible missing markup", "$1", "<a idref=\"random\">$1</a>" );
292   }
293   if( $line =~ /[^>](action[[:space:]]+charts?)/i ) {
294     &printError( "ne", $currentSection, $lineNumber, "possible missing markup", "$1", "<a idref=\"action\">$1</a>" );
295   }
296
297   ##### Others
298   if( $line =~ m{<!--(?!/?ERRTAG)} ) { &printError( "ne", $currentSection, $lineNumber, "XML comment found (check for editor comments)" ); }
299   if( $line =~ /([[:upper:]]{5,})/ &&
300       $` !~ /<signpost>$/ &&
301       (($language eq 'en' && 
302         $1 ne "ENDURANCE" &&
303         $1 ne "COMBAT" &&
304         $1 ne "WILLPOWER" &&
305         $1 ne "HAVOC" &&
306         $1 ne "CLOSE") ||
307        ($language eq 'es' &&
308         $1 ne 'DESTREZA' &&
309         $1 ne 'RESISTENCIA')) &&
310         $1 ne "DOCTYPE" &&
311         $1 ne "ENTITY" &&
312         $1 ne "XVIII" &&
313         $1 ne "ERRTAG" )
314       { &printError( "ne", $currentSection, $lineNumber, "possible <signpost> needed", "$1", "<signpost>$1</signpost>" ); }
315
316   #####
317   ++$lineNumber;
318 }
319
320 unless( $endOfDTD || $skipLines > 0 ) { print "End of document reached without finding end of internal DTD subset \"]>\".\n"; }
321
322 ################################################################################
323
324 sub printError {
325   my ($type, $section, $line, $message, $original, $corrected) = @_;
326   my $report = "";
327
328   if( $useCorr ) {
329     $report = "($type) $section: ";
330     if( defined $original ) { $report .= "$original "; }
331     if( defined $corrected ) { $report .= "-> $corrected "; }
332     $report .= "[$initials: $message <line $line>]\n";
333   }
334   else {
335     $report = "line $line ($section): $message";
336     if( defined $original ) { $report .= " \"$original\""; }
337     if( defined $corrected ) { $report .= " ($corrected)"; }
338     $report .= "\n";
339   }
340
341   print $report;
342
343   ++$errorCount;
344   if( $maxErrorCount > 0 && $errorCount > $maxErrorCount ) { die "Maximum number of errors ($maxErrorCount) exceeded. Quitting.\n"; }
345 }