Updated art6 ideas.
[artemus.git] / waste / artemus.src
blobc8a1aa0ff17d20454543acbd71b9f1314949a50b
1 #!/usr/bin/perl
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
25 use locale;
26 use Getopt::Long;
28 # id
29 $VERSION="3.0beta1";
30 $artemus_id="Artemus $VERSION";
32 # substitution variables
33 %vars=();
35 # substitution functions
36 %funcs=();
38 # source and destination files
39 $src="";
40 $dst="";
42 # config file
43 $config_file="artemus.conf";
45 # local configuration file
46 $local_config_file="local-artemus.conf";
48 # paragraph separator
49 $para_sep="";
51 # use cr/lf instead of lf
52 $use_cr_lf=0;
54 # append instead of overwrite output file
55 $append=0;
57 # send files using ftp
58 $use_ftp=0;
60 # build site map
61 $site_map=0;
63 # create makefile template
64 $makefile_template=0;
66 # quiet flag
67 $quiet=0;
69 # inverse substitutions
70 $inverse_config_file="artemus-inv.conf";
71 %inv_vars=();
73 # include path
74 $include_path="";
76 # include debugging info
77 $debug=0;
79 # list of unresolved symbols
80 @unresolved=();
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,
92                         "f|ftp"            => \$use_ftp,
93                         "k|makefile"       => \$makefile_template,
94                         "s|site-map"       => \$site_map,
95                         "q|quiet"          => \$quiet,
96                         "l|include-path=s" => \$include_path,
97                         "d|debug"          => \$debug,
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;
108 if(!$use_ftp)
110         usage() unless $src;
111         usage() unless $dst;
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'";
123 # get all the file
124 #undef $/;
125 $data=join("",<F>);
126 close F;
128 @artemus.pl@
130 if($dst =~ /.html$/)
132         $data="<meta name=\"generator\" content=\"$artemus_id\">\n".$data;
133         $data=sprintf("<!-- Date: %s -->\n",scalar(localtime)).$data;
134         $data="<!-- Built with $artemus_id Angel Ortega 2000/2002 -->\n".$data;
137 # special functions
138 $funcs{"filesize"}=sub { -s $_[0] };
139 $funcs{"shell"}=sub { $_=`$_[0]`; chop; return $_ };
141 $data=artemus($data, "paragraph-separator"      => $para_sep,
142                      "vars"                     => \%vars,
143                      "inv-vars"                 => \%inv_vars,
144                      "funcs"                    => \%funcs,
145                      "include-path"             => $include_path,
146                      "use-cr-lf"                => $use_cr_lf,
147                      "debug"                    => $debug,
148                      "unresolved"               => \@unresolved
149               );
151 # save file
153 open F, ($append?">":"").">$dst" or die "can't write '$dst'";
154 print F $data;
155 close F;
157 # dump errors
158 unless($quiet)
160         foreach my $e (@unresolved)
161         {
162                 print STDERR "Unresolved: '$e'\n";
163         }
167 exit(0);
169 # #####################################################################
171 sub read_config
173         my ($conf,$inverse)=@_;
174         local (*F);
176         # read config file
177         unless(open F, $conf)
178         {
179                 if($quiet)
180                 { return }
181                 else
182                 { die "'$conf' bad config file"; }
183         }
185         while(<F>)
186         {
187                 my ($key,$val);
189                 chomp($_);
191                 unless(/^#/ or /^$/)
192                 {
193                         ($key,$val)=split("=",$_,2);
195                         if($val =~ s/^\|//)
196                         {
197                                 $val=`$val`;
198                                 chop($val);
199                         }
200                         elsif($val eq "<<EOF")
201                         {
202                                 # 'document here' construction
203                                 $val="";
205                                 while(<F>)
206                                 {
207                                         last if /^EOF/;
209                                         $val.=$_;
210                                 }
211                         }
212                         elsif($key eq "\\INCLUDE")
213                         {
214                                 read_config($val);
215                                 next;
216                         }
218                         $vars{$key}=$val;
219                         $inv_vars{$key}=$val if($inverse);
220                 }
221         }
223         close F;
227 sub usage
229         print("$artemus_id - HTML (and other things) Preprocessor\n");
230         print("Copyright (C) 2000/2002 Angel Ortega <angel\@triptico.com>\n\n");
232         print("Usage:\n");
233         print("  artemus -i|--input={input file} -o|--output={output file}\n");
234         print("        [-c|--conf={config file}]\n");
235         print("        [-l|--include-path={path to includes}]\n");
236         print("        [-p|--paragraph={paragraph_separator}]\n");
237         print("        [-q|--quiet]\n");
238         print("        [-m|--msdos] [-a|--append] [-d|--debug]\n\n");
239         print("    or\n\n");
240         print("  artemus -f|--ftp {files to ftp...}\n\n");
241         print("    or\n\n");
242         print("  artemus -k|--makefile\n");
243         print("    or\n\n");
244         print("  artemus -s|--site-map\n");
246         exit(1);
250 sub version_only
252         print("$VERSION\n");
253         exit(0);
257 sub ftp_send
258 # send files using ftp
260         my ($ftp);
262         require Net::FTP;
264         if(scalar(@ARGV)==0)
265         {
266                 print "Nothing to send.\n";
267                 exit(0);
268         }
270         print "Connecting to $vars{'ftp.host'}...\n";
271         $ftp=Net::FTP->new($vars{'ftp.host'});
272         print "OK\n";
274         print "Logging in as $vars{'ftp.user'}...\n";
275         $ftp->login($vars{'ftp.user'},$vars{'ftp.passwd'})
276                 or die "ftp login error";
277         print "OK\n";
279         if(defined($vars{'ftp.dir'}))
280         {
281                 print "Chdir $vars{'ftp.dir'}...\n";
282                 $ftp->cwd($vars{'ftp.dir'});
283                 print "OK\n";
284         }
286         $ftp->binary();
288         foreach my $f (@ARGV)
289         {
290                 print "Sending $f...\n";
291                 $ftp->put($f,$f);
292                 print "OK\n";
293         }
295         print "Done.\n";
296         $ftp->quit;
298         exit(0);
302 sub make_makefile
303 # makes a makefile template
305         print <<"EOF";
307 # Makefile template created by $artemus_id
308 # for HTML projects (GNU Make)
310 # artemus (C) 2000/2002 Angel Ortega <angel\@triptico.com>
312 # Use:
313 #       make            -- to build everything
314 #       make file.html  -- to build just file.html from file.artemus
315 #       make clean      -- to delete every rebuildable thing
316 #       make ftp        -- to upload using ftp
319 # Fill DEST with a list of the .html files created by artemus
320 # Use relative paths (e.g. misc/contact.html) if there are
321 # subdirectories. This paths will be the same when uploading;
322 # subdirs on ftp server must exist
324 DEST=   index.html [MODIFY HERE]
326 # Add \$(DEST) to INET plus any other file/directory than will
327 # be uploaded but not built by this makefile.
328 # e.g.: download/* or images/*
330 INET=\$(DEST) images/* [MODIFY HERE]
332 # directories for the site map
333 DIRS= [MODIFY HERE]
335 # default target
336 .PHONY: all
337 all: \$(DEST)
339 # default rule. Artemus sources use the .artemus extension.
340 # Change if you use another
341 %.html: %.artemus
342         artemus -i \$< -o \$@
344 # site map. File sitemap/index.artemus must be included
345 # in DEST in order to be correctly remade.
346 map:
347         artemus --site-map \$(DIRS) > sitemap/index.artemus
348         \$(MAKE)
350 # The usual clean
351 .PHONY: clean
352 clean:
353         -rm \$(DEST) unresolved
355 # Last upload timestamp
356 .PHONY: ftp
357 ftp: ftpstamp
359 # if any file is newer than the last upload,
360 # send it and recreate timestamp
361 ftpstamp: \$(INET)
362         artemus --ftp \$?
363         touch ftpstamp
367         exit(0);
371 sub build_site_map
372 # builds a site map from current directory to stdout
374         my (@dirs)=@_;
376         # special artemus token site-map-head
377         print "{-site-map-head}\n\n";
378         print "<ul>\n";
380         foreach my $dir (@dirs)
381         {
382                 my ($cnt,@l);
384                 open F, "find $dir -name \"*.html\" -follow|" or next;
386                 while(<F>)
387                 {
388                         chop;
389                         s/^\.\///;
390                         push(@l,$_) unless /index.html$/;
391                 }
393                 close F;
395                 @l=sort(@l);
397                 # the (supposed) index.html is put the first
398                 unshift(@l,"$dir/index.html");
400                 # travel the list
401                 $cnt=0;
402                 foreach my $i (@l)
403                 {
404                         my ($size,$file,$title);
406                         $size=-s $i;
408                         # slurps all
409                         open F, $i or next;
410                         $file=join("",<F>);
411                         close F;
413                         if ($file =~ /<title>([^<]*)/i)
414                         {
415                                 $title=$1;
416                         }
417                         else
418                         {
419                                 $title=$i;
420                         }
422                         print "<li><a href='../$i'>$title</a> [$i] $size bytes\n";
423                         print "<ul>\n" unless $cnt;
425                         $cnt++;
426                 }
427                 print "</ul>\n";
428         }
430         print "</ul>\n";
432         # special artemus token site-map-foot
433         print "{-site-map-foot}\n";
435         exit(0);