| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | # gxml2html: generic, template-based XML to HTML conversion tool | ||
| 4 | # version 2.2 | ||
| 5 | # Copyright (C) 1999-2001 Josh Carter <josh@multipart-mixed.com> | ||
| 6 | # All rights reserved. | ||
| 7 | # | ||
| 8 | # This program is free software; you can redistribute it and/or modify | ||
| 9 | # it under the same terms as Perl itself. | ||
| 10 | |||
| 11 | # | ||
| 12 | # Global vars | ||
| 13 | # | ||
| 14 | use vars ('$gxml', | ||
| 15 | '%config', | ||
| 16 | '%remappings', | ||
| 17 | '%modtimes', | ||
| 18 | '$baseDir', | ||
| 19 | '$workDir', | ||
| 20 | '$finalDir', | ||
| 21 | '$templateDir', | ||
| 22 | '$templatesChanged', | ||
| 23 | '$currentFile', | ||
| 24 | '$output', | ||
| 25 | '$debugMode', | ||
| 26 | ); | ||
| 27 | |||
| 28 | # Stuff that comes with Perl and/or CPAN | ||
| 29 | use strict; | ||
| 30 | use Cwd; | ||
| 31 | use Getopt::Long; | ||
| 32 | use File::Copy; | ||
| 33 | use XML::GXML; # of course | ||
| 34 | |||
| 35 | $baseDir = cwd(); | ||
| 36 | |||
| 37 | # | ||
| 38 | # Figure out our configuration | ||
| 39 | # | ||
| 40 | %config = ReadConfig(); | ||
| 41 | |||
| 42 | # | ||
| 43 | # Create new GXML object | ||
| 44 | # | ||
| 45 | $gxml = new XML::GXML(\%config); | ||
| 46 | |||
| 47 | # | ||
| 48 | # Check template mod times | ||
| 49 | # | ||
| 50 | $templatesChanged = $gxml->TemplateMgr()->CheckModified(); | ||
| 51 | $gxml->TemplateMgr()->UpdateModified(); | ||
| 52 | |||
| 53 | # | ||
| 54 | # Assemble the list of files to process | ||
| 55 | # | ||
| 56 | $baseDir = cwd(); | ||
| 57 | chdir($workDir); | ||
| 58 | my $fileListRef = XML::GXML::Util::GetFileList(); | ||
| 59 | chdir($baseDir); | ||
| 60 | |||
| 61 | # | ||
| 62 | # Itererate over the file list and format/copy | ||
| 63 | # each item as appropriate. | ||
| 64 | # | ||
| 65 | foreach my $filename (@$fileListRef) | ||
| 66 | { | ||
| 67 | chdir($baseDir); | ||
| 68 | |||
| 69 | $currentFile = $filename; | ||
| 70 | |||
| 71 | my $source = File::Spec->catfile($workDir, $filename); | ||
| 72 | my $dest = File::Spec->catfile($finalDir, $filename); | ||
| 73 | my $sourcemod = (stat $source)[9]; | ||
| 74 | my $changed; | ||
| 75 | |||
| 76 | # If the destination file exists and the source file hasn't been | ||
| 77 | # modified since the last time we were run, skip the file. BUT, if | ||
| 78 | # a template changed, we want to still processes all .xml files. | ||
| 79 | if ((-f $dest) && (defined($modtimes{$filename})) && | ||
| 80 | ($sourcemod == $modtimes{$filename})) | ||
| 81 | { | ||
| 82 | $changed = 0; | ||
| 83 | } | ||
| 84 | else | ||
| 85 | { | ||
| 86 | $changed = 1; | ||
| 87 | $modtimes{$filename} = $sourcemod; | ||
| 88 | } | ||
| 89 | |||
| 90 | if ($filename =~ /\.xml$/) | ||
| 91 | { | ||
| 92 | (my $htmldest = $dest) =~ s/\.xml$/\.html/; | ||
| 93 | next unless (!-f $htmldest || $changed || $templatesChanged); | ||
| 94 | |||
| 95 | print "processing XML file $filename...\n"; | ||
| 96 | |||
| 97 | $gxml->ProcessFile($source, $htmldest); | ||
| 98 | |||
| 99 | # also copy the XML source in case someone wants it. | ||
| 100 | HandleSomeOtherFile($source, $dest); | ||
| 101 | } | ||
| 102 | else | ||
| 103 | { | ||
| 104 | next unless $changed; | ||
| 105 | |||
| 106 | print "copying file $filename...\n"; | ||
| 107 | |||
| 108 | HandleSomeOtherFile($source, $dest); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | # Now flush the mod times back out to the .modtimes file. | ||
| 113 | XML::GXML::Util::SaveModtimes(File::Spec->catfile($workDir, '.modtimes'), \%modtimes); | ||
| 114 | |||
| 115 | print "\nall done!\n\n"; | ||
| 116 | |||
| 117 | |||
| 118 | ####################################################################### | ||
| 119 | # END OF MAIN; subroutines from here down | ||
| 120 | ####################################################################### | ||
| 121 | |||
| 122 | |||
| 123 | # | ||
| 124 | # HandleSomeOtherFile | ||
| 125 | # | ||
| 126 | # Just copy the file verbatim to the result directory. | ||
| 127 | # | ||
| 128 | sub HandleSomeOtherFile | ||
| 129 | { | ||
| 130 | my ($source, $dest) = @_; | ||
| 131 | |||
| 132 | # Change to dest directory and back to create | ||
| 133 | # any necessary directories... | ||
| 134 | XML::GXML::Util::ChangeToDirectory($dest); | ||
| 135 | |||
| 136 | chdir($baseDir); | ||
| 137 | |||
| 138 | # Achtung! If the source and dest directories are the same, we do | ||
| 139 | # *not* want to copy a file onto itself. File::Copy will blow away | ||
| 140 | # the file in question. That would be a Very Bad Thing(tm). | ||
| 141 | # | ||
| 142 | # NOTE: I don't know if this works on non-*nix platforms, but it's | ||
| 143 | # the recommended way to do it, according to the Perl Cookbook. | ||
| 144 | my ($srcdev, $srcnode) = stat $source; | ||
| 145 | my ($dstdev, $dstnode) = stat $dest; | ||
| 146 | return if (($srcdev == $dstdev) && ($srcnode == $dstnode)); | ||
| 147 | |||
| 148 | # ...and then make File::Copy do something useful with itself. | ||
| 149 | copy($source, $dest) or print "couldn't copy $source: $!\n"; | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | # | ||
| 154 | # ReadConfig | ||
| 155 | # | ||
| 156 | # Scan our command line params looking for configuration directives, | ||
| 157 | # then look for more config stuff in .gxml2html files in either this | ||
| 158 | # directory or the source file directory. | ||
| 159 | # | ||
| 160 | sub ReadConfig | ||
| 161 | { | ||
| 162 | my ($force, $noHTML, $dashConvert, %remappings, | ||
| 163 | $templateDir, $urlPrefix, $configFile); | ||
| 164 | |||
| 165 | # First, grab any command line params. These override anything | ||
| 166 | # found in a configuration file. | ||
| 167 | my %options = ("debug" => \$debugMode, | ||
| 168 | "nohtml" => \$noHTML, | ||
| 169 | "dash" => \$dashConvert, | ||
| 170 | "work" => \$workDir, | ||
| 171 | "source" => \$workDir, | ||
| 172 | "final" => \$finalDir, | ||
| 173 | "dest" => \$finalDir, | ||
| 174 | "templates" => \$templateDir, | ||
| 175 | "urlprefix" => \$urlPrefix, | ||
| 176 | "force" => \$force, | ||
| 177 | "config" => \$configFile); | ||
| 178 | |||
| 179 | # Thank goodness for standard perl modules. | ||
| 180 | GetOptions(\%options, "debug", "nohtml", "dash", "work=s", | ||
| 181 | "source=s", "final=s", "dest=s", "templates=s", | ||
| 182 | "urlprefix=s", "config=s", "force"); | ||
| 183 | |||
| 184 | # Now try digging for config files in case we didn't get any | ||
| 185 | # command line params. Look in work file directory first, | ||
| 186 | # otherwise in the directory of this script. | ||
| 187 | if (defined $configFile) | ||
| 188 | { | ||
| 189 | # user passed it in | ||
| 190 | } | ||
| 191 | if ((defined $workDir) && | ||
| 192 | (-f File::Spec->catfile($workDir, '.gxml2html'))) | ||
| 193 | { | ||
| 194 | $configFile = File::Spec->catfile($workDir, '.gxml2html'); | ||
| 195 | } | ||
| 196 | elsif (-f File::Spec->catfile(File::Spec->curdir, '.gxml2html')) | ||
| 197 | { | ||
| 198 | $configFile = File::Spec->catfile(File::Spec->curdir, '.gxml2html'); | ||
| 199 | } | ||
| 200 | |||
| 201 | if ($configFile) | ||
| 202 | { | ||
| 203 | my $line; | ||
| 204 | |||
| 205 | open(CONFIG, $configFile); | ||
| 206 | |||
| 207 | while ($line = <CONFIG>) | ||
| 208 | { | ||
| 209 | chomp($line); | ||
| 210 | |||
| 211 | # Only set options which exist but whose values are | ||
| 212 | # not yet defined. | ||
| 213 | if (($line =~ /^\s*(\w+)[\s:=]+(\S+)/) && | ||
| 214 | (exists $options{$1} && !defined ${$options{$1}})) | ||
| 215 | { | ||
| 216 | ${$options{$1}} = $2; | ||
| 217 | } | ||
| 218 | elsif ($line =~ /^\s*<(\S+)>[\s:=]*<?([^\s>]*)>?/) | ||
| 219 | { | ||
| 220 | # tag remappings of the form <premap> <aftermap> | ||
| 221 | XML::GXML::Util::Log("remapping $1 to $2"); | ||
| 222 | $remappings{$1} = $2; | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | close(CONFIG); | ||
| 227 | } | ||
| 228 | |||
| 229 | # If none of that worked, just make up stuff. | ||
| 230 | $workDir = File::Spec->catdir(File::Spec->curdir, 'work') | ||
| 231 | unless defined $workDir; | ||
| 232 | $finalDir = File::Spec->catdir(File::Spec->curdir, 'final') | ||
| 233 | unless defined $finalDir; | ||
| 234 | $templateDir = File::Spec->catdir(File::Spec->curdir, 'templates') | ||
| 235 | unless defined $templateDir; | ||
| 236 | $urlPrefix = '/' unless defined $urlPrefix; | ||
| 237 | |||
| 238 | die "source directory does not exist\n" unless -d $workDir; | ||
| 239 | die "output directory does not exist\n" unless -d $finalDir; | ||
| 240 | die "templates directory does not exist\n" unless -d $templateDir; | ||
| 241 | |||
| 242 | # Load the modification times of the stuff we're processing into | ||
| 243 | # the %modtimes hash. We'll use this later to determine if we | ||
| 244 | # really need to process the file. | ||
| 245 | unless ($force) | ||
| 246 | { | ||
| 247 | XML::GXML::Util::LoadModtimes(File::Spec->catfile($workDir, '.modtimes'), \%modtimes); | ||
| 248 | } | ||
| 249 | |||
| 250 | XML::GXML::Util::Log("source directory : $workDir"); | ||
| 251 | XML::GXML::Util::Log("output directory : $finalDir"); | ||
| 252 | XML::GXML::Util::Log("templates directory : $templateDir"); | ||
| 253 | XML::GXML::Util::Log("base directory : $baseDir"); | ||
| 254 | |||
| 255 | return ('work' => $workDir, | ||
| 256 | 'final' => $finalDir, | ||
| 257 | 'templateDir' => $templateDir, | ||
| 258 | 'remappings' => \%remappings, | ||
| 259 | 'urlprefix' => $urlPrefix, | ||
| 260 | 'dashConvert' => $dashConvert, | ||
| 261 | 'debugMode' => $debugMode, | ||
| 262 | 'html' => 1, | ||
| 263 | ); | ||
| 264 | } | ||
| 265 |