Updated a6 ideas.
[artemus.git] / artemus
blob9d92ee5d0e4b19bd01c4a48f6aec6ec6c20cada3
1 #!/usr/bin/perl
4 # artemus - HTML (and other things) Preprocessor
6 # Copyright (C) 2000/2009 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 use Artemus;
30 # substitution variables
31 %vars = ();
33 # substitution functions
34 %funcs = ();
36 # source and destination files
37 $src = '-';
38 $dst = '>-';
40 # config file
41 $config_file = 'artemus.conf';
43 # local configuration file
44 $local_config_file = 'local-artemus.conf';
46 # paragraph separator
47 $para_sep = '';
49 # use cr/lf instead of lf
50 $use_cr_lf = 0;
52 # append instead of overwrite output file
53 $append = 0;
55 # send files using ftp
56 $use_ftp = 0;
58 # build site map
59 $site_map = 0;
61 # quiet flag
62 $quiet = 0;
64 # inverse substitutions
65 $inverse_config_file = 'artemus-inv.conf';
66 %inv_vars = ();
68 # include path
69 $include_path = '';
71 # include debugging info
72 $debug = 0;
74 # unresolved templates
75 @unresolved = ();
77 #####################################################################
79 # special utility functions
80 $funcs{'filesize'} = sub { -s $_[0] };
81 $funcs{'shell'} = sub { $_=`$_[0]`; chomp; return $_ };
83 # id
84 $VERSION = $Artemus::VERSION;
85 $artemus_id = "Artemus $VERSION";
87 usage() if (!GetOptions('i|input=s' => \$src,
88 'o|output=s' => \$dst,
89 'c|conf=s' => \$config_file,
90 'p|paragraph=s' => \$para_sep,
91 'm|msdos' => \$use_cr_lf,
92 'a|append' => \$append,
93 'v|version-only' => \$version_only,
94 'q|quiet' => \$quiet,
95 'l|include-path=s' => \$include_path,
96 'd|debug' => \$debug,
97 'h|help' => \$help) or $help);
99 $dst = '>-' if $dst eq '-';
101 if ($version_only) {
102 print("$VERSION\n");
103 exit 0;
106 # read the configuration files
107 if (-f $config_file) {
108 read_config($config_file);
111 if (-f $local_config_file) {
112 read_config($local_config_file);
115 if (-f $inverse_config_file) {
116 read_config($inverse_config_file, 1);
119 open F, $src or die "can't open '$src'";
121 # get all the file
122 $data = join('', <F>);
123 close F;
125 # create the Artemus handle
126 $ah = Artemus->new('paragraph-separator' => $para_sep,
127 'vars' => \%vars,
128 'inv-vars' => \%inv_vars,
129 'funcs' => \%funcs,
130 'include-path' => $include_path,
131 'use-cr-lf' => $use_cr_lf,
132 'unresolved' => \@unresolved,
133 'debug' => $debug
136 # do it
137 $data = $ah->process($data);
139 # save file
141 open F, ($append ? '>' : '') . ">$dst" or die "can't write '$dst'";
142 print F $data;
143 close F;
145 foreach my $t (@unresolved) {
146 print STDERR "Artemus: unresolved '$t'\n";
149 if ($debug) {
150 foreach my $c (@{$ah->{call_stack}}) {
151 my ($t, $level, $full_line, $ret) = @{$c};
153 $ret =~ s/\n/\\n/g;
154 $full_line =~ s/\n/\\n/g;
156 print STDERR (' ' x $level),
157 '{-', $full_line, '} -> ',
158 $ret,
159 "\n"
164 exit(0);
166 ######################################################################
168 sub read_config
170 my ($conf, $inverse) = @_;
171 local (*F);
173 # read config file
174 unless (open F, $conf) {
175 if($quiet) {
176 return;
178 else {
179 die "'$conf' bad config file";
183 while (<F>) {
184 my ($key, $val);
186 chomp;
188 unless (/^#/ or /^$/) {
189 ($key, $val) = split("=", $_, 2);
191 if ($val =~ s/^\|//) {
192 $val = `$val`;
193 chop($val);
195 elsif ($val eq ('<<' . 'EOF')) {
196 # 'document here' construction
197 $val = '';
199 while (<F>) {
200 last if /^EOF/;
202 $val .= $_;
205 elsif ($key eq "\\INCLUDE") {
206 read_config($val);
207 next;
210 $vars{$key} = $val;
212 if ($inverse) {
213 $inv_vars{$key} = $val;
218 close F;
222 sub usage
224 print("$artemus_id - HTML (and other things) Preprocessor\n");
225 print("Copyright (C) 2000/2009 Angel Ortega <angel\@triptico.com>\n\n");
227 print("Usage:\n");
228 print(" artemus -i|--input={input file} -o|--output={output file}\n");
229 print(" [-c|--conf={config file}]\n");
230 print(" [-l|--include-path={path to includes}]\n");
231 print(" [-p|--paragraph={paragraph_separator}]\n");
232 print(" [-q|--quiet]\n");
233 print(" [-m|--msdos] [-a|--append] [-d|--debug]\n\n");
235 exit(1);