5 ######################################################################
10 use open ':encoding(UTF-8)';
12 my $FILE_EXTENSION = 'txt';
13 my $BASE_INDENT = ' ';
17 die "xmlize.pl maxSectionNumber [minSectionNumber]\n" if $#ARGV < 0;
18 my $minSectionNumber = 1;
19 my $numberOfSections = shift @ARGV;
20 $minSectionNumber = shift @ARGV if $#ARGV > -1;
22 print << "(End of XML Header)";
23 <?xml version="1.0" encoding="ISO-8859-1"?>
24 <!DOCTYPE gamebook SYSTEM "gamebook.dtd" [
25 <!ENTITY % general.links SYSTEM "genlink.mod">
27 <!ENTITY % xhtml.links SYSTEM "htmllink.mod">
30 <!ENTITY % general.inclusions SYSTEM "geninc.mod">
34 <gamebook xml:lang="en-UK" version="0.13">
37 <title>[Insert Title]</title>
47 <title>Title Page</title>
48 <link class="next" idref="dedicate" />
55 <section class="numbered" id="numbered">
56 <meta><title>Numbered Sections</title></meta>
61 for( my $sectionNumber = $minSectionNumber; $sectionNumber <= $numberOfSections; ++$sectionNumber ) {
63 my $infile = "${sectionNumber}.${FILE_EXTENSION}";
65 open( INFILE, "<$infile" ) or die "Input file \"$infile\" is not readable.\n";
72 my $title = shift @oldlines;
73 my $section = shift @oldlines;
74 my $illustration = shift @oldlines;
76 $illustration =~ s/^Illustration\s+(\d+)\s+/$1/;
77 $illustration =~ s/\r//g;
78 shift @oldlines if( $illustration ne "" );
80 my @newlines = ( "" );
83 # Parsing waits for an empty line to XMLize and store
84 # the preceding lines.
85 push( @oldlines, "" ) if( $oldlines[ $#oldlines ] ne "" );
87 foreach my $oldline (@oldlines) {
88 $oldline =~ s/\r|\n/ /g;
89 $oldline =~ s/^\s*(\S*)\s*$/$1/;
90 $oldline =~ s/\s{2,}/ /;
91 if( $oldline ne "" ) {
92 $newline .= (" " . $oldline);
95 $newline = &xmlize($newline, $infile);
96 $newline .= "\n" if($newline ne "");
97 push( @newlines, $newline );
102 print "\n\n$BASE_INDENT<section class=\"numbered\" id=\"sect$sectionNumber\">\n$BASE_INDENT <meta><title>$sectionNumber</title></meta>\n\n$BASE_INDENT <data>\n";
104 print "$BASE_INDENT </data>\n$BASE_INDENT</section>";
107 print << "(End of XML footer)";
122 my( $inline, $infile ) = @_;
125 $inline =~ s/[[:space:]]{2,}/ /g;
126 $inline =~ s/[[:space:]]+$//;
127 $inline =~ s/^[[:space:]]+//;
128 $inline =~ s/[[:space:]]*(\.\.\.|\.\s\.\s\.)[[:space:]]*/<ch.ellips\/>/g;
130 $inline =~ s/\&(?=[[:space:]])/<ch.ampersand\/>/g;
131 $inline =~ tr/\"\`/\'/;
132 $inline =~ s/[\N{U+2018}\N{U+201C}]/<quote>/g;
133 $inline =~ s/[\N{U+2019}\N{U+201D}]/<\/quote>/g;
134 $inline =~ s/[\N{U+2014}]/<ch.endash\/>/g;
135 $inline =~ s/[\N{U+2014}]/<ch.emdash\/>/g;
137 $inline =~ s/(Random\sNumber\sTable)/<a idref=\"random\">$1<\/a>/gi;
138 $inline =~ s/(Action\sCharts?)/<a idref=\"action\">$1<\/a>/gi;
140 if( $inline =~ /^\*/ ) {
142 $inline =~ s/^\*\s*/$BASE_INDENT <ul>\n$BASE_INDENT <li>/;
143 $inline =~ s/\s*\*\s*/<\/li>\n$BASE_INDENT <li>/g;
144 $inline .= "</li>\n$BASE_INDENT </ul>";
146 elsif( $inline =~ /^\d+\)\s/ ) {
148 $inline =~ s/^\d+\)\s+/$BASE_INDENT <ol>\n$BASE_INDENT <li>/;
149 $inline =~ s/\s*\d+\)\s+/<\/li>\n$BASE_INDENT <li>/g;
150 $inline .= "</li>\n$BASE_INDENT </ol>";
152 elsif( $inline =~ /^\<\!\-\-\spre\s\-\-\>/ ) {
154 $inline =~ s/^\<\!\-\-\spre\s\-\-\>//;
155 warn( "Warning: pre-formatted text in \"$infile\"\n" );
157 elsif( $inline =~ /^.+:\s+CLOSE\sCOMBAT\sSKILL/ ) {
158 # Freeway Warrior combat
159 $inline =~ s/^(.+):\s+CLOSE\sCOMBAT\sSKILL\s+([0-9]+)\s+ENDURANCE\s+([0-9]+)/$BASE_INDENT <combat><enemy>$1<\/enemy><enemy-attribute class=\"closecombatskill\">$2<\/enemy-attribute><enemy-attribute class=\"endurance\">$3<\/enemy-attribute><\/combat>/g;
161 elsif( $inline =~ /^.+:\s+COMBAT\sSKILL/ ) {
163 $inline =~ s/^(.+):\s+COMBAT\sSKILL\s+([0-9]+)\s+ENDURANCE\s+([0-9]+)/$BASE_INDENT <combat><enemy>$1<\/enemy><enemy-attribute class=\"combatskill\">$2<\/enemy-attribute><enemy-attribute class=\"endurance\">$3<\/enemy-attribute><\/combat>/;
165 elsif( $inline =~ /^(.*)\b(return|turn|go)([a-zA-Z\s]+?to )(\d{1,3})/i ) {
167 $inline =~ s/^(.*)\b(return|turn|go)([a-zA-Z\s]+?to )(\d{1,3})(.*)/$BASE_INDENT <choice idref=\"sect$4\">$1<link-text>$2$3$4<\/link-text>$5<\/choice>/i;
168 $inline =~ s/\s+<\/choice>/<\/choice>/;
170 elsif( $inline =~ /^\[/ ) {
172 $inline =~ s/\[(.*)\]/$1/;
173 $inline = "$BASE_INDENT <signpost>$inline</signpost>";
174 $inline =~ s/\s+<\/signpost>/<\/signpost>/;
176 elsif( $inline =~ /^<!--(.*)-->/ ) {
178 warn( "Warning: unknown comment \"$1\" in \"$infile\"\n" );
180 elsif( $inline eq "" ) {
184 $inline = "$BASE_INDENT <p>$inline</p>";
187 # Interferes with selecting a combat paragraph if done earlier
188 $inline =~ s/(COMBAT\sSKILL|CLOSE\sCOMBAT\sSKILL|ENDURANCE|WILLPOWER|\bCS\b|\bEP\b)([^<])/<typ class="attribute">$1<\/typ>$2/g;