5 ######################################################################
10 use open ':encoding(UTF-8)';
12 my $FILE_EXTENSION = 'txt';
16 die "xmlize.pl maxSectionNumber [minSectionNumber]\n" if $#ARGV < 0;
17 my $minSectionNumber = 1;
18 my $numberOfSections = shift @ARGV;
19 $minSectionNumber = shift @ARGV if $#ARGV > -1;
21 print << "(End of XML Header)";
22 <?xml version="1.0" encoding="ISO-8859-1"?>
23 <!DOCTYPE gamebook SYSTEM "gamebook.dtd" [
24 <!ENTITY % general.links SYSTEM "genlink.mod">
26 <!ENTITY % xhtml.links SYSTEM "htmllink.mod">
29 <!ENTITY % general.inclusions SYSTEM "geninc.mod">
33 <gamebook xml:lang="en-UK" version="0.12">
36 <title>[Insert Title]</title>
46 <title>Title Page</title>
47 <link class="next" idref="dedicate" />
54 <section class="numbered" id="numbered">
55 <meta><title>Numbered Sections</title></meta>
60 for( my $sectionNumber = $minSectionNumber; $sectionNumber <= $numberOfSections; ++$sectionNumber ) {
62 my $infile = "${sectionNumber}.${FILE_EXTENSION}";
64 open( INFILE, "<$infile" ) or die "Input file \"$infile\" is not readable.\n";
71 my $title = shift @oldlines;
72 my $section = shift @oldlines;
73 my $illustration = shift @oldlines;
75 $illustration =~ s/^Illustration\s+(\d+)\s+/$1/;
76 $illustration =~ s/\r//g;
77 shift @oldlines if( $illustration ne "" );
79 my @newlines = ( "" );
82 # Parsing waits for an empty line to XMLize and store
83 # the preceding lines.
84 push( @oldlines, "" ) if( $oldlines[ $#oldlines ] ne "" );
86 foreach my $oldline (@oldlines) {
87 $oldline =~ s/\r|\n/ /g;
88 $oldline =~ s/^\s*(\S*)\s*$/$1/;
89 $oldline =~ s/\s{2,}/ /;
90 if( $oldline ne "" ) {
91 $newline .= (" " . $oldline);
94 $newline = &xmlize($newline, $infile);
95 $newline .= "\n" if($newline ne "");
96 push( @newlines, $newline );
101 print "\n\n <section class=\"numbered\" id=\"sect$sectionNumber\">\n <meta><title>$sectionNumber</title></meta>\n\n <data>\n";
103 print " </data>\n </section>";
106 print << "(End of XML footer)";
121 my( $inline, $infile ) = @_;
124 $inline =~ s/[[:space:]]{2,}/ /g;
125 $inline =~ s/[[:space:]]+$//;
126 $inline =~ s/^[[:space:]]+//;
127 $inline =~ s/[[:space:]]*(\.\.\.|\.\s\.\s\.)[[:space:]]*/<ch.ellips\/>/g;
129 $inline =~ s/\&(?=[[:space:]])/<ch.ampersand\/>/g;
130 $inline =~ tr/\"\`/\'/;
131 $inline =~ s/[\N{U+2018}\N{U+201C}]/<quote>/g;
132 $inline =~ s/[\N{U+2019}\N{U+201D}]/<\/quote>/g;
133 $inline =~ s/[\N{U+2014}]/<ch.endash\/>/g;
134 $inline =~ s/[\N{U+2014}]/<ch.emdash\/>/g;
136 $inline =~ s/(Random\sNumber\sTable)/<a idref=\"random\">$1<\/a>/gi;
137 $inline =~ s/(Action\sCharts?)/<a idref=\"action\">$1<\/a>/gi;
139 if( $inline =~ /^\*/ ) {
141 $inline =~ s/^\*\s*/ <ul>\n <li>/;
142 $inline =~ s/\s*\*\s*/<\/li>\n <li>/g;
143 $inline .= "</li>\n </ul>";
145 elsif( $inline =~ /^\d+\)\s/ ) {
147 $inline =~ s/^\d+\)\s+/ <ol>\n <li>/;
148 $inline =~ s/\s*\d+\)\s+/<\/li>\n <li>/g;
149 $inline .= "</li>\n </ol>";
151 elsif( $inline =~ /^\<\!\-\-\spre\s\-\-\>/ ) {
153 $inline =~ s/^\<\!\-\-\spre\s\-\-\>//;
154 warn( "Warning: pre-formatted text in \"$infile\"\n" );
156 elsif( $inline =~ /^.+:\s+CLOSE\sCOMBAT\sSKILL/ ) {
157 # Freeway Warrior combat
158 $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;
160 elsif( $inline =~ /^.+:\s+COMBAT\sSKILL/ ) {
162 $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>/;
164 elsif( $inline =~ /^(.*)\b(return|turn|go)([a-zA-Z\s]+?to )(\d{1,3})/i ) {
166 $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;
167 $inline =~ s/\s+<\/choice>/<\/choice>/;
169 elsif( $inline =~ /^\[/ ) {
171 $inline =~ s/\[(.*)\]/$1/;
172 $inline = " <signpost>$inline</signpost>";
173 $inline =~ s/\s+<\/signpost>/<\/signpost>/;
175 elsif( $inline =~ /^<!--(.*)-->/ ) {
177 warn( "Warning: unknown comment \"$1\" in \"$infile\"\n" );
179 elsif( $inline eq "" ) {
183 $inline = " <p>$inline</p>";
186 # Interferes with selecting a combat paragraph if done earlier
187 $inline =~ s/(COMBAT\sSKILL|CLOSE\sCOMBAT\sSKILL|ENDURANCE|WILLPOWER|\bCS\b|\bEP\b)([^<])/<typ class="attribute">$1<\/typ>$2/g;