Skip to content

Commit

Permalink
Item9075: separator=, like header, format, footer and pager all shoul…
Browse files Browse the repository at this point in the history
…d expand the FormatTokens

git-svn-id: http://svn.foswiki.org/trunk@7597 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
SvenDowideit authored and SvenDowideit committed May 30, 2010
1 parent c3e90ab commit aca9a0a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 27 deletions.
78 changes: 77 additions & 1 deletion UnitTestContrib/test/unit/Fn_FOREACH.pm
Expand Up @@ -30,6 +30,49 @@ sub test_separator {
$this->assert_str_equals( "OkATopic,OkBTopic,OkTopic", $result );
}

sub test_perl_newline_separator {
my $this = shift;

my $result =
$this->{test_topicObject}->expandMacros(
'%FOREACH{"OkATopic,OkBTopic,OkTopic" nonoise="on" format="$topic" separator="'."\n".'"}%'
);

$this->assert_str_equals( "OkATopic\nOkBTopic\nOkTopic", $result );
}
sub test_newline_separator {
my $this = shift;

my $result =
$this->{test_topicObject}->expandMacros(
'%FOREACH{"OkATopic,OkBTopic,OkTopic" nonoise="on" format="$topic" separator="
"}%'
);

$this->assert_str_equals( "OkATopic\nOkBTopic\nOkTopic", $result );
}
sub test_dollar_newline_separator {
my $this = shift;

my $result =
$this->{test_topicObject}->expandMacros(
'%FOREACH{"OkATopic,OkBTopic,OkTopic" nonoise="on" format="$topic" separator="$n"}%'
);

$this->assert_str_equals( "OkATopic\nOkBTopic\nOkTopic", $result );
}
#TODO: Sven isn't sure we use \n
sub test_backslash_escaped_newline_separator {
my $this = shift;

my $result =
$this->{test_topicObject}->expandMacros(
'%FOREACH{"OkATopic,OkBTopic,OkTopic" nonoise="on" format="$topic" separator="\n"}%'
);

$this->assert_str_equals( "OkATopic\\nOkBTopic\\nOkTopic", $result );
}

sub test_separator_with_header {
my $this = shift;

Expand Down Expand Up @@ -68,7 +111,7 @@ sub test_footer_with_ntopics_no_format {
$this->assert_str_equals( "Total found: 3", $result );
}

sub test_footer_with_ntopics_no_format_nonooise {
sub test_footer_with_ntopics_no_format_nonoise {
my $this = shift;

my $result = $this->{test_topicObject}->expandMacros(
Expand Down Expand Up @@ -528,4 +571,37 @@ sub test_not_topics {
'1:(A);2:(B);3:(C)', $result );
}

#%STARTINCLUDE%| =$n= or =$n()= | New line. Use =$n()= if followed by alphanumeric character, e.g. write =Foo$n()Bar= instead of =Foo$nBar= |
#| =$nop= or =$nop()= | Is a "no operation". This token gets removed; useful for nested search |
#| =$quot= | Double quote (="=) (\" also works) |
#| =$percent= | Percent sign (=%=) (=$percnt= also works) |
#| =$dollar= | Dollar sign (=$=) |
#| =$lt= | Less than sign (=<=) |
#| =$gt= | Greater than sign (=>=) |
#| =$amp= | Ampersand (=&=) |
#| =$comma= | Comma (=,=) |
#%STOPINCLUDE%
sub test_standard_escapes {
my $this = shift;

my $result =
$this->{test_topicObject}->expandMacros(
'%FOREACH{
"OkATopic,OkBTopic,OkTopic"
header="RESULT: $comma"
footer="$amp"
nonoise="on"
format="$topic"
separator="$quot"
}%'
);

$this->assert_str_equals(
"RESULT: ,
OkATopic\"OkBTopic\"OkTopic\"&", $result
);

}


1;
2 changes: 1 addition & 1 deletion core/data/System/VarSEARCH.txt
Expand Up @@ -32,7 +32,7 @@
| =multiple="on"= | Multiple hits per topic. Each hit can be [[FormattedSearch][formatted]]. The last token is used in case of a regular expression ";" _and_ search | Only one hit per topic |
| =nofinalnewline="on"= | If =on=, the search variable does not end in a line by itself. Any text continuing immediately after the SEARCH macro on the same line will be rendered as part of the table generated by the search, if appropriate. This feature is only active when format is defined. | =on= |
| =recurse="on"= | Recurse into subwebs, if subwebs are enabled. Note: recurse will currently search subwebs of explicitly excluded webs. =(web="all, -Sandbox" recurse="on")= will still search subwebs of =Sandbox=. This behavior is likely to change in a future release. | =off= |
| =separator=", "= | Line separator _between_ search hits (only used when format= is set) | ="$n"= (Newline) |
| =separator=", "= | Line separator _between_ search hits (only used when format= is set) uses FormatTokens | ="$n"= (Newline) |
| =newline="%<nop>BR%"= | Line separator _within_ a search hit. Useful if you want to put multi-line content into a table cell, for example if the format="" parameter contains a $pattern() that captures more than one line, or contains a $formfield() that returns a multi-line textfield. | ="$n"= (Newline) |
| =pagesize="25"= | number of items to show per page | ="25"= |
| =showpage="1"= | Page of items to show (starts at 1) (over-ridden by the value specified by the URL parameter hash from =$previousurl= and =$nexturl=) | ="1"= |
Expand Down
5 changes: 5 additions & 0 deletions core/lib/Foswiki/Macros/FOREACH.pm
Expand Up @@ -11,6 +11,11 @@ sub FOREACH {

my @list = split( /,\s*/, $params->{_DEFAULT} || '' );
my $s;

#TODO: this is a common default that should be extracted into a 'test, default and refine' parameters for all formatResult calls
if ( defined($params->{separator}) ) {
$params->{separator} = Foswiki::expandStandardEscapes($params->{separator});
}

# If the format string contains any of the topic-specific format specifiers
# then the list is treated as a list of topic names. Otherwise it is treated
Expand Down
10 changes: 10 additions & 0 deletions core/lib/Foswiki/Macros/SEARCH.pm
Expand Up @@ -14,6 +14,16 @@ sub SEARCH {
$params->{search} = $params->{_DEFAULT} if defined $params->{_DEFAULT};
$params->{type} = $this->{prefs}->getPreference('SEARCHVARDEFAULTTYPE')
unless ( $params->{type} );

#TODO: this is a common default that should be extracted into a 'test, default and refine' parameters for all formatResult calls
if ( defined($params->{separator}) ) {
$params->{separator} = Foswiki::expandStandardEscapes($params->{separator});
}
# newline feature replaces newlines within each search result
if ( defined($params->{newline}) ) {
$params->{newline} = Foswiki::expandStandardEscapes($params->{newline});
}

my $s;
try {
$s = $this->search->searchWeb(%$params);
Expand Down
27 changes: 2 additions & 25 deletions core/lib/Foswiki/Search.pm
Expand Up @@ -401,27 +401,6 @@ sub searchWeb {
&$callback( $cbdata, $tmplSearch );
}

my $mixedAlpha = $Foswiki::regex{mixedAlpha};

# separator defines what separates each search result
# excluding header and footer
# Replace $n and $n() with \n for separator
my $separator = $params{separator};
if ( defined($separator) ) {
$separator =~ s/\$n\(\)/\n/gos; # expand "$n()" to new line
$separator =~ s/\$n([^$mixedAlpha]|$)/\n$1/gos;
}
$params{separator} = $separator;

# newline feature replaces newlines within each search result
# Replace $n and $n() with \n for newLine
my $newLine = $params{newline} || '';
if ($newLine) {
$newLine =~ s/\$n\(\)/\n/gos; # expand "$n()" to new line
$newLine =~ s/\$n([^$mixedAlpha]|$)/\n$1/gos;
}
$params{newline} = $newLine;

# We now format the results.
# All the
my ( $numberOfResults, $web_searchResult ) =
Expand All @@ -434,8 +413,8 @@ sub searchWeb {
# Remove trailing separator or new line if nofinalnewline parameter is set
my $noFinalNewline = Foswiki::isTrue( $params{nofinalnewline}, 1 );
if ( $formatDefined && $noFinalNewline ) {
if ($separator) {
$separator = quotemeta($separator);
if ($params{separator}) {
my $separator = quotemeta($params{separator});
$searchResult =~ s/$separator$//s; # remove separator at end
}
else {
Expand Down Expand Up @@ -1192,9 +1171,7 @@ sub formatResult {
$out =~ s/([^\n])$/$1\n/s;
}
}

$out = Foswiki::expandStandardEscapes($out);

}

#see http://foswiki.org/Tasks/Item2371 - needs unit test exploration
Expand Down

0 comments on commit aca9a0a

Please sign in to comment.