Fix rules to point to correct locations
[project-aon.git] / common / scripts / xmlize.pl
1 #!/usr/bin/perl -w
2 #
3 # xmlize.pl
4 #
5 ######################################################################
6
7 #use strict;
8
9 $FILE_EXTENSION = 'txt';
10
11 #### Subroutines
12
13 sub xmlize {
14     my( $inline, $infile ) = @_;
15
16     $inline =~ s/[[:space:]]*(\.\.\.|\.\s\.\s\.)[[:space:]]*/<ch.ellips\/>/g;
17     $inline =~ tr/\t/ /;
18     $inline =~ s/\s{2,}/ /g;
19     $inline =~ s/\s+$//;
20     $inline =~ s/\&\s/<ch.ampersand\/>/g;
21     $inline =~ tr/\"\`\222\221/\'/;
22     $inline =~ s/(Random\sNumber\sTable)/<a idref=\"random\">$1<\/a>/gi;
23     $inline =~ s/(Action\sCharts?)/<a idref=\"action\">$1<\/a>/gi;
24     # \222 and \221 are some form of funky right and
25     # left quotes not present in ascii (of course) 
26     $inline =~ tr/\227/-/;
27     # \227 is an em or en dash
28
29     $inline =~ s/^\s*(.*)\s*$/$1/;
30
31     if( $inline =~ /^\*/ ) {
32         $inline =~ s/^\*\s*/       <ul>\n        <li>/;
33         $inline =~ s/\s*\*\s*/<\/li>\n        <li>/g;
34         $inline .= "</li>\n       </ul>";
35     }
36     elsif( $inline =~ /^\d+\)\s/ ) {
37         $inline =~ s/^\d+\)\s+/       <ol>\n        <li>/;
38         $inline =~ s/\s*\d+\)\s+/<\/li>\n        <li>/g;
39         $inline .= "</li>\n       </ol>";
40     }
41     elsif( $inline =~ /^\<\!\-\-\spre\s\-\-\>/ ) {
42         $inline =~ s/^\<\!\-\-\spre\s\-\-\>//;
43         warn( "Warning: preformatted text in \"$infile\"\n" );
44     }
45     elsif( $inline =~ /^.+:\s+CLOSE\sCOMBAT\sSKILL/ ) {
46         $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;
47     }
48     elsif( $inline =~ /^.+:\s+COMBAT\sSKILL/ ) {
49         $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>/;
50     }
51     elsif( $inline =~ /^(.*)\b(return|turn|go)([a-zA-Z\s]+?to )(\d{1,3})/i ) {
52         $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;
53         $inline =~ s/\s+<\/choice>/<\/choice>/;
54     }
55     elsif( $inline =~ /^\[/ ) {
56         $inline =~ s/\[(.*)\]/$1/;
57         $inline = "       <signpost>$inline</signpost>";
58         $inline =~ s/\s+<\/signpost>/<\/signpost>/;
59     }
60     elsif( $inline eq "" ) {
61     }
62     elsif( $inline =~ /^<!--(.*)-->/ ) {
63         warn( "Warning: unknown comment \"$1\" in \"$infile\"\n" );
64     }
65     else {
66         $inline = "       <p>$inline</p>";
67         $inline =~ s/\s+<\/p>/<\/p>/;
68     }
69
70 # Interferes with selecting a combat paragraph if done earlier
71     $inline =~ s/(COMBAT\sSKILL|CLOSE\sCOMBAT\sSKILL|ENDURANCE|WILLPOWER|\bCS\b|\bEP\b)([^<])/<typ class="attribute">$1<\/typ>$2/g;
72
73     return $inline;
74 }
75
76 #### Main Routine
77
78 die "xmlize.pl maxSectionNumber [minSectionNumber]\n" if $#ARGV < 0;
79 my $minSectionNumber = 1;
80 my $numberOfSections = shift @ARGV;
81 $minSectionNumber = shift @ARGV if $#ARGV > -1;
82
83 print << "(End of XML Header)";
84 <?xml version="1.0" encoding="ISO-8859-1"?>
85 <!DOCTYPE gamebook SYSTEM "gamebook.dtd" [
86  <!ENTITY % general.links SYSTEM "genlink.mod">
87  %general.links;
88  <!ENTITY % xhtml.links   SYSTEM "htmllink.mod">
89  %xhtml.links;
90
91  <!ENTITY % general.inclusions SYSTEM "geninc.mod">
92  %general.inclusions;
93 ]>
94
95 <gamebook xml:lang="en-UK" version="0.12">
96
97  <meta>
98   <title>[Insert Title]</title>
99  </meta>
100
101  <section id="toc">
102   <meta />
103   <data />
104  </section>
105
106  <section id="title">
107   <meta>
108    <title>Title Page</title>
109    <link class="next" idref="dedicate" />
110   </meta>
111
112   <data>
113
114    <!-- Frontmatter -->
115
116    <section class="numbered" id="numbered">
117     <meta><title>Numbered Sections</title></meta>
118
119     <data>
120 (End of XML Header)
121
122 for( my $sectionNumber = $minSectionNumber; $sectionNumber <= $numberOfSections; ++$sectionNumber ) {
123
124     my $infile = "${sectionNumber}.${FILE_EXTENSION}";
125
126     open( INFILE, "<$infile" ) or die "Input file \"$infile\" is not readable.\n";
127
128     my @oldlines = ( );
129     @oldlines = <INFILE>;
130
131     close INFILE;
132
133     my $title = shift @oldlines;
134     my $section = shift @oldlines;
135     my $illustration = shift @oldlines;
136     chomp $illustration;
137     $illustration =~ s/^Illustration\s+(\d+)\s+/$1/;
138     $illustration =~ s/\r//g;
139     shift @oldlines if( $illustration ne "" );
140
141     my @newlines = ( "" );
142     my $newline;
143
144     # Parsing waits for an empty line to XMLize and store
145     # the preceding lines. 
146     push( @oldlines, "" ) if( $oldlines[ $#oldlines ] ne "" );
147
148     foreach my $oldline (@oldlines) {
149         $oldline =~ s/\r|\n/ /g;
150         $oldline =~ s/^\s*(\S*)\s*$/$1/;
151         $oldline =~ s/\s\s/ /;
152         if( $oldline ne "" ) {
153             $newline .= (" " . $oldline);
154         }
155         else {
156             $newline = &xmlize( $newline, $infile );
157             $newline .= "\n" if( $newline ne "" );
158             push( @newlines, $newline );
159             $newline = "";
160         }
161     }
162
163     print "\n\n    <section class=\"numbered\" id=\"sect$sectionNumber\">\n     <meta><title>$sectionNumber</title></meta>\n\n     <data>\n";
164     print @newlines;
165     print "     </data>\n    </section>";
166 }
167
168 print << "(End of XML footer)";
169
170     </data>
171    </section>
172
173    <!-- Backmatter -->
174
175   </data>
176  </section>
177 </gamebook>
178 (End of XML footer)