Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
--- lib/MT/Builder.pm (4.23) +++ lib/MT/Builder.pm (4.32) @@ -69,12 +69,13 @@ # Other namespaces (like 'Foo') would require the colon. # Tag and attributes are case-insensitive. So you can write: # <mtfoo>...</MTFOO> - while ($text =~ m!(<\$?(MT:?)((?:<[^>]+?>|"(?:<[^>]+?>|.)*?"|'(?:<[^>]+?>|.)*?'|.)+?)[\$/]?>)!gis) { - my($whole_tag, $prefix, $tag) = ($1, $2, $3); + while ($text =~ m!(<\$?(MT:?)((?:<[^>]+?>|"(?:<[^>]+?>|.)*?"|'(?:<[^>]+?>|.)*?'|.)+?)([-]?)[\$/]?>)!gis) { + my($whole_tag, $prefix, $tag, $space_eater) = ($1, $2, $3, $4); ($tag, my($args)) = split /\s+/, $tag, 2; my $sec_start = pos $text; my $tag_start = $sec_start - length $whole_tag; _text_block($state, $pos, $tag_start) if $pos < $tag_start; + $state->{space_eater} = $space_eater; $args ||= ''; # Structure of a node: # tag name, attribute hashref, contained tokens, template text, @@ -256,6 +257,8 @@ sub _text_block { my $text = substr ${ $_[0]->{text} }, $_[1], $_[2] - $_[1]; if ((defined $text) && ($text ne '')) { + return if $_[0]->{space_eater} && ($text =~ m/^\s+$/s); + $text =~ s/^\s+//s if $_[0]->{space_eater}; my $rec = bless [ 'TEXT', $text, undef, undef, undef, $_[0]->{tokens}, $_[0]->{tmpl} ], NODE; # Avoids circular reference between NODE and TOKENS, MT::Template. weaken($rec->[5]); @@ -338,6 +341,8 @@ $uncompiled = $t->[3]; } my($h, $type) = $ctx->handler_for($t->[0]); + my $conditional = defined $type && $type == 2; + if ($h) { $timer->pause_partial if $timer; local($ctx->{__stash}{tag}) = $t->[0]; @@ -352,7 +357,7 @@ if (ref $args{$v} eq 'ARRAY') { foreach (@{$args{$v}}) { if (m/^\$([A-Za-z_](\w|\.)*)$/) { - $_ = $ctx->var($1); + local $_ = $ctx->var($1); } } } else { @@ -381,6 +386,13 @@ # Stores a reference to the ordered list of arguments, # just in case the handler wants them local $args{'@'} = \@args; + + my $vars = $ctx->{__stash}{vars}; + local $vars->{__cond_value__} = $vars->{__cond_value__} + if $conditional; + local $vars->{__cond_name__} = $vars->{__cond_name__} + if $conditional; + my $out = $h->($ctx, \%args, $cond); unless (defined $out) { @@ -395,11 +407,8 @@ } } - if ((defined $type) && ($type == 2)) { + if ($conditional) { # conditional; process result - my $vars = $ctx->{__stash}{vars}; - local $vars->{__value__} = delete $vars->{__cond_value__}; - local $vars->{__name__} = delete $vars->{__cond_name__}; $out = $out ? $ctx->slurp(\%args, $cond) : $ctx->else(\%args, $cond); delete $vars->{__cond_tag__}; return $build->error(MT->translate("Error in <mt[_1]> tag: [_2]", $t->[0], $ctx->errstr)) --- lib/MT/Template/ContextHandlers.pm (4.23) +++ lib/MT/Template/ContextHandlers.pm (4.32) @@ -3442,7 +3459,14 @@ local $ctx->{__stash}{tokens} = $value; local $args->{name} = undef; local $args->{var} = undef; - $ctx->var($_, $args->{$_}) for keys %{$args || {}}; + # Pass through SetVarTemplate arguments as variables + # so that they do not affect the global stash + my $vars = $ctx->{__stash}{vars} ||= {}; + my @names = keys %$args; + my @var_names; + push @var_names, lc $_ for @names; + local @{$vars}{@var_names}; + $vars->{lc($_)} = $args->{$_} for @names; $value = _hdlr_pass_tokens(@_) or return; } elsif (ref($value) eq 'ARRAY') { if ( defined $index ) { @@ -4580,6 +4661,11 @@ Used to include a template from another blog in the system. Use in conjunction with the module, widget or identifier attributes. +=item * local (optional) + +Forces an Include of a template that exists in the blog that is being +published. + =item * global (optional; default "0") Forces an Include of a globally defined template even if the @@ -4689,7 +4775,14 @@ $type = 'widget'; $tmpl_name =~ s/^Widget: ?//; } - my $blog_id = $arg->{blog_id} || $ctx->{__stash}{blog_id} || 0; + my $blog_id = defined($arg->{blog_id}) + ? $arg->{blog_id} + : ( $arg->{global} ) + ? 0 + : defined($ctx->{__stash}{blog_id}) + ? $ctx->{__stash}{blog_id} + : 0; + $blog_id = $ctx->stash('local_blog_id') if $arg->{local}; my $stash_id = 'template_' . $type . '::' . $blog_id . '::' . $tmpl_name; return $ctx->error(MT->translate("Recursion attempt on [_1]: [_2]", MT->translate($name), $tmpl_name)) if $include_stack{$stash_id}; @@ -4787,7 +4880,7 @@ my $cache_driver; if ($cache_enabled) { my $tmpl_mod = $tmpl->modified_on; - my $tmpl_ts = MT::Util::ts2epoch($blog, $tmpl_mod); + my $tmpl_ts = MT::Util::ts2epoch($tmpl->blog_id ? $tmpl->blog : undef, $tmpl_mod); if (($ttl == 0) || (time - $tmpl_ts < $ttl)) { $ttl = time - $tmpl_ts; } @@ -4881,13 +4974,15 @@ $tokens = $cref; } else { my $blog = $ctx->stash('blog'); - if ($blog->id != $blog_id) { + if ($blog && $blog->id != $blog_id) { $blog = MT::Blog->load($blog_id) or return $ctx->error(MT->translate( "Can't find blog for id '[_1]", $blog_id)); } - my @paths = ($file, map File::Spec->catfile($_, $file), - $blog->site_path, $blog->archive_path); + my @paths = ($file); + push @paths, map { File::Spec->catfile($_, $file) } + ($blog->site_path, $blog->archive_path) + if $blog; my $path; for my $p (@paths) { $path = $p, last if -e $p && -r _; @@ -5751,6 +5849,7 @@ ? $existing->[ $index ] : undef; } + $existing = '' unless defined $existing; if ($args->{prepend}) { $val = $val . $existing; @@ -19772,8 +20202,10 @@ require MT::Template; my $tmpl = MT::Template->load({ name => $tmpl_name, blog_id => $blog_id ? [ 0, $blog_id ] : 0, - type => 'widgetset' }) - or return $ctx->error(MT->translate("Specified WidgetSet not found.")); + type => 'widgetset' }, + { sort => 'blog_id', + direction => 'descend' }) + or return $ctx->error(MT->translate( "Specified WidgetSet '[_1]' not found.", $tmpl_name )); my $text = $tmpl->text; return $ctx->build($text) if $text; --- lib/MT/Template/Context.pm (4.23) +++ lib/MT/Template/Context.pm (4.32) @@ -252,6 +263,9 @@ if (ref $code eq 'HASH') { $code = $code->{code} ||= MT->handler_to_coderef($code->{handler}); } + elsif (defined $code and ! ref $code) { + $code = MT->handler_to_coderef($code); + } $str = $code->($str, $val, $ctx); } }
This paste will be private.
From the Design Piracy series on my blog: