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
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.
14 $xmlfile = shift @ARGV;
15 $xmlfile || die("usage: $0 <xml file>\n");
16 $invalid = shift @ARGV;
17 $invalid && die("usage: $0 <xml file>\n");
19 open(ERRATA, $xmlfile) || die("Cannot read input file $xmlfile\n");
20 open(SECTIONS, $xmlfile) || die("Cannot read input file $xmlfile\n");
22 while(<ERRATA>) { # find errata list
23 last if m/<section.*id="errerr".*>/;
25 die("No errata section!? Aborting.\n") if eof(ERRATA);
26 while(<ERRATA>) { # find start of actual list
29 die("No data in the errata section!? Aborting.\n") if eof(ERRATA);
31 print("No matches for these errata entries were found:\n");
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;
42 # Now find section and append all contents into one string
43 while(<SECTIONS>) { # locate section
44 last if m/<section.*id=\"$sect\">/;
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
50 die("Could not find any data in $sect!? Aborting.\n") if eof(SECTIONS);
52 while(<SECTIONS>) { # grab all section contents
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
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;
63 die("Could not find the end of $sect!? Aborting.\n") if eof(SECTIONS);
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");
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");
88 print("Checking finished!\n");