converted Anskavern to Anskaven and minor footnote issues
[project-aon.git] / scripts / xmlize.pl
1 #!/usr/bin/perl
2 #
3 # xmlize.pl
4 #
5 # $Id$
6 #
7 # $Log$
8 # Revision 1.1  2005/04/26 04:48:03  jonathan.blake
9 # Initial revision
10 #
11 # Revision 1.2  2002/10/20 05:46:31  jblake
12 # Fixed a couple of bugs in the handling of carriage returns and
13 # added support for Freeway Warrior's CLOSE COMBAT SKILL.
14 #
15 # Revision 1.1  2002/10/20 03:18:35  jblake
16 # Initial revision
17 #
18 #
19 # 21 Jun 2002 - Fixed bug in tagging of character-attributes
20 # 06 May 2002 - Incorporated funcionality of xmlize-all
21 # 20 Oct 2001 - Added more spaces to xmlized lines to make 'em purty
22 #               in the final product
23 # 19 May 2001 - Updated to conform to new gamebook DTD
24 # 17 Apr 2001 - Repurposed as XMLizer
25 # 24 Feb 2001 - Commented out some of the filtering in favor of
26 #               placing it in a separate script
27 # 22 Feb 2000 - Added filtering for &
28 #               Padding ENDURANCE in combat <p> with spaces
29 # 21 Feb 2000 - Added filtering for \t
30 # 05 Feb 2000 - Added Action Chart linking
31 #               Added [] centering
32 #               Fixed the "A Giak" caps problem
33 #
34 ######################################################################
35
36 #use strict;
37
38 #### Subroutines
39
40 sub xmlize {
41     my( $inline, $infile ) = @_;
42
43     $inline =~ s/(\.\.\.|\.\s\.\s\.)/\&ellips\;/g;
44     $inline =~ tr/\t/ /;
45     $inline =~ s/\s{2,}/ /g;
46     $inline =~ s/\s+$//;
47     $inline =~ s/\&\s/\&amp\; /g;
48     $inline =~ tr/\"\`\222\221/\'/;
49     $inline =~ s/(Random\sNumber\sTable)/<a idref=\"random\">$1<\/a>/gi;
50     $inline =~ s/(Action\sCharts?)/<a idref=\"action\">$1<\/a>/gi;
51     # \222 and \221 are some form of funky right and
52     # left quotes not present in ascii (of course) 
53     $inline =~ tr/\227/-/;
54     # \227 is an em or en dash
55
56     $inline =~ s/^\s*(.*)\s*$/$1/;
57
58     if( $inline =~ /^\*/ ) {
59         $inline =~ s/^\*\s*/       <ul>\n        <li>/;
60         $inline =~ s/\s*\*\s*/<\/li>\n        <li>/g;
61         $inline .= "</li>\n       </ul>";
62     }
63     elsif( $inline =~ /^\d+\)\s/ ) {
64         $inline =~ s/^\d+\)\s+/       <ol>\n        <li>/;
65         $inline =~ s/\s*\d+\)\s+/<\/li>\n        <li>/g;
66         $inline .= "</li>\n       </ol>";
67     }
68     elsif( $inline =~ /^\<\!\-\-\spre\s\-\-\>/ ) {
69         $inline =~ s/^\<\!\-\-\spre\s\-\-\>//;
70         warn( "Warning: preformatted text in \"$infile\"\n" );
71     }
72     elsif( $inline =~ /^.+:\s+CLOSE\sCOMBAT\sSKILL/ ) {
73         $inline =~ s/^(.+):\s+CLOSE\sCOMBAT\sSKILL\s+([0-9]+)\s+ENDURANCE\s+([0-9]+)/       <combat><enemy>$1<\/enemy><enemy-attribute class=\"closecombatskill\">$2<\/enemy-attribute><enemy-attribute class=\"endurance\">$3<\/enemy-attribute><\/combat>/g;
74     }
75     elsif( $inline =~ /^.+:\s+COMBAT\sSKILL/ ) {
76         $inline =~ s/^(.+):\s+COMBAT\sSKILL\s+([0-9]+)\s+ENDURANCE\s+([0-9]+)/       <combat><enemy>$1<\/enemy><enemy-attribute class=\"combatskill\">$2<\/enemy-attribute><enemy-attribute class=\"endurance\">$3<\/enemy-attribute><\/combat>/;
77     }
78     elsif( $inline =~ /^(.*)\b(return|turn|go)([a-zA-Z\s]+?to )(\d{1,3})/i ) {
79         $inline =~ s/^(.*)\b(return|turn|go)([a-zA-Z\s]+?to )(\d{1,3})(.*)/       <choice idref=\"sect$4\">$1<link-text>$2$3$4<\/link-text>$5<\/choice>/i;
80         $inline =~ s/\s+<\/choice>/<\/choice>/;
81     }
82     elsif( $inline =~ /^\[/ ) {
83         $inline =~ s/\[(.*)\]/$1/;
84         $inline = "       <signpost>$inline</signpost>";
85         $inline =~ s/\s+<\/signpost>/<\/signpost>/;
86     }
87     elsif( $inline eq "" ) {
88     }
89     elsif( $inline =~ /^<!--(.*)-->/ ) {
90         warn( "Warning: unknown comment \"$1\" in \"$infile\"\n" );
91     }
92     else {
93         $inline = "       <p>$inline</p>";
94         $inline =~ s/\s+<\/p>/<\/p>/;
95     }
96
97 # Interferes with selecting a combat paragraph if done earlier
98     $inline =~ s/(COMBAT\sSKILL|CLOSE\sCOMBAT\sSKILL|ENDURANCE|WILLPOWER|\bCS\b|\bEP\b)([^<])/<typ class="attribute">$1<\/typ>$2/g;
99
100     return $inline;
101 }
102
103 #### Main Routine
104
105 my $numberOfSections = shift @ARGV;
106
107 print << "(End of XML Header)";
108 <?xml version="1.0" encoding="ISO-8859-1"?>
109 <!DOCTYPE gamebook SYSTEM "gamebook.dtd" [
110  <!ENTITY % xhtml.characters SYSTEM "htmlchar.mod">
111  <!ENTITY % latex.characters SYSTEM "ltexchar.mod">
112  %xhtml.characters;
113
114  <!ENTITY % general.links SYSTEM "genlink.mod">
115  %general.links;
116  <!ENTITY % xhtml.links   SYSTEM "htmllink.mod">
117  %xhtml.links;
118
119  <!ENTITY % general.inclusions SYSTEM "geninc.mod">
120  %general.inclusions;
121 ]>
122
123 <gamebook xml:lang="en-UK" version="0.10">
124
125 <!--
126
127 \$Id\$
128
129 \$Log\$
130
131 -->
132  <meta>
133   <title>[Insert Title]</title>
134  </meta>
135
136  <section id="toc">
137   <meta />
138   <data />
139  </section>
140
141  <section id="title">
142   <meta>
143    <title>Title Page</title>
144    <link class="next" idref="dedicate" />
145   </meta>
146
147   <data>
148
149    <!-- Frontmatter -->
150
151    <section class="numbered" id="numbered">
152     <meta><title>Numbered Sections</title></meta>
153
154     <data>
155 (End of XML Header)
156
157 for( my $sectionNumber = 1; $sectionNumber <= $numberOfSections; ++$sectionNumber ) {
158
159     my $infile = "${sectionNumber}.txt";
160
161     open( INFILE, "<$infile" ) or die "Input file \"$infile\" is not readable.\n";
162
163     my @oldlines = ( );
164     @oldlines = <INFILE>;
165
166     close INFILE;
167
168     my $title = shift @oldlines;
169     my $section = shift @oldlines;
170     my $illustration = shift @oldlines;
171     chomp $illustration;
172     $illustration =~ s/^Illustration\s+(\d+)\s+/$1/;
173     $illustration =~ s/\r//g;
174     shift @oldlines if( $illustration ne "" );
175
176     my @newlines = ( "" );
177     my $newline;
178
179     # Parsing waits for an empty line to XMLize and store
180     # the preceding lines. 
181     push( @oldlines, "" ) if( @oldlines[ $#oldlines ] ne "" );
182
183     foreach my $oldline (@oldlines) {
184         $oldline =~ s/\r|\n/ /g;
185         $oldline =~ s/^\s*(\S*)\s*$/$1/;
186         $oldline =~ s/\s\s/ /;
187         if( $oldline ne "" ) {
188             $newline .= (" " . $oldline);
189         }
190         else {
191             $newline = &xmlize( $newline, $infile );
192             $newline .= "\n" if( $newline ne "" );
193             push( @newlines, $newline );
194             $newline = "";
195         }
196     }
197
198     print "\n\n    <section class=\"numbered\" id=\"sect$sectionNumber\">\n     <meta><title>$sectionNumber</title></meta>\n\n     <data>\n";
199     print @newlines;
200     print "     </data>\n    </section>";
201 }
202
203 print << "(End of XML footer)";
204
205     </data>
206    </section>
207
208    <!-- Backmatter -->
209
210   </data>
211  </section>
212 </gamebook>
213 (End of XML footer)