[Rt-commit] r11443 - in rtfm/branches/2.3-EXPERIMENTAL: . html/Callbacks/RTFM/Elements/MessageBox lib/RT/FM

falcone at bestpractical.com falcone at bestpractical.com
Thu Apr 3 17:36:31 EDT 2008


Author: falcone
Date: Thu Apr  3 17:36:31 2008
New Revision: 11443

Modified:
   rtfm/branches/2.3-EXPERIMENTAL/   (props changed)
   rtfm/branches/2.3-EXPERIMENTAL/html/Callbacks/RTFM/Elements/MessageBox/Default
   rtfm/branches/2.3-EXPERIMENTAL/html/RTFM/Article/Elements/Preformatted
   rtfm/branches/2.3-EXPERIMENTAL/lib/RT/FM/Article_Overlay.pm

Log:
 r30908 at ketch:  falcone | 2008-04-03 16:00:03 -0400
 * merge template parsing code from 2.2


Modified: rtfm/branches/2.3-EXPERIMENTAL/html/Callbacks/RTFM/Elements/MessageBox/Default
==============================================================================
--- rtfm/branches/2.3-EXPERIMENTAL/html/Callbacks/RTFM/Elements/MessageBox/Default	(original)
+++ rtfm/branches/2.3-EXPERIMENTAL/html/Callbacks/RTFM/Elements/MessageBox/Default	Thu Apr  3 17:36:31 2008
@@ -45,27 +45,28 @@
 %# those contributions and any derivatives thereof.
 %# 
 %# END BPS TAGGED BLOCK }}}
-%foreach my $arg (keys %ARGS) {
-% if ($arg eq 'RTFM-Include-Article' && $ARGS{$arg} ) {
-%    my $art = RT::FM::Article->new($session{'CurrentUser'});
-%    $art->Load($ARGS{$arg});
-<& /RTFM/Article/Elements/Preformatted, Article => $art &>\
-% }
-%
-% if ($arg =~ /^RTFM-Include-Article-(\d+)$/) {
-%    my $art = RT::FM::Article->new($session{'CurrentUser'});
-%    $art->Load($1);
-<& /RTFM/Article/Elements/Preformatted, Article => $art &>\
-% }
-% if ($arg =~ /RTFM-Include-Article-Named/ && $ARGS{$arg} =~ /\D/) {
-%    my $art = RT::FM::Article->new($session{'CurrentUser'});
-%    $art->LoadByCols(Name => $ARGS{$arg});
-<& /RTFM/Article/Elements/Preformatted, Article => $art &>\
-% } elsif ($arg =~ /RTFM-Include-Article-Named/ && $ARGS{$arg}) {
-%    my $art = RT::FM::Article->new($session{'CurrentUser'});     
-%    $art->LoadByCols(id => $ARGS{$arg});
-<& /RTFM/Article/Elements/Preformatted, Article => $art &>\
-% }
-% }
-% return(1);
+<%INIT>
+foreach my $arg ( keys %ARGS ) {
+    my $article;
+    if ($arg eq 'RTFM-Include-Article' && $ARGS{$arg} ) {
+        $article = RT::FM::Article->new($session{'CurrentUser'});
+        $article->Load($ARGS{$arg});
+    } elsif ( $arg =~ /^RTFM-Include-Article-(\d+)$/ ) {
+        $article = RT::FM::Article->new($session{'CurrentUser'});
+        $article->Load($1);
+    } elsif ( $arg =~ /^RTFM-Include-Article-Named/ && $ARGS{ $arg } ) {
+        $article = RT::FM::Article->new($session{'CurrentUser'});
+        if ( $ARGS{$arg} =~ /\D/ ) {
+            $article->LoadByCols( Name => $ARGS{ $arg } );
+        } else {
+            $article->LoadByCols( id => $ARGS{ $arg } );
+        }
+    }
+    next unless $article && $article->id;
 
+    $m->comp('/RTFM/Article/Elements/Preformatted',
+        Article => $article,
+    );
+}
+return;
+</%INIT>

Modified: rtfm/branches/2.3-EXPERIMENTAL/html/RTFM/Article/Elements/Preformatted
==============================================================================
--- rtfm/branches/2.3-EXPERIMENTAL/html/RTFM/Article/Elements/Preformatted	(original)
+++ rtfm/branches/2.3-EXPERIMENTAL/html/RTFM/Article/Elements/Preformatted	Thu Apr  3 17:36:31 2008
@@ -50,47 +50,34 @@
 <%'-' x length("#".$Article->Id.": ".($Article->Name || loc('(no name)'))) %>
 % }
 % if ($Article->Summary =~ /\S/ and $include{Summary}) {
-<% $Article->Summary%>
+<% $Article->Summary %>
 % }
 % while (my $cf = $cfs->Next) {
 %   next unless $include{"CF-Title-".$cf->Id} or $include{"CF-Value-".$cf->Id};
 %   my $values = $Article->CustomFieldValues($cf->Id);
-
 %   if ($values->Count == 1) {
 %     my $value = $values->First; 
-%     my $content = $value ? $value->Content : "";
-%     if ($content =~ /<(.{1,5})>/) {
-%       $content = de_htmlify($content);
-%     }
 %     if ($include{"CF-Title-".$cf->Id}) {
 <%      $cf->Name%>:
 <%      '-' x length($cf->Name) %>
 %     }
-%     if ($include{"CF-Value-".$cf->Id}) {
-<%      $content%>
+%     if ($value && $include{"CF-Value-".$cf->Id}) {
+<%      $get_content->( $value ) %>
 %     }
 %   } else {
 %     my $val = $values->Next;
-%     my $content = $val ? $val->Content : "";
-%     if ($content =~ /<(.{1,5})>/) {
-%       $content = de_htmlify($content);
-%     }
 %     if ($include{"CF-Title-".$cf->Id}) {
 <%      $cf->Name%>: \
 %     }
-%     if ($include{"CF-Value-".$cf->Id}) {
-<%      $val && $content%>
+%     if ($val && $include{"CF-Value-".$cf->Id}) {
+<%      $get_content->( $val ) %>
 %     }
 %     while ($val = $values->Next) { 
-%       my $content = $val->Content;
-%       if ($content =~ /<(.{1,5})>/) {
-%         $content = de_htmlify($content);
-%       }
 %       if ($include{"CF-Title-".$cf->Id}) {
 <%        ' ' x length($cf->Name)%>  \
 %       }
-%       if ($include{"CF-Title-".$cf->Id}) {
-<%        $content%>
+%       if ($include{"CF-Value-".$cf->Id}) {
+<%        $get_content->( $val ) %>
 %       }
 %     } 
 %   }
@@ -102,7 +89,7 @@
 $include{"CF-Title-".$_->Id} = $include{"CF-Value-".$_->Id} = 1 while $_ = $cfs->Next;
 $include{$_} = not $class->FirstAttribute("Skip-$_") for keys %include;
 
-sub de_htmlify {
+my $de_htmlify = sub {
     my $content = shift;
     require HTML::TreeBuilder;
     my $tree = HTML::TreeBuilder->new;
@@ -113,8 +100,32 @@
     my $formatter = HTML::FormatText->new(leftmargin => 0, rightmargin => 50);
     $content = $formatter->format($tree);
     return $content;
-}
+};
+
+my $get_content = sub {
+    my $value = shift;
+    return '' unless $value;
+
+    my $content = $value->Content;
+    return '' unless defined $content && length $content;
+
+    if ( $RT::RTFM_ParseContentAsTemplate ) {
+        my ($new, $msg) = $Article->ParseTemplate( $content, Ticket => $Ticket );
+        unless ( defined $new && length $new ) {
+            $RT::Logger->error("Couldn't parse article's content: $msg");
+        } else {
+            $content = $new;
+        }
+    }
+
+    if ( $content =~ /<.{1,5}>/ ) {
+        $content = $de_htmlify->( $content );
+    }
+    return $content;
+};
+
 </%init>
 <%args>
 $Article
-</%args>
\ No newline at end of file
+$Ticket => undef
+</%args>

Modified: rtfm/branches/2.3-EXPERIMENTAL/lib/RT/FM/Article_Overlay.pm
==============================================================================
--- rtfm/branches/2.3-EXPERIMENTAL/lib/RT/FM/Article_Overlay.pm	(original)
+++ rtfm/branches/2.3-EXPERIMENTAL/lib/RT/FM/Article_Overlay.pm	Thu Apr  3 17:36:31 2008
@@ -506,6 +506,64 @@
     }
 }
 
+=head2 ParseTemplate $CONTENT, %TEMPLATE_ARGS
+
+Parses $CONTENT string as a template (L<Text::Template>).
+$Article and other arguments from %TEMPLATE_ARGS are
+available in code of the template as perl variables.
+
+=cut
+
+sub ParseTemplate {
+    my $self = shift;
+    my $content = shift;
+    my %args = (
+        Ticket => undef,
+        @_
+    );
+
+    return ($content) unless defined $content && length $content;
+
+    $args{'Article'} = $self;
+    $args{'rtname'}  = $RT::rtname;
+    if ( $args{'Ticket'} ) {
+        my $t = $args{'Ticket'}; # avoid memory leak
+        $args{'loc'} = sub { $t->loc(@_) };
+    } else {
+        $args{'loc'} = sub { $self->loc(@_) };
+    }
+
+    foreach my $key ( keys %args ) {
+        next unless ref $args{ $key };
+        next if ref $args{ $key } =~ /^(ARRAY|HASH|SCALAR|CODE)$/;
+        my $val = $args{ $key };
+        $args{ $key } = \$val;
+    }
+
+    # We need to untaint the content of the template, since we'll be working
+    # with it
+    $content =~ s/^(.*)$/$1/;
+    my $template = Text::Template->new(
+        TYPE   => 'STRING',
+        SOURCE => $content
+    );
+
+    my $is_broken = 0;
+    my $retval = $template->fill_in(
+        HASH => \%args,
+        BROKEN => sub {
+            my (%args) = @_;
+            $RT::Logger->error("Article parsing error: $args{error}")
+                unless $args{error} =~ /^Died at /; # ignore intentional die()
+            $is_broken++;
+            return undef;
+        },
+    );
+    return ( undef, $self->loc('Article parsing error') ) if $is_broken;
+
+    return ($retval);
+}
+
 =head2 CurrentUserHasRight
 
 Returns true if the current user has the right for this article, for the whole system or for this article's class


More information about the Rt-commit mailing list