5 ######################################################################
10 use open ':encoding(UTF-8)';
11 use open ':std', ':encoding(UTF-8)';
13 my $FILE_EXTENSION = 'txt';
14 my $BASE_INDENT = ' ';
18 die "xmlize.pl maxSectionNumber [minSectionNumber]\n" if $#ARGV < 0;
19 my $minSectionNumber = 1;
20 my $numberOfSections = shift @ARGV;
21 $minSectionNumber = shift @ARGV if $#ARGV > -1;
23 print << "(End of XML Header)";
24 <?xml version="1.0" encoding="ISO-8859-1"?>
25 <!DOCTYPE gamebook SYSTEM "gamebook.dtd" [
26 <!ENTITY % general.links SYSTEM "genlink.mod">
28 <!ENTITY % xhtml.links SYSTEM "htmllink.mod">
31 <!ENTITY % general.inclusions SYSTEM "geninc.mod">
35 <gamebook xml:lang="en-UK" version="0.13">
38 <title>[Insert Title]</title>
48 <title>Title Page</title>
49 <link class="next" idref="dedicate" />
56 <section class="numbered" id="numbered">
57 <meta><title>Numbered Sections</title></meta>
62 for( my $sectionNumber = $minSectionNumber; $sectionNumber <= $numberOfSections; ++$sectionNumber ) {
64 my $infile = "${sectionNumber}.${FILE_EXTENSION}";
66 open( INFILE, "<$infile" ) or die "Input file \"$infile\" is not readable.\n";
73 my $title = shift @oldlines;
74 my $section = shift @oldlines;
75 my $illustration = shift @oldlines;
77 $illustration =~ s/^Illustration\s+(\d+)\s+/$1/;
78 $illustration =~ s/\r//g;
79 shift @oldlines if( $illustration ne "" );
81 my @newlines = ( "" );
84 # Parsing waits for an empty line to XMLize and store
85 # the preceding lines.
86 push( @oldlines, "" ) if( $oldlines[ $#oldlines ] ne "" );
88 foreach my $oldline (@oldlines) {
89 $oldline =~ s/\r|\n/ /g;
90 $oldline =~ s/^\s*(\S*)\s*$/$1/;
91 $oldline =~ s/\s{2,}/ /;
92 if( $oldline ne "" ) {
93 $newline .= (" " . $oldline);
96 $newline = &xmlize($newline, $infile);
97 $newline .= "\n" if($newline ne "");
98 push( @newlines, $newline );
103 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";
105 print "$BASE_INDENT </data>\n$BASE_INDENT</section>";
108 print << "(End of XML footer)";
123 my( $inline, $infile ) = @_;
125 if(!defined $inline || $inline eq "") {
130 $inline =~ s/[[:space:]]{2,}/ /g;
131 $inline =~ s/[[:space:]]+$//;
132 $inline =~ s/^[[:space:]]+//;
133 $inline =~ s/[[:space:]]*(\.\.\.|\.\s\.\s\.)[[:space:]]*/<ch.ellips\/>/g;
135 $inline =~ s/\&(?=[[:space:]])/<ch.ampersand\/>/g;
136 $inline =~ tr/\"\`/\'/;
137 $inline =~ s/[\N{U+2018}\N{U+201C}]/<quote>/g;
138 $inline =~ s|[\N{U+2019}\N{U+201D}]([[:alpha:]])|<ch.apos/>$1|g;
139 $inline =~ s/[\N{U+2019}\N{U+201D}]/<\/quote>/g;
140 $inline =~ s/[\N{U+2014}]/<ch.endash\/>/g;
141 $inline =~ s/[\N{U+2014}]/<ch.emdash\/>/g;
143 $inline =~ s/(Random\sNumber\sTable)/<a idref=\"random\">$1<\/a>/gi;
144 $inline =~ s/(Action\sCharts?)/<a idref=\"action\">$1<\/a>/gi;
146 if( $inline =~ /^\*/ ) {
148 $inline =~ s/^\*\s*/$BASE_INDENT <ul>\n$BASE_INDENT <li>/;
149 $inline =~ s/\s*\*\s*/<\/li>\n$BASE_INDENT <li>/g;
150 $inline .= "</li>\n$BASE_INDENT </ul>";
152 elsif( $inline =~ /^\d+\)\s/ ) {
154 $inline =~ s/^\d+\)\s+/$BASE_INDENT <ol>\n$BASE_INDENT <li>/;
155 $inline =~ s/\s*\d+\)\s+/<\/li>\n$BASE_INDENT <li>/g;
156 $inline .= "</li>\n$BASE_INDENT </ol>";
158 elsif( $inline =~ /^\<\!\-\-\spre\s\-\-\>/ ) {
160 $inline =~ s/^\<\!\-\-\spre\s\-\-\>//;
161 warn( "Warning: pre-formatted text in \"$infile\"\n" );
163 elsif( $inline =~ /^.+:\s+CLOSE\sCOMBAT\sSKILL/ ) {
164 # Freeway Warrior combat
165 $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;
167 elsif( $inline =~ /^.+:\s+COMBAT\sSKILL/ ) {
169 $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>/;
171 elsif( $inline =~ /^(.*)\b(return|turn|go)([a-zA-Z\s]+?to )(\d{1,3})/i ) {
173 $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;
174 $inline =~ s/\s+<\/choice>/<\/choice>/;
176 elsif( $inline =~ /^\[/ ) {
178 $inline =~ s/\[(.*)\]/$1/;
179 $inline = "$BASE_INDENT <signpost>$inline</signpost>";
180 $inline =~ s/\s+<\/signpost>/<\/signpost>/;
182 elsif( $inline =~ /^<!--(.*)-->/ ) {
184 warn( "Warning: unknown comment \"$1\" in \"$infile\"\n" );
186 elsif( $inline eq "" ) {
190 $inline = "$BASE_INDENT <p>$inline</p>";
193 # Interferes with selecting a combat paragraph if done earlier
194 $inline =~ s/(COMBAT\sSKILL|CLOSE\sCOMBAT\sSKILL|ENDURANCE|WILLPOWER|\bCS\b|\bEP\b)([^<])/<typ class="attribute">$1<\/typ>$2/g;