258ce2d2d17007d47297c4ac2d0faabef7119545
[project-aon.git] / scripts / checkerrata.pl
1 #!/usr/bin/perl -w
2 # This script parses the errata list of a book in XML form and tries to
3 # locate the added or replaced strings in the sections where they are now
4 # supposed to exist. Things that are not found by the simple errata list
5 # parsing code are just skipped. Nesting frontmatter sections are also
6 # skipped.
7
8 # Old History:
9 #  2005-08-23 Tried to improve handling of <link-text> tags
10 #  2005-03-12 Treat &thinspace; correctly.
11 #  2003-03-16 Fixed reporting of false positived due to <link-text> tags
12 #  2003-03-15 First version.
13
14 $xmlfile = shift @ARGV;
15 $xmlfile || die("usage: $0 <xml file>\n");
16 $invalid = shift @ARGV;
17 $invalid && die("usage: $0 <xml file>\n");
18
19 open(ERRATA, $xmlfile) || die("Cannot read input file $xmlfile\n");
20 open(SECTIONS, $xmlfile) || die("Cannot read input file $xmlfile\n");
21
22 while(<ERRATA>) { # find errata list
23   last if m/<section.*id="errerr".*>/;
24 }
25 die("No errata section!? Aborting.\n") if eof(ERRATA);
26 while(<ERRATA>) { # find start of actual list
27   last if m/<data>/;
28 }
29 die("No data in the errata section!? Aborting.\n") if eof(ERRATA);
30
31 print("No matches for these errata entries were found:\n");
32
33 # Now pick each paragraph (= errata) line
34 ERRATALOOP: while(<ERRATA>) {
35   next unless m/^\s*<p>/; # no paragraph = not an errata line
36   last if m|</data>|; # end of the errata list
37   ($sect) = m|\(<a (?:id="\w+?" )?idref="(\w+?)">.*</a>\)|;
38   @reps = m|<quote>.*?</quote> with <quote>(.*?)</quote>|g;
39   @adds = m|[Aa]dded <quote>(.*?(?=</quote>))</quote>|g;
40   next unless @reps || @adds;
41
42   # Now find section and append all contents into one string
43   while(<SECTIONS>) { # locate section
44     last if m/<section.*id=\"$sect\">/;
45   }
46   die("Could not find section $sect!? (This might be because the errata\nentry for it is not placed in the correct section order.) Aborting.\n") if eof(SECTIONS);
47   while(<SECTIONS>) { # locate data
48     last if m/<data>/;
49   }
50   die("Could not find any data in $sect!? Aborting.\n") if eof(SECTIONS);
51   $text = "";
52   while(<SECTIONS>) { # grab all section contents
53     last if m|</data>|;
54     if (m/<section/) { # oh no, nested sections - we cannot handle this well
55       seek(SECTIONS, -length(), 1); # give the next section a chance
56       next ERRATALOOP; # give up on this section
57     }
58     # Before adding the text to the section blob, modify <link-text> tags
59     # since they are commented out in the errata entries
60     s|<(/?)link-text>|<!--${1}link-text-->|g;
61     $text .= $_;
62   }
63   die("Could not find the end of $sect!? Aborting.\n") if eof(SECTIONS);
64
65   # The replacement may contain &[lr][sd]quot; which in the sections are
66   # <quote> thingies. Translate these. Also ignore &thinspace; first and
67   # last in the replacements.
68   # Refactor the duplicated code below some day!
69   foreach $rep (@reps) {
70     $rep =~ s/\&l[sd]quot;/<quote>/g;
71     $rep =~ s/\&r[sd]quot;/<\/quote>/g;
72     $rep =~ s/^\&thinspace;//g;
73     $rep =~ s/\&thinspace;$//g;
74     if ($text !~ m/\Q$rep\E/) {
75       print("Replacement \"$rep\" in $sect\n");
76     }
77   }
78   foreach $add (@adds) {
79     $add =~ s/\&l[sd]quot;/<quote>/g;
80     $add =~ s/\&r[sd]quot;/<\/quote>/g;
81     $add =~ s/^\&thinspace;//g;
82     $add =~ s/\&thinspace;$//g;
83     if ($text !~ m/\Q$add\E/) {
84       print("Addition    \"$add\" in $sect\n");
85     }
86   }
87 }
88 print("Checking finished!\n");