Index: core/lib/Foswiki/Macros/EVAL.pm =================================================================== --- core/lib/Foswiki/Macros/EVAL.pm (revision 0) +++ core/lib/Foswiki/Macros/EVAL.pm (revision 0) @@ -0,0 +1,77 @@ +# See bottom of file for license and copyright information +package Foswiki; + +use strict; + +our $evalParser; + +sub EVAL { + my ( $this, $params, $topicObject ) = @_; + my $result; + my $expr = $params->{_DEFAULT}; + my $style = $params->{style} || ''; + + # Recursion block. + $this->{evaluatingEval} ||= {}; + + # Block after 5 levels. + if ( $this->{evaluatingEval}->{$expr} + && $this->{evaluatingEval}->{$expr} > 5 ) + { + delete $this->{evaluatingEval}->{$expr}; + return ''; + } + unless ($evalParser) { + require Foswiki::If::Parser; + $evalParser = new Foswiki::If::Parser(); + } + + $this->{evaluatingEval}->{$expr}++; + try { + my $node = $evalParser->parse($expr); + $result = $node->evaluate( tom => $topicObject, data => $topicObject ); + if ($style eq 'json') { + use JSON (); + $result = JSON::to_json($result, {allow_nonref => 1}); + } elsif ($style eq 'perl' || ref($result)) { + use Data::Dumper (); + local $Data::Dumper::Indent = 0; + local $Data::Dumper::Terse = 1; + $result = Data::Dumper->Dump([$result]); + } else { + $result = '' if (!defined $result); + } + + } + catch Foswiki::Infix::Error with { + my $e = shift; + $result = + $this->inlineAlert( 'alerts', 'generic', 'EVAL{', $params->stringify(), + '}:', $e->{-text} ); + } + finally { + delete $this->{evaluatingEval}->{$expr}; + }; + + return $result; +} + +1; +__DATA__ +Foswiki - The Free and Open Source Wiki, http://foswiki.org/ + +Copyright (C) 2010 Foswiki Contributors. Foswiki Contributors +are listed in the AUTHORS file in the root of this distribution. +NOTE: Please extend that file, not this notice. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. For +more details read LICENSE in the root of this distribution. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +As per the GPL, removal of this notice is prohibited. Index: core/lib/Foswiki.pm =================================================================== --- core/lib/Foswiki.pm (revision 6495) +++ core/lib/Foswiki.pm (working copy) @@ -184,6 +184,7 @@ }, ENCODE => undef, ENV => undef, + EVAL => undef, EXPAND => undef, FORMFIELD => undef, FOREACH => undef, Index: core/data/System/VarEVAL.txt =================================================================== --- core/data/System/VarEVAL.txt (revision 0) +++ core/data/System/VarEVAL.txt (revision 0) @@ -0,0 +1,14 @@ +%META:TOPICINFO{author="ProjectContributor" date="1266949232" format="1.1" version="1.2"}% +%META:TOPICPARENT{name="Macros"}% +#VarEVAL +---+++ EVAL -- displays meta-data + * Uses the query syntax described in [[System.QuerySearch][QuerySearch]] (and also used for [[System.VarIF][%IF]]) to get information about meta-data. + * Syntax: =%EVAL{ "expression" [ style="perl|json" ] }%= + * Examples: + * %EVAL{"form.name"}% gets the name of the form in the current topic + * %EVAL{"fields[name='Firstname'].value"}% gets the value of the 'Firstname' form field in the current topic + * %EVAL{"Firstname"}% gets the value of the 'Firstname' form field in the current topic (shorthand version) + * %EVAL{"'System.DocumentGraphics'/attachments[1].name"}% gets a list of all the names of attachments on the topic 'System.DocumentGraphics' +Plain strings (such as field values) are generated _without_ quotes. Where values are data structures (hashes or arrays), they will be returned as Perl code strings generated by running through CPAN:Data::Dumper. You can change this behaviour to generate valid perl code strings by giving the =style="perl"= parameter, and as JSON strings by passing =style="json"=. + + * Related: [[%IF{"'%INCLUDINGTOPIC%'='Macros'" then="#"}%VarMETA][META]] [[%IF{"'%INCLUDINGTOPIC%'='Macros'" then="#"}%QuerySearch][QuerySearch]] \ No newline at end of file