5 ######################################################################
10 my $FILE_EXTENSION = 'txt';
14 die "xmlize.pl maxSectionNumber [minSectionNumber]\n" if $#ARGV < 0;
15 my $minSectionNumber = 1;
16 my $numberOfSections = shift @ARGV;
17 $minSectionNumber = shift @ARGV if $#ARGV > -1;
19 print << "(End of XML Header)";
20 <?xml version="1.0" encoding="ISO-8859-1"?>
21 <!DOCTYPE gamebook SYSTEM "gamebook.dtd" [
22 <!ENTITY % general.links SYSTEM "genlink.mod">
24 <!ENTITY % xhtml.links SYSTEM "htmllink.mod">
27 <!ENTITY % general.inclusions SYSTEM "geninc.mod">
31 <gamebook xml:lang="en-UK" version="0.12">
34 <title>[Insert Title]</title>
44 <title>Title Page</title>
45 <link class="next" idref="dedicate" />
52 <section class="numbered" id="numbered">
53 <meta><title>Numbered Sections</title></meta>
58 for( my $sectionNumber = $minSectionNumber; $sectionNumber <= $numberOfSections; ++$sectionNumber ) {
60 my $infile = "${sectionNumber}.${FILE_EXTENSION}";
62 open( INFILE, "<$infile" ) or die "Input file \"$infile\" is not readable.\n";
69 my $title = shift @oldlines;
70 my $section = shift @oldlines;
71 my $illustration = shift @oldlines;
73 $illustration =~ s/^Illustration\s+(\d+)\s+/$1/;
74 $illustration =~ s/\r//g;
75 shift @oldlines if( $illustration ne "" );
77 my @newlines = ( "" );
80 # Parsing waits for an empty line to XMLize and store
81 # the preceding lines.
82 push( @oldlines, "" ) if( $oldlines[ $#oldlines ] ne "" );
84 foreach my $oldline (@oldlines) {
85 $oldline =~ s/\r|\n/ /g;
86 $oldline =~ s/^\s*(\S*)\s*$/$1/;
87 $oldline =~ s/\s\s/ /;
88 if( $oldline ne "" ) {
89 $newline .= (" " . $oldline);
92 $newline = &xmlize( $newline, $infile );
93 $newline .= "\n" if( $newline ne "" );
94 push( @newlines, $newline );
99 print "\n\n <section class=\"numbered\" id=\"sect$sectionNumber\">\n <meta><title>$sectionNumber</title></meta>\n\n <data>\n";
101 print " </data>\n </section>";
104 print << "(End of XML footer)";
119 my( $inline, $infile ) = @_;
121 $inline =~ s/[[:space:]]*(\.\.\.|\.\s\.\s\.)[[:space:]]*/<ch.ellips\/>/g;
123 $inline =~ s/\s{2,}/ /g;
125 $inline =~ s/\&\s/<ch.ampersand\/>/g;
126 $inline =~ tr/\"\`\222\221/\'/;
127 $inline =~ s/(Random\sNumber\sTable)/<a idref=\"random\">$1<\/a>/gi;
128 $inline =~ s/(Action\sCharts?)/<a idref=\"action\">$1<\/a>/gi;
129 # \222 and \221 are some form of funky right and
130 # left quotes not present in ascii (of course)
131 $inline =~ tr/\227/-/;
132 # \227 is an em or en dash
134 $inline =~ s/^\s*(.*)\s*$/$1/;
136 if( $inline =~ /^\*/ ) {
137 $inline =~ s/^\*\s*/ <ul>\n <li>/;
138 $inline =~ s/\s*\*\s*/<\/li>\n <li>/g;
139 $inline .= "</li>\n </ul>";
141 elsif( $inline =~ /^\d+\)\s/ ) {
142 $inline =~ s/^\d+\)\s+/ <ol>\n <li>/;
143 $inline =~ s/\s*\d+\)\s+/<\/li>\n <li>/g;
144 $inline .= "</li>\n </ol>";
146 elsif( $inline =~ /^\<\!\-\-\spre\s\-\-\>/ ) {
147 $inline =~ s/^\<\!\-\-\spre\s\-\-\>//;
148 warn( "Warning: preformatted text in \"$infile\"\n" );
150 elsif( $inline =~ /^.+:\s+CLOSE\sCOMBAT\sSKILL/ ) {
151 $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;
153 elsif( $inline =~ /^.+:\s+COMBAT\sSKILL/ ) {
154 $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>/;
156 elsif( $inline =~ /^(.*)\b(return|turn|go)([a-zA-Z\s]+?to )(\d{1,3})/i ) {
157 $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;
158 $inline =~ s/\s+<\/choice>/<\/choice>/;
160 elsif( $inline =~ /^\[/ ) {
161 $inline =~ s/\[(.*)\]/$1/;
162 $inline = " <signpost>$inline</signpost>";
163 $inline =~ s/\s+<\/signpost>/<\/signpost>/;
165 elsif( $inline eq "" ) {
167 elsif( $inline =~ /^<!--(.*)-->/ ) {
168 warn( "Warning: unknown comment \"$1\" in \"$infile\"\n" );
171 $inline = " <p>$inline</p>";
172 $inline =~ s/\s+<\/p>/<\/p>/;
175 # Interferes with selecting a combat paragraph if done earlier
176 $inline =~ s/(COMBAT\sSKILL|CLOSE\sCOMBAT\sSKILL|ENDURANCE|WILLPOWER|\bCS\b|\bEP\b)([^<])/<typ class="attribute">$1<\/typ>$2/g;