]> mj.ucw.cz Git - temple.git/blobdiff - UCW/Temple.pm
The argument of UCW::Temple::start() is no longer mandatory
[temple.git] / UCW / Temple.pm
index 32453daf1daaab9a70db2ecb095ba17d6db81249..1a3dc971f10f5980da10ccbdea2458d3151db1f3 100644 (file)
@@ -8,10 +8,28 @@ use strict;
 use warnings;
 no strict 'vars';
 
+require Exporter;
+our $VERSION = 1.0;
+our @ISA = qw(Exporter);
+our @EXPORT = qw(out);
+our @EXPORT_OK = qw();
+
 use IO::File;
 
+our $arguments;
 our @cond;
 
+sub out {
+       $T::out_func->(@_);
+       return;
+}
+
+our %deps = ();
+
+sub add_depend($) {
+       $deps{$_[0]} = 1;
+}
+
 sub get_nested($$$) {
        my ($r,$left,$right) = @_;
        pos $$r = pos($$r)-1;
@@ -72,7 +90,8 @@ sub eval_func($$) {
                }
                # print "Cond stack: @cond\n";
        } else {
-               eval_if_ok("$f($a)");
+               my $res = eval_if_ok("$f $a");
+               out $res if defined $res;
        }
 }
 
@@ -81,21 +100,26 @@ sub parse_string($) {
        pos $t = 0;
        for(;;) {
                if ($t =~ /\G([^\@]+)/cgs) {
-                       print $1 if $cond[0] > 0;
+                       out $1 if $cond[0] > 0;
                } elsif ($t =~ /\G\@\s*\n/cgs) {
                        # @ at end of line is ignored and eats the end of line
                } elsif ($t =~ /\G\@#[^\n]*\n/cgs) {
                        # a comment, which is ignored
                } elsif ($t =~ /\G\@\@/cgs) {
-                       print "\@";
+                       out "\@" if $cond[0] > 0;
                } elsif ($t =~ /\G\@{/cgs) {
                        my $x = get_nested(\$t, "{", "}");
-                       print eval_if_ok($x);
+                       out eval_if_ok($x);
                } elsif ($t =~ /\G\@\[/cgs) {
                        my $x = get_nested(\$t, '\[', '\]');
                        $x =~ s/^\[//;
                        $x =~ s/\]$//;
                        eval_if_ok($x);
+               } elsif ($t =~ /\G\@\(/cgs) {
+                       my $x = get_nested(\$t, '\(', '\)');
+                       $x =~ s/^\(//;
+                       $x =~ s/\)$//;
+                       out (defined($arguments->{$x}) ? $arguments->{$x} : "");
                } elsif ($t =~ /\G\@(\w+)\(/cgs) {
                        my $func = $1;
                        my $args = get_nested(\$t, '(', ')');
@@ -103,7 +127,7 @@ sub parse_string($) {
                } elsif ($t =~ /\G\@(\w+)([^\n]*)\n/cgs) {
                        eval_func($1, "($2)");
                } elsif ($t =~ /\G\@(\$\w+)/cgs) {
-                       print eval_if_ok($1);
+                       out eval_if_ok($1);
                } elsif ($t =~ /\G(\@[^\n]*)/cgs) {
                        die "Unknown control sequence $1";
                } elsif ($t =~ /\G$/cgs) {
@@ -116,6 +140,7 @@ sub parse_string($) {
 
 sub parse_file($) {
        my ($name) = @_;
+       add_depend($name);
        my $fh = new IO::File $name;
        die "Unable to open $name: $!" unless defined $fh;
        my $text;
@@ -124,8 +149,9 @@ sub parse_file($) {
        parse_string($text);
 }
 
-sub start()
+sub start(;$)
 {
+       $arguments = $_[0];
        @cond = (1);
 }
 
@@ -134,15 +160,17 @@ sub finish()
        $#cond and die "Unterminated \@if (depth $#cond)";
 }
 
-sub process_file($) {
-       start();
-       parse_file($_[0]);
+sub process_file($;$) {
+       my ($name, $args) = @_;
+       start($args);
+       parse_file($name);
        finish();
 }
 
-sub process_string($) {
-       start();
-       parse_string($_[0]);
+sub process_string($;$) {
+       my ($string, $args) = @_;
+       start($args);
+       parse_string($string);
        finish();
 }
 
@@ -150,7 +178,10 @@ sub process_string($) {
 
 package T;
 
+import UCW::Temple;
+
 our $temp;
+our $out_func = sub { print @_; };
 
 sub include {
        my $fn = shift @_;
@@ -160,12 +191,15 @@ sub include {
                UCW::Temple::eval_if_ok("\$$v = \$temp");
        }
        UCW::Temple::parse_file($fn);
+       return;
 }
 
 sub load {
        my $f = shift @_;
        (!defined($f) || @_) and die "\@load requires only one argument";
+       UCW::Temple::add_depend($f);
        require $f;
+       return;
 }
 
 1;