mark: A photo of Mark kneeling on top of the Taal Volcano in the Philippines. It was a long hike. (Default)
Mark Smith ([staff profile] mark) wrote in [site community profile] changelog2010-03-08 07:38 pm

[dw-free] Template Toolkit Plugin for DW stuff

[commit: http://hg.dwscoalition.org/dw-free/rev/6e52432d0978]

http://bugs.dwscoalition.org/show_bug.cgi?id=2318

Move Dreamwidth functions to a TT plugin, also does some cleanup to
DW::Template.

Patch by [personal profile] exor674.

Files modified:
  • cgi-bin/DW/Template.pm
  • cgi-bin/DW/Template/Filters.pm
  • cgi-bin/DW/Template/Plugin.pm
  • views/_init.tt
--------------------------------------------------------------------------------
diff -r ef99e212a4c3 -r 6e52432d0978 cgi-bin/DW/Template.pm
--- a/cgi-bin/DW/Template.pm	Mon Mar 08 11:51:34 2010 -0600
+++ b/cgi-bin/DW/Template.pm	Mon Mar 08 19:38:35 2010 +0000
@@ -30,7 +30,7 @@ DW::Template - Template Toolkit helpers 
 
 =cut 
 
-# setting this to 0 -- have to explicitly specify which plugins we want.
+# setting this to false -- have to explicitly specify which plugins we want.
 $Template::Plugins::PLUGIN_BASE = '';
 
 my $site_constants = Template::Namespace::Constants->new({
@@ -51,30 +51,30 @@ my $view_engine = Template->new({
         site => $site_constants,
         roots => $roots_constants,
     },
-    FILTERS => {
-        ml => [ \&ml, 1 ],
-    },
     CACHE_SIZE => $LJ::TEMPLATE_CACHE_SIZE, # this can be undef, and that means cache everything.
     STAT_TTL => $LJ::IS_DEV_SERVER ? 1 : 3600,
     PLUGINS => {
         autoformat => 'Template::Plugin::Autoformat',
         date => 'Template::Plugin::Date',
         url => 'Template::Plugin::URL',
+        dw => 'DW::Template::Plugin',
     },
+    PRE_PROCESS => '_init.tt',
 });
 
 =head1 API
 
-=head2 C<< $class->template_string( $filename, $opts ) >>
+=head2 C<< $class->template_string( $filename, $opts, $extra ) >>
 
 Render a template to a string.
 
 =cut
 
 sub template_string {
-    my ($class, $filename, $opts) = @_;
+    my ($class, $filename, $opts, $extra ) = @_;
     my $r = DW::Request->get;
 
+    $opts->{sections} = $extra;
     $r->note('ml_scope',"/$filename") unless $r->note('ml_scope');
 
     my $out;
@@ -107,13 +107,12 @@ sub cached_template_string {
     my ($class, $key, $filename, $subref, $opts, $extra ) = @_;
 
     $extra ||= {};
-    $opts->{sections} = $extra;
     return DW::FragmentCache->get( $key, {
         lock_failed => $opts->{lock_failed},
         expire => $opts->{expire},
         grace_period => $opts->{grace_period},
         render => sub {
-            return $class->template_string( $filename, $subref->( $_[0] ) );
+            return $class->template_string( $filename, $subref->( $_[0] ), $extra );
         }
     }, $extra);
 }
@@ -178,9 +177,7 @@ sub render_template {
     my ( $class, $filename, $opts, $extra ) = @_;
 
     $extra ||= {};
-    $opts->{sections} = $extra;
-
-    my $out = $class->template_string( $filename, $opts );
+    my $out = $class->template_string( $filename, $opts, $extra );
 
     return $class->render_string( $out, $extra );
 }
@@ -224,84 +221,6 @@ sub render_string {
     }
 }
 
-=head1 ML Stuff
-
-NOTE: All these methods use DW::Template::blah, not DW::Template->blah.
-
-=head2 C<< DW::Template::ml_scope( $scope ) >>
-
-Gets the scope or sets the scope to the given location
-
-=cut
-
-sub ml_scope {
-    return DW::Request->get->note('ml_scope', $_[0]);
-}
-
-=head2 C<< DW::Template::ml( $code, $vars ) >>
-
-=cut
-
-sub ml {
-    # save the last argument as the hashref, hopefully
-    my $args = $_[-1];
-    $args = {} unless $args && ref $args eq 'HASH';
-
-    # we have to return a sub here since we are a dynamic filter
-    return sub {
-        my ( $code ) = @_;
-
-        $code = DW::Request->get->note( 'ml_scope' ) . $code
-            if rindex( $code, '.', 0 ) == 0;
-
-        my $lang = decide_language();
-        return $code if $lang eq 'debug';
-        return LJ::Lang::get_text( $lang, $code, undef, $args );
-    };
-}
-
-sub decide_language {
-    my $r = DW::Request->get;
-    return $r->note( 'ml_lang' ) if $r->note( 'ml_lang' );
-    
-    my $lang = _decide_language();
-    
-    $r->note( 'ml_lang', $lang );
-    return $lang;
-}
-
-sub _decide_language
-{
-    my $r = DW::Request->get;
-
-    my $args = $r->get_args;
-    # GET param 'uselang' takes priority
-    my $uselang = $args->{'uselang'};
-    if ( $uselang eq "debug" || LJ::Lang::get_lang($uselang) ) {
-        return $uselang;
-    }
-
-    # next is their cookie preference
-    #FIXME: COOKIE!
-    #if ($BML::COOKIEIE{'langpref'} =~ m!^(\w{2,10})/(\d+)$!) {
-    #    if (exists $env->{"Langs-$1"}) {
-    #        # FIXME: Probably should actually do this!!!
-    #        # make sure the document says it was changed at least as new as when
-    #        # the user last set their current language, else their browser might
-    #        # show a cached (wrong language) version.
-    #        return $1;
-    #    }
-    #}
-
-    # FIXME: next is their browser's preference
-
-    # next is the default language
-    return $LJ::DEFAULT_LANG || $LJ::LANGS[0];
-
-    # lastly, english.
-    return "en";
-}
-
 =head1 AUTHOR
 
 =over
diff -r ef99e212a4c3 -r 6e52432d0978 cgi-bin/DW/Template/Filters.pm
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cgi-bin/DW/Template/Filters.pm	Mon Mar 08 19:38:35 2010 +0000
@@ -0,0 +1,79 @@
+#!/usr/bin/perl
+#
+# DW::Template::Filters
+#
+# Filters for the Dreamwidth Template Toolkit plugin
+#
+# Authors:
+#      Andrea Nall <anall@andreanall.com>
+#
+# Copyright (c) 2010 by Dreamwidth Studios, LLC.
+#
+# This program is free software; you may redistribute it and/or modify it under
+# the same terms as Perl itself.  For a copy of the license, please reference
+# 'perldoc perlartistic' or 'perldoc perlgpl'.
+#
+
+package DW::Template::Filters;
+use strict;
+
+# Seperated out of DW::Template::Plugin to avoid accidental use as a method.
+sub ml {
+    # save the last argument as the hashref, hopefully
+    my $args = $_[-1];
+    $args = {} unless $args && ref $args eq 'HASH';
+
+    # we have to return a sub here since we are a dynamic filter
+    return sub {
+        my ( $code ) = @_;
+
+        $code = DW::Request->get->note( 'ml_scope' ) . $code
+            if rindex( $code, '.', 0 ) == 0;
+
+        my $lang = decide_language();
+        return $code if $lang eq 'debug';
+        return LJ::Lang::get_text( $lang, $code, undef, $args );
+    };
+}
+
+sub decide_language {
+    my $r = DW::Request->get;
+    return $r->note( 'ml_lang' ) if $r->note( 'ml_lang' );
+    
+    my $lang = _decide_language();
+    
+    $r->note( ml_lang => $lang );
+    return $lang;
+}
+
+sub _decide_language {
+    my $r = DW::Request->get;
+    my $args = $r->get_args;
+
+    # GET param 'uselang' takes priority
+    my $uselang = $args->{uselang};
+    return $uselang
+        if $uselang eq 'debug' || LJ::Lang::get_lang( $uselang );
+
+    # next is their cookie preference
+    #FIXME: COOKIE!
+    #if ($BML::COOKIEIE{'langpref'} =~ m!^(\w{2,10})/(\d+)$!) {
+    #    if (exists $env->{"Langs-$1"}) {
+    #        # FIXME: Probably should actually do this!!!
+    #        # make sure the document says it was changed at least as new as when
+    #        # the user last set their current language, else their browser might
+    #        # show a cached (wrong language) version.
+    #        return $1;
+    #    }
+    #}
+
+    # FIXME: next is their browser's preference
+
+    # next is the default language
+    return $LJ::DEFAULT_LANG || $LJ::LANGS[0];
+
+    # lastly, english.
+    return 'en';
+}
+
+1;
diff -r ef99e212a4c3 -r 6e52432d0978 cgi-bin/DW/Template/Plugin.pm
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cgi-bin/DW/Template/Plugin.pm	Mon Mar 08 19:38:35 2010 +0000
@@ -0,0 +1,101 @@
+#!/usr/bin/perl
+#
+# DW::Template::Plugin
+#
+# Template Toolkit plugin for Dreamwidth
+#
+# Authors:
+#      Andrea Nall <anall@andreanall.com>
+#
+# Copyright (c) 2010 by Dreamwidth Studios, LLC.
+#
+# This program is free software; you may redistribute it and/or modify it under
+# the same terms as Perl itself.  For a copy of the license, please reference
+# 'perldoc perlartistic' or 'perldoc perlgpl'.
+#
+package DW::Template::Plugin;
+use base 'Template::Plugin';
+use strict;
+
+use DW::Template::Filters;
+
+=head1 NAME
+
+DW::Template::Plugin - Template Toolkit plugin for Dreamwidth
+
+=head1 SYNOPSIS
+
+=cut
+
+sub load {
+    return $_[0];
+}
+
+sub new {
+    my ( $class, $context, @params ) = @_;
+
+    my $self = bless {
+        _CONTEXT => $context,
+    }, $class;
+
+    $context->define_filter( 'ml', [ \&DW::Template::Filters::ml, 1 ] );
+
+    return $self;
+}
+
+=head1 METHODS
+
+=head2 need_res
+
+Render a template to a string.
+
+    [% dw.need_res( 'stc/some.css' ); %]
+
+=cut
+
+sub need_res {
+    my $self = shift;
+    return LJ::need_res( @_ );
+}
+
+=head2 ml_scope
+
+Set the ML scope of the template
+
+    [% dw.ml_scope( '/foo.tt' ) %]
+
+=cut
+
+sub ml_scope {
+    return DW::Request->get->note( 'ml_scope', $_[1] );
+}
+
+=head1 FILTERS
+
+=head2 ml
+
+Apply a ML string.
+
+    [% '.foo' | ml(arg = 'bar') %]
+
+=cut
+
+=head1 AUTHOR
+
+=over
+
+=item Andrea Nall <anall@andreanall.com>
+
+=back
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (c) 2010 by Dreamwidth Studios, LLC.
+
+This program is free software; you may redistribute it and/or modify it under
+the same terms as Perl itself. For a copy of the license, please reference
+'perldoc perlartistic' or 'perldoc perlgpl'.
+
+=cut
+
+1;
\ No newline at end of file
diff -r ef99e212a4c3 -r 6e52432d0978 views/_init.tt
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/views/_init.tt	Mon Mar 08 19:38:35 2010 +0000
@@ -0,0 +1,18 @@
+[%# _init.tt
+
+Preprocess template.
+
+NOTE: This template should *never* output anything.
+This template is ran for every page, and just contains things that every
+page *needs* to do.
+
+Authors:
+     Andrea Nall <anall@andreanall.com>
+
+Copyright (c) 2010 by Dreamwidth Studios, LLC.
+
+This program is free software; you may redistribute it and/or modify it under
+the same terms as Perl itself.  For a copy of the license, please reference
+'perldoc perlartistic' or 'perldoc perlgpl'.
+
+%][%- USE dw -%]
--------------------------------------------------------------------------------