aboutsummaryrefslogtreecommitdiff
path: root/lib/WebCoso
diff options
context:
space:
mode:
authordakkar <dakkar@luxion>2008-01-03 20:05:51 +0000
committerdakkar <dakkar@luxion>2008-01-03 20:05:51 +0000
commit409b0fae3968de8b7ec3adfc4840ddad536757ff (patch)
tree182eb0b71053ff9d4bb665bd7fc0173cfe9672c2 /lib/WebCoso
parentMakeMaker! (diff)
downloadWebCoso-409b0fae3968de8b7ec3adfc4840ddad536757ff.tar.gz
WebCoso-409b0fae3968de8b7ec3adfc4840ddad536757ff.tar.bz2
WebCoso-409b0fae3968de8b7ec3adfc4840ddad536757ff.zip
refactoring: TT
git-svn-id: svn://luxion/repos/WebCoso/trunk@326 fcb26f47-9200-0410-b104-b98ab5b095f3
Diffstat (limited to 'lib/WebCoso')
-rw-r--r--lib/WebCoso/Common.pm46
-rw-r--r--lib/WebCoso/TT.pm63
2 files changed, 109 insertions, 0 deletions
diff --git a/lib/WebCoso/Common.pm b/lib/WebCoso/Common.pm
new file mode 100644
index 0000000..0680550
--- /dev/null
+++ b/lib/WebCoso/Common.pm
@@ -0,0 +1,46 @@
+package WebCoso::Common;
+
+our $SRCPATH='src';
+our $DSTPATH='dst';
+our $DSTBASEURL='/';
+
+sub langOf {
+ my ($name)=@_;
+ $name=~m{(^|/)document\.([^.]+)(\.|$)} and return $2;
+ return;
+}
+
+sub typeOf {
+ my ($name)=@_;
+ $name=~m{(^|/)document\.[^.]+\.([^.]+\.[^.]+)$} and return $2;
+ return;
+}
+
+sub typedAs {
+ my ($name,$newtype)=@_;
+ $name=~s{(^|/)(document\.[^.]+\.)([^.]+\.[^.]+)$}{$1$2$newtype};
+ return $name;
+}
+
+sub dstUriFor {
+ my ($name,$short)=(@_,1);
+ warn "dstUriFor($name,$short)\n";
+ if ($short) {
+ $name=~s{/[^/]+$}{/};
+ }
+ else {
+ $name=typedAs($name,'html');
+ }
+ $name=~s{^\Q$SRCPATH\E/}{$DSTBASEURL};
+ return $name;
+}
+
+sub isLang {
+ my ($lang,$name)=@_;
+ warn "isLang($lang,$name)\n";
+ return 1 if $name=~m{/$}; # assume that MultiViews on the server will work
+ return 1 if langOf($name) eq $lang;
+ return;
+}
+
+1;
diff --git a/lib/WebCoso/TT.pm b/lib/WebCoso/TT.pm
new file mode 100644
index 0000000..d04c209
--- /dev/null
+++ b/lib/WebCoso/TT.pm
@@ -0,0 +1,63 @@
+package WebCoso::TT;
+use WebCoso::Common;
+use Path::Class;
+use Template;
+
+sub new {
+ my ($class,%opts)=@_;
+
+ $opts{stash}||={
+ dstUriFor => \&WebCoso::Common::dstUriFor,
+ isLang => \&WebCoso::Common::isLang,
+ };
+
+ my $self={%opts};
+
+ $self->{template_provider}=Template::Provider->new({
+ INCLUDE_PATH=> $self->{TMPLPATH},
+ ABSOLUTE=>1,
+ RELATIVE=>1,
+ });
+ $self->{template}=Template->new({
+ LOAD_TEMPLATES=>[$self->{template_provider}],
+ });
+
+ $self->{fc}->add_parser(
+ qr{\.tt2?$} =>
+ sub {
+ $self->{template}->context->template($_[0]);
+ }); #$});
+
+ $self->{expander}=sub {
+ my ($maker,$target,$deps,$matches)=@_;
+
+ warn "expandTT($target,@$deps,@$matches)\n";
+
+ my $tmpl=$self->{fc}->get($deps->[-1]);
+ my $vars={ path=> $matches->[0],
+ language => $matches->[1],
+ %{$self->{stash}},
+ };
+ if (@$deps>1) {
+ warn "tagging as $deps->[0]\n";
+ $vars->{tagged}=$self->{fc}->get($deps->[0]);
+ }
+ push @{$self->{template_provider}->include_path},
+ file($deps->[-1])->parent;
+ $self->{fc}->put($target,
+ $self->{template}->context->process($tmpl,
+ $vars));
+ pop @{$self->{template_provider}->include_path};
+ };
+
+ bless $self,$class;
+}
+
+
+sub expandTT {
+ my ($self)=@_;
+
+ return $self->{expander}
+ }
+
+1;