Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added get-args method to Flower, and updated modifiers to use it.

  • Loading branch information...
commit e6899d3e70299e499d643b05cafa685da61db407 1 parent a6a9bba
@supernovus authored
View
10 README
@@ -91,11 +91,11 @@ Sets marked with * have been partially implemented and are included with
the current version of Flower. Sets marked with ** are not yet started, and
therefore aren't included with Flower.
-The interfaces to the modifiers included with Flower may not be compatible
-with the ones from Petal. For instance in Petal::Utils, you could call
-<div tal:replace="substr: $string 2 5 1">
-whereas with Flower::Utils::Text you would use:
-<div tal:replace="substr: 2,5,1 string">
+The syntax for the Flower::Utils methods is based on the modifiers from
+Petal::Utils, but extended to use the Flower-specific get-opts method, includes
+non whitespace, strings wrapped in 'quotes' and nested queries wrapped in
+((special markers)). The get-opts method is also used when passing parameters
+to object method calls in queries themselves.
Full documentation for the usage of Flower and the Flower::Utils modifiers
will be included in the docs/ folder in an upcoming release, until then
View
17 lib/Flower.pm
@@ -330,6 +330,21 @@ method process-query($data is copy, :$forcexml, :$noxml, :$noescape, :$bool) {
return $data;
}
+## get-args now supports parameters in the form of ((param name)) for
+## when you have queries with spaces in them that shouldn't be treated
+## as strings, like 'a string' does.
+method get-args($string, *@defaults) {
+ my @result = $string.comb(/ [ \'.*?\' | '(('.*?'))' | \S+ ] /);
+ @result>>.=subst(/^'(('/, '');
+ @result>>.=subst(/'))'$/, '');
+ my $results = @result.elems - 1;
+ my $defs = @defaults.elems;
+ if $results < $defs {
+ @result.push: @defaults[$results..$defs-1];
+ }
+ return @result;
+}
+
## This handles the lookups for query().
method !lookup (@paths is copy, $data) {
my $path = @paths.shift;
@@ -346,7 +361,7 @@ method !lookup (@paths is copy, $data) {
}
}
default {
- my ($command, *@args) = $path.comb(/ [ \'.*?\' | \S+ ] /); #split(/\s+/);
+ my ($command, *@args) = self.get-args($path);
if .can($command) {
## Let's query those arguments.
for @args -> $arg is rw {
View
2  lib/Flower/Utils/List.pm
@@ -17,7 +17,7 @@ our sub array_sort ($parent, $query, *%opts) {
}
our sub array_group ($parent, $query, *%opts) {
- my ($num, $subquery) = $query.split(/\s+/, 2);
+ my ($subquery, $num) = $parent.get-args($query, 1);
my $array = $parent.query($subquery);
if $array ~~ Array {
my @nest = ([]);
View
20 lib/Flower/Utils/Text.pm
@@ -43,29 +43,27 @@ our sub uc_first ($parent, $query, *%opts) {
## E.g.: <div tal:content="substr: 3,5 'theendoftheworld'"/>
## Returns: <div>endof</div>
our sub sub_string ($parent, $query, *%opts) {
- my ($params, $subquery) = $query.split(/\s+/, 2);
- my @params = $params.split(',');
+ my ($subquery, $start, $chars, $ellipsis) =
+ $parent.get-args($query, 0, Nil, Nil);
my $text = $parent.query($subquery);
if defined $text {
- my $substr = $text.substr(|@params[0..1]);
- if @params.elems gt 2 && @params[2] {
+ my $substr = $text.substr($start, $chars);
+ if $ellipsis {
$substr ~= '...';
}
return $parent.process-query($substr, |%opts);
}
}
-## Usage: printf: 'format' varname/path
-## The format needs to be surrounded by ' ' marks.
+## Usage: printf: format varname/path
## E.g.: <div tal:content="printf: '$%0.2f' '2.5'"/>
## Returns: <div>$2.50</div>
our sub print_formatted ($parent, $query, *%opts) {
- my regex stringparam { ^ \'(.*?)\' \s+ }
- if ($query ~~ /<stringparam=&stringparam>/) {
- my $format = $/<stringparam>[0];
- my $subquery = $query.subst(/<&stringparam>/, '');
+ my ($fmtquery, $subquery) = $parent.get-args($query, Nil);
+ if defined $subquery {
+ my $format = $parent.query($fmtquery);
my $text = $parent.query($subquery);
- if $text {
+ if $text && $format {
my $formatted = sprintf($format, $text);
return $parent.process-query($formatted, |%opts);
}
View
28 t/01-basics.t
@@ -5,7 +5,9 @@ BEGIN { @*INC.unshift: './lib' }
use Test;
use Flower;
-plan 9;
+plan 10;
+
+sub attrmake (*@opts) { @opts.join(' ') | @opts.reverse.join(' ') }
my $xml = '<?xml version="1.0"?>';
@@ -44,11 +46,9 @@ is $flower.parse(hello => 'hello world'), $xml~'<test>Good Day Mate</test>', 'ta
$template = '<test><attrib tal:attributes="hello hello; cya goodbye"/></test>';
$flower.=another(:template($template));
-my $matched = False;
-my $output = $flower.parse(hello => 'Hello World', goodbye => 'Goodbye Universe');
-if $output eq $xml~'<test><attrib hello="Hello World" cya="Goodbye Universe"/></test>' { $matched = True; }
-elsif $output eq $xml~'<test><attrib cya="Goodbye Universe" hello="Hello World"/></test>' { $matched = True; }
-ok $matched, 'tal:attributes';
+my $attrpos = attrmake 'hello="Hello World"', 'cya="Goodbye Universe"';
+is $flower.parse(hello => 'Hello World', goodbye => 'Goodbye Universe'),
+ $xml~'<test><attrib '~$attrpos~'/></test>', 'tal:attributes';
## test 7
@@ -68,3 +68,19 @@ $template = '<test tal:attributes="id id">Test document</test>';
$flower.=another(:template($template));
is $flower.parse(id=>'first'), $xml~'<test id="first">Test document</test>', 'attributes on root document';
+## test 10
+
+my @options = (
+ { value => 'a', label => 'Option 1' },
+ { value => 'b', label => 'Option 2', selected => 'selected' },
+ { value => 'c', label => 'Option 3' },
+);
+
+$template = '<select><option tal:repeat="option options" tal:attributes="value option/value; selected option/selected" tal:content="option/label"/></select>';
+
+$flower.=another(:template($template));
+
+$attrpos = attrmake 'value="b"', 'selected="selected"';
+
+is $flower.parse(options => @options), $xml~'<select><option value="a">Option 1</option><option '~$attrpos~'>Option 2</option><option value="c">Option 3</option></select>', 'attributes with undefined value';
+
View
2  t/06-utils-list.t
@@ -9,7 +9,7 @@ plan 2;
my $xml = '<?xml version="1.0"?>';
-my $template = '<table><tr tal:repeat="row group:2 items"><td tal:repeat="col row" tal:content="col"/></tr></table>';
+my $template = '<table><tr tal:repeat="row group:items 2"><td tal:repeat="col row" tal:content="col"/></tr></table>';
my $flower = Flower.new(:template($template));
View
13 t/06-utils-text.t
@@ -5,7 +5,7 @@ BEGIN { @*INC.unshift: './lib' }
use Test;
use Flower;
-plan 6;
+plan 7;
my $xml = '<?xml version="1.0"?>';
@@ -36,7 +36,7 @@ is $flower.parse(), $xml~'<test>Bob</test>', 'ucfirst: modifier';
## test 4
-$template = '<test><substr tal:replace="substr:3,5 \'theendoftheworld\'"/></test>';
+$template = '<test><substr tal:replace="substr:\'theendoftheworld\' 3 5"/></test>';
$flower.=another(:template($template));
@@ -44,7 +44,7 @@ is $flower.parse(), $xml~'<test>endof</test>', 'substr: modifier';
## test 5
-$template = '<test><substr tal:replace="substr:3,5,1 \'theendoftheworld\'"/></test>';
+$template = '<test><substr tal:replace="substr:\'theendoftheworld\' 3 5 1"/></test>';
$flower.=another(:template($template));
@@ -52,6 +52,13 @@ is $flower.parse(), $xml~'<test>endof...</test>', 'substr: modifier with ellipsi
## test 6
+$template = '<test><substr tal:replace="substr:\'theendoftheworld\' 3"/></test>';
+$flower.=another(:template($template));
+
+is $flower.parse(), $xml~'<test>endoftheworld</test>', 'substr: modifier without length';
+
+## test 7
+
$template = '<test><printf tal:replace="printf: \'$%0.2f\' \'2.5\'"/></test>';
$flower.=another(:template($template));
Please sign in to comment.
Something went wrong with that request. Please try again.