4 # artemus - HTML (and other things) Preprocessor
6 # Copyright (C) 2000/2002 Angel Ortega <angel@triptico.com>
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 # http://www.triptico.com
30 $artemus_id="Artemus $VERSION";
32 # substitution variables
35 # substitution functions
38 # source and destination files
43 $config_file="artemus.conf";
45 # local configuration file
46 $local_config_file="local-artemus.conf";
51 # use cr/lf instead of lf
54 # append instead of overwrite output file
57 # send files using ftp
63 # create makefile template
69 # inverse substitutions
70 $inverse_config_file="artemus-inv.conf";
76 # include debugging info
79 # list of unresolved symbols
82 #####################################################################
85 usage
() if(!GetOptions
( "i|input=s" => \
$src,
86 "o|output=s" => \
$dst,
87 "c|conf=s" => \
$config_file,
88 "p|paragraph=s" => \
$para_sep,
89 "m|msdos" => \
$use_cr_lf,
90 "a|append" => \
$append,
91 "v|version-only" => \
$version_only,
93 "k|makefile" => \
$makefile_template,
94 "s|site-map" => \
$site_map,
96 "l|include-path=s" => \
$include_path,
98 "h|help" => \
$help) or $help);
100 $dst=">-" if $dst eq "-";
102 version_only
() if $version_only;
104 make_makefile
() if $makefile_template;
106 build_site_map
(@ARGV) if $site_map;
114 # read the configuration files
115 read_config
($config_file) if -f
$config_file;
116 read_config
($local_config_file) if -f
$local_config_file;
117 read_config
($inverse_config_file,1) if -f
$inverse_config_file;
119 ftp_send
() if $use_ftp;
121 open F
, $src or die "can't open '$src'";
128 ###########################################
130 # Artemus main processing function
132 ###########################################
139 my ($vars,$inv_vars,$funcs);
140 my ($unresolved,$res);
145 # make hashes comfortable
147 $inv_vars=$opts{'inv-vars'};
148 $funcs=$opts{'funcs'};
149 $unresolved=$opts{'unresolved'};
152 # {-\n}, substitutes as \n
156 $funcs->{"localtime"}=sub { scalar(localtime) };
157 $funcs->{"if"}=sub { $_[0] ?
return($_[1]) : return("") };
158 $funcs->{"ifelse"}=sub { $_[0] ?
return($_[1]) : return($_[2]) };
160 # if defined, substitute the paragraphs
161 # with the paragraph separator
162 if($opts{'paragraph-separator'})
164 $data =~ s/\n\n/\n$opts{'paragraph-separator'}\n/g;
167 # concat special variables BEGIN & END
168 $data = $vars->{"\\BEGIN"} . $data . $vars->{"\\END"};
170 # inverse substitutions
171 for my $i (keys(%$inv_vars))
173 next if $inv_vars->{$i} =~ /\$/;
175 $data =~ s/\b($i)\b/\{\-$1\}/g;
178 # start counting resolutions
181 # main function, variable and include substitutions
182 while($data =~ /{-([^}{]*)}/s)
185 my ($key,@params,$text,$n);
187 ($key,@params)=split(/\|/,$found);
189 # exclude dangerous keys
190 unless($key =~ /^[-\\\w_ \.]+$/)
196 elsif(defined $vars->{$key})
200 for($n=0;$text =~ /\$$n/;$n++)
202 $text =~ s/\$$n/$params[$n]/g;
207 elsif(defined $funcs->{$key})
211 $func=$funcs->{$key};
212 $text=&$func(@params);
214 # functions can abort further execution
215 last if $artemus_abort;
219 elsif($opts{'include-path'} and
220 open (INC
, "$opts{'include-path'}/$key"))
222 $text=join("",<INC
>);
225 for($n=0;$text =~ /\$$n/;$n++)
227 $text =~ s/\$$n/$params[$n]/g;
231 unless(defined $text)
233 # print STDERR "unresolved: '$found'\n" if not $quiet;
234 push(@
$unresolved,$found);
238 # include debug information, if necessary
239 $text="<!-- artemus: $key ($res) -->$text<!-- artemus: /$key ($res) -->" if $opts{'debug'};
241 # make the substitution
242 $data =~ s/{-\Q$found\E}/$text/;
248 # finally, convert end of lines if necessary
249 $data =~ s/\n/\r\n/g if($opts{'use-cr-lf'});
258 $data="<meta name=\"generator\" content=\"$artemus_id\">\n".$data;
259 $data=sprintf("<!-- Date: %s -->\n",scalar(localtime)).$data;
260 $data="<!-- Built with $artemus_id Angel Ortega 2000/2002 -->\n".$data;
264 $funcs{"filesize"}=sub { -s
$_[0] };
265 $funcs{"shell"}=sub { $_=`$_[0]`; chop; return $_ };
267 $data=artemus
($data, "paragraph-separator" => $para_sep,
269 "inv-vars" => \
%inv_vars,
271 "include-path" => $include_path,
272 "use-cr-lf" => $use_cr_lf,
274 "unresolved" => \
@unresolved
279 open F
, ($append?
">":"").">$dst" or die "can't write '$dst'";
286 foreach my $e (@unresolved)
288 print STDERR
"Unresolved: '$e'\n";
295 # #####################################################################
299 my ($conf,$inverse)=@_;
303 unless(open F
, $conf)
308 { die "'$conf' bad config file"; }
319 ($key,$val)=split("=",$_,2);
326 elsif($val eq "<<EOF")
328 # 'document here' construction
338 elsif($key eq "\\INCLUDE")
345 $inv_vars{$key}=$val if($inverse);
355 print("$artemus_id - HTML (and other things) Preprocessor\n");
356 print("Copyright (C) 2000/2002 Angel Ortega <angel\@triptico.com>\n\n");
359 print(" artemus -i|--input={input file} -o|--output={output file}\n");
360 print(" [-c|--conf={config file}]\n");
361 print(" [-l|--include-path={path to includes}]\n");
362 print(" [-p|--paragraph={paragraph_separator}]\n");
363 print(" [-q|--quiet]\n");
364 print(" [-m|--msdos] [-a|--append] [-d|--debug]\n\n");
366 print(" artemus -f|--ftp {files to ftp...}\n\n");
368 print(" artemus -k|--makefile\n");
370 print(" artemus -s|--site-map\n");
384 # send files using ftp
392 print "Nothing to send.\n";
396 print "Connecting to $vars{'ftp.host'}...\n";
397 $ftp=Net
::FTP
->new($vars{'ftp.host'});
400 print "Logging in as $vars{'ftp.user'}...\n";
401 $ftp->login($vars{'ftp.user'},$vars{'ftp.passwd'})
402 or die "ftp login error";
405 if(defined($vars{'ftp.dir'}))
407 print "Chdir $vars{'ftp.dir'}...\n";
408 $ftp->cwd($vars{'ftp.dir'});
414 foreach my $f (@ARGV)
416 print "Sending $f...\n";
429 # makes a makefile template
433 # Makefile template created by $artemus_id
434 # for HTML projects (GNU Make)
436 # artemus (C) 2000/2002 Angel Ortega <angel\@triptico.com>
439 # make -- to build everything
440 # make file.html -- to build just file.html from file.artemus
441 # make clean -- to delete every rebuildable thing
442 # make ftp -- to upload using ftp
445 # Fill DEST with a list of the .html files created by artemus
446 # Use relative paths (e.g. misc/contact.html) if there are
447 # subdirectories. This paths will be the same when uploading;
448 # subdirs on ftp server must exist
450 DEST= index.html [MODIFY HERE]
452 # Add \$(DEST) to INET plus any other file/directory than will
453 # be uploaded but not built by this makefile.
454 # e.g.: download/* or images/*
456 INET=\$(DEST) images/* [MODIFY HERE]
458 # directories for the site map
465 # default rule. Artemus sources use the .artemus extension.
466 # Change if you use another
468 artemus -i \$< -o \$@
470 # site map. File sitemap/index.artemus must be included
471 # in DEST in order to be correctly remade.
473 artemus --site-map \$(DIRS) > sitemap/index.artemus
479 -rm \$(DEST) unresolved
481 # Last upload timestamp
485 # if any file is newer than the last upload,
486 # send it and recreate timestamp
498 # builds a site map from current directory to stdout
502 # special artemus token site-map-head
503 print "{-site-map-head}\n\n";
506 foreach my $dir (@dirs)
510 open F
, "find $dir -name \"*.html\" -follow|" or next;
516 push(@l,$_) unless /index.html$/;
523 # the (supposed) index.html is put the first
524 unshift(@l,"$dir/index.html");
530 my ($size,$file,$title);
539 if ($file =~ /<title>([^<]*)/i)
548 print "<li><a href='../$i'>$title</a> [$i] $size bytes\n";
549 print "<ul>\n" unless $cnt;
558 # special artemus token site-map-foot
559 print "{-site-map-foot}\n";