r4 - 20 Dec 2003 - 01:57:18 - SvenDowideitYou are here: TWiki >  Codev Web > ExtractNameValuePairsNeedsExpandValueSwitch
Tags:
, create new tag

Proposal: Allow expansion of %Twiki tags% inside extractNameValuePair

Use Cases

End user usecase:
  • %SEARCH{".*" web="Codev" regex="on" nosearch="on" order="modified" reverse="on" limit="\%URLPARAM\{'limit' default='5'}\%" }
(NB, this example doesn't need it, but relies on the user knowing there is a dependency between URLPARAM being expanded before search. This patch eliminates this requirement for end users)

API Use Case

  • my $attrLimit         = extractNameValuePair( $attributes, "limit",  1);
  • my $attrLimit         = extractNameValuePair( -attrs => $attributes, -key => "limit", -expandtags => 1);

Implementation

Implementation was relatively trivial, but enhances TWiki semantics quite dramatically. Default would be to NOT expand tags.

Named parameters version not yet written. (The whole of extractNameValuePair needs a revamp) Patch that allows this to work:

--- TWiki.pm    6 Dec 2003 14:41:44 -0000       1.15.2.6
+++ TWiki.pm    24 Nov 2003 23:40:25 -0000      1.11.2.7
@@ -1262,14 +1288,21 @@
     $htext =~ s/([\s\(])($abbrevRegex)/$1<nop>$2/g;
     $htext =~ s/@([a-zA-Z0-9\-\_\.]+)/@<nop>$1/g;      # email address

     return $htext;
 }

 # =========================
 sub extractNameValuePair
 {
-    my( $str, $name ) = @_;
-
+    my $str = shift;
+    my $name = shift;
+    my $expandVars = shift || 0;
     my $value = "";
     return $value unless( $str );
     $str =~ s/\\\"/\\$TranslationToken/g;  # escape \"
@@ -1279,22 +1312,29 @@
         if( $str =~ /(^|[^\S])$name\s*=\s*\"([^\"]*)\"/ ) {
             $value = $2 if defined $2;  # distinguish between "" and "0"
         }
-
     } else {
         # test if format: { "value" ... }
         if( $str =~ /(^|\=\s*\"[^\"]*\")\s*\"([^\"]*)\"/ ) {
             # is: %VAR{ ... = "..." "value" ... }%
             $value = $2 if defined $2;  # distinguish between "" and "0";
-
         } elsif( ( $str =~ /^\s*\w+\s*=\s*\"([^\"]*)/ ) && ( $1 ) ) {
             # is: %VAR{ name = "value" }%
             # do nothing, is not a standalone var
-
         } else {
             # format is: %VAR{ value }%
             $value = $str;
         }
     }
+
+    $value =~ s/\\//g unless (($value =~ /\$pattern/) || ($name eq "pattern")); # Unescape any special chars in the format
+    $value =~ s/'/"/g; # Convert any embedded 's to "s - this allows INCLUDEs in formats
+    if ($expandVars) {
+       &handleInternalTagsTextOnly($value);
+       &handleInternalTagsTopicAndWeb($value, $topicName, $webName);
+       $value =~ s/%INCLUDE{(.*?)}%/&handleIncludeFile($1, $topicName, $webName, "", ($topicName) )/ge;
+    }
+    $value =~ s/\\//g unless (($value =~ /\$pattern/) || ($name eq "pattern")); # Unescape any special chars in the format
+
     $value =~ s/\\$TranslationToken/\"/go;  # resolve \"
     return $value;
 }

An example that relies on this functionality is this:

%SEARCH{".*" web="%INCLUDINGWEB%" regex="on" nosearch="on" order="modified" reverse="on" limit="%URLPARAM\{'limit' default='20'}\%"}%

NB:

  • this search also relies upon the backslash patch in named include sections . (This is included in the above patch)
  • I haven't tested this patch file against TWiki. (I've chopped the complete chunk out of significantly larger patchfile, but often this works.)
    Also, this part would need changing...
    +       &handleInternalTagsTextOnly($value);
    +       &handleInternalTagsTopicAndWeb($value, $topicName, $webName);
    
Whilst ugly and not as good as a rewrite, I've found this makes TWiki significantly more flexible.

(Whilst you can argue this isn't necessary due to %FOO% being evaluated before %BAR%, this approach - effectively of subrequests - eliminates ordering dependencies)

-- MS - 28 Aug 2003, 19 Dec 2003

Edit | WYSIWYG | Attach | Printable | Raw View | Backlinks: Web, All Webs | History: r4 < r3 < r2 < r1 | More topic actions
 
Powered by TWiki
This site is powered by the TWiki collaboration platformCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback SourceForge.net Logo