Permalink
Browse files

Item8873: expand macros in PREFERENCEs during topic creation. Arguabl…

…y should do this for FIELD as well; not sure, though.

git-svn-id: http://svn.foswiki.org/trunk@7516 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information...
CrawfordCurrie CrawfordCurrie
CrawfordCurrie authored and CrawfordCurrie committed May 23, 2010
1 parent f5d6c99 commit 3427fed4f66f6ade5dec0a3a7d39f12600a1f897
@@ -33,13 +33,21 @@ sub test_NOP {
$result = $topicObject->expandMacros("%NOP{%WEB{}%}%");
$this->assert_equals( $this->{test_web}, $result );
$result = $topicObject->expandNewTopic("%NOP%");
$topicObject->text("%NOP%");
$topicObject->expandNewTopic();
$result = $topicObject->text();
$this->assert_equals( '', $result );
$result = $topicObject->expandNewTopic("%GM%NOP%TIME%");
$topicObject->text("%GM%NOP%TIME%");
$topicObject->expandNewTopic();
$result = $topicObject->text();
$this->assert_equals( '%GMTIME%', $result );
$result = $topicObject->expandNewTopic("%NOP{ ignore me }%");
$topicObject->text("%NOP{ ignore me }%");
$result = $topicObject->expandNewTopic();
$result = $topicObject->text();
$this->assert_equals( '', $result );
# this *ought* to work, but by the definition of TML, it doesn't.
@@ -456,7 +456,7 @@ EVIL
# All meta should have found its way into text
$this->assert_equals($text, $topicObject->text()."\n");
$topicObject->expandMacros($topicObject->text());
$topicObject->expandNewTopic($topicObject->text());
$topicObject->expandNewTopic();
$topicObject->renderTML($topicObject->text());
$topicObject->renderFormForDisplay();
$text = $topicObject->text();
@@ -494,7 +494,7 @@ GOOD
$topicObject->save();
$this->assert_equals($gunk, $topicObject->text());
$topicObject->expandMacros($topicObject->text());
$topicObject->expandNewTopic($topicObject->text());
$topicObject->expandNewTopic();
$topicObject->renderTML($topicObject->text());
$topicObject->renderFormForDisplay();
}
@@ -83,7 +83,11 @@ Kill me
%USERINFO{format="$emails,$username,$wikiname,$wikiusername"}%
%ENDSECTION{name="fred" type="section"}%
END
my $result = $this->{test_topicObject}->expandNewTopic($text);
$this->{test_topicObject}->text($text);
$this->{test_topicObject}->put('PREFERENCE',
{name=>"BLAH", value=>"%WIKINAME%"});
$this->{test_topicObject}->expandNewTopic();
my $xpect = <<END;
scum
@@ -95,7 +99,8 @@ scum, $this->{users_web}.ScumBag, scumbag\@example.com
scumbag\@example.com,scum,ScumBag,$this->{users_web}.ScumBag
%ENDSECTION{name="fred" type="section"}%
END
$this->assert_str_equals( $xpect, $result );
$this->assert_str_equals( $xpect, $this->{test_topicObject}->text() );
$this->assert_str_equals( "ScumBag", $this->{test_topicObject}->get('PREFERENCE', 'BLAH')->{value} );
}
sub test_userExpansions {
View
@@ -2295,94 +2295,103 @@ sub parseSections {
=begin TML
---++ ObjectMethod expandMacrosOnTopicCreation ( $text, $topicObject ) -> $text
---++ ObjectMethod expandMacrosOnTopicCreation ( $topicObject )
* =$text= - text to expand
* =$topicObject= - the topic
Expand limited set of variables during topic creation. These are variables
expected in templates that must be statically expanded in new content.
Expand only that subset of Foswiki variables that are
expanded during topic creation, in the body text and
PREFERENCE meta only. The expansion is in-place inside
the topic object.
# SMELL: no plugin handler
=cut
sub expandMacrosOnTopicCreation {
my ( $this, $text, $topicObject ) = @_;
return '' unless defined $text;
# Chop out templateonly sections
my ( $ntext, $sections ) = parseSections($text);
if ( scalar(@$sections) ) {
# Note that if named templateonly sections overlap,
# the behaviour is undefined.
foreach my $s ( reverse @$sections ) {
if ( $s->{type} eq 'templateonly' ) {
$ntext =
substr( $ntext, 0, $s->{start} )
. substr( $ntext, $s->{end}, length($ntext) );
}
else {
# put back non-templateonly sections
my $start = $s->remove('start');
my $end = $s->remove('end');
$ntext =
substr( $ntext, 0, $start )
. '%STARTSECTION{'
. $s->stringify() . '}%'
. substr( $ntext, $start, $end - $start )
. '%ENDSECTION{'
. $s->stringify() . '}%'
. substr( $ntext, $end, length($ntext) );
}
}
$text = $ntext;
}
my ( $this, $topicObject ) = @_;
# Make sure func works, for registered tag handlers
$Foswiki::Plugins::SESSION = $this;
$text = _processMacros( $this, $text, \&_expandMacroOnTopicCreation,
$topicObject, 16 );
local $Foswiki::Plugins::SESSION = $this;
# expand all variables for type="expandvariables" sections
( $ntext, $sections ) = parseSections($text);
if ( scalar(@$sections) ) {
foreach my $s ( reverse @$sections ) {
if ( $s->{type} eq 'expandvariables' ) {
my $etext =
substr( $ntext, $s->{start}, $s->{end} - $s->{start} );
$this->innerExpandMacros( \$etext, $topicObject );
$ntext =
substr( $ntext, 0, $s->{start} )
. $etext
. substr( $ntext, $s->{end}, length($ntext) );
my $text = $topicObject->text();
if ($text) {
# Chop out templateonly sections
my ( $ntext, $sections ) = parseSections($text);
if ( scalar(@$sections) ) {
# Note that if named templateonly sections overlap,
# the behaviour is undefined.
foreach my $s ( reverse @$sections ) {
if ( $s->{type} eq 'templateonly' ) {
$ntext =
substr( $ntext, 0, $s->{start} )
. substr( $ntext, $s->{end}, length($ntext) );
}
else {
# put back non-templateonly sections
my $start = $s->remove('start');
my $end = $s->remove('end');
$ntext =
substr( $ntext, 0, $start )
. '%STARTSECTION{'
. $s->stringify() . '}%'
. substr( $ntext, $start, $end - $start )
. '%ENDSECTION{'
. $s->stringify() . '}%'
. substr( $ntext, $end, length($ntext) );
}
}
else {
# put back non-expandvariables sections
my $start = $s->remove('start');
my $end = $s->remove('end');
$ntext =
substr( $ntext, 0, $start )
. '%STARTSECTION{'
. $s->stringify() . '}%'
. substr( $ntext, $start, $end - $start )
. '%ENDSECTION{'
. $s->stringify() . '}%'
. substr( $ntext, $end, length($ntext) );
$text = $ntext;
}
$text = _processMacros( $this, $text, \&_expandMacroOnTopicCreation,
$topicObject, 16 );
# expand all variables for type="expandvariables" sections
( $ntext, $sections ) = parseSections($text);
if ( scalar(@$sections) ) {
foreach my $s ( reverse @$sections ) {
if ( $s->{type} eq 'expandvariables' ) {
my $etext =
substr( $ntext, $s->{start}, $s->{end} - $s->{start} );
$this->innerExpandMacros( \$etext, $topicObject );
$ntext =
substr( $ntext, 0, $s->{start} )
. $etext
. substr( $ntext, $s->{end}, length($ntext) );
}
else {
# put back non-expandvariables sections
my $start = $s->remove('start');
my $end = $s->remove('end');
$ntext =
substr( $ntext, 0, $start )
. '%STARTSECTION{'
. $s->stringify() . '}%'
. substr( $ntext, $start, $end - $start )
. '%ENDSECTION{'
. $s->stringify() . '}%'
. substr( $ntext, $end, length($ntext) );
}
}
$text = $ntext;
}
$text = $ntext;
# kill markers used to prevent variable expansion
$text =~ s/%NOP%//g;
$topicObject->text($text);
}
# kill markers used to prevent variable expansion
$text =~ s/%NOP%//g;
return $text;
# Expand preferences
my @prefs = $topicObject->find( 'PREFERENCE' );
foreach my $p (@prefs) {
$p->{value} = _processMacros(
$this, $p->{value}, \&_expandMacroOnTopicCreation,
$topicObject, 16 );
}
}
=begin TML
View
@@ -2110,9 +2110,11 @@ sub expandVariablesOnTopicCreation {
my $topicObject = Foswiki::Meta->new(
$Foswiki::Plugins::SESSION,
$Foswiki::Plugins::SESSION->{webName},
$Foswiki::Plugins::SESSION->{topicName}
$Foswiki::Plugins::SESSION->{topicName},
$_[0]
);
return $topicObject->expandNewTopic( $_[0] );
$topicObject->expandNewTopic();
return $topicObject->text();
}
=begin TML
View
@@ -2602,16 +2602,20 @@ sub moveAttachment {
=begin TML
---++ ObjectMethod expandNewTopic( $text ) -> $text
---++ ObjectMethod expandNewTopic()
Expand only that subset of Foswiki variables that are
expanded during topic creation. Returns the expanded text.
expanded during topic creation, in the body text and
PREFERENCE meta only.
The expansion is in-place in the object data.
Only valid on topics.
=cut
sub expandNewTopic {
my ( $this, $text ) = @_;
return $this->{_session}->expandMacrosOnTopicCreation( $text, $this );
my ( $this ) = @_;
$this->{_session}->expandMacrosOnTopicCreation( $this );
}
=begin TML
@@ -279,10 +279,13 @@ sub init_edit {
# Copy field values from the template
$topicObject->copyFrom( $ttom, 'FIELD' );
$topicObject->text( $topicObject->expandNewTopic( $ttom->text() ) );
# Copy preference values
$topicObject->copyFrom( $ttom, 'PREFERENCE' );
#SMELL: need to at least explain why we need to do this a second time.
$topicObject->text( $topicObject->expandNewTopic( $ttom->text() ) );
# Copy the text
$topicObject->text($ttom->text());
$topicObject->expandNewTopic();
}
$tmpl =~ s/%TEMPLATETOPIC%/$templateTopic/;
@@ -968,12 +968,12 @@ sub _writeRegistrationDetailsToTopic {
my $safe = $session->{user};
$session->{user} = $user;
try {
$text = $topicObject->expandNewTopic($text);
$topicObject->text($text);
$topicObject->expandNewTopic();
my $agent = $Foswiki::cfg{Register}{RegistrationAgentWikiName};
$session->{user} = $session->{users}->getCanonicalUserID($agent);
$topicObject->put( 'TOPICPARENT',
{ name => $Foswiki::cfg{UsersTopicName} } );
$topicObject->text($text);
unless ( $topicObject->haveAccess('CHANGE') ) {
throw Foswiki::AccessControlException( 'CHANGE', $agent,
@@ -121,7 +121,7 @@ sub buildNewTopic {
$text = $ttom->text();
$text = '' if $query->param('newtopic'); # created by edit
$text = $topicObject->expandNewTopic($text);
$topicObject->text($text);
foreach my $k ( keys %$ttom ) {
@@ -131,6 +131,9 @@ sub buildNewTopic {
}
}
$topicObject->expandNewTopic();
$text = $topicObject->text();
# topic creation, there is no original rev
$ancestorRev = 0;
}

0 comments on commit 3427fed

Please sign in to comment.