Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cleanups, another() method, initial List utils.

 * Renamed 'never' to 'skip'.
 * Added repeat-every and repeat-skip methods to Repeat object.
 * Added a new method of creating a new Flower object from an existing one.
   Preserves the find function and custom modifiers.
 * Added an initial version of Flower::Utils::List, with sort: and group: methods.
  • Loading branch information...
commit 1ed469a3b970cb81d4531eb10445ffe572db75d3 1 parent ed24b24
@supernovus authored
View
6 README
@@ -43,7 +43,7 @@ Note: Flower is not yet complete, and is still missing a lot of functionality.
the Petal version. Note: it does not support the 'letter' or 'Letter'
attributes, but instead has some new methods that take paramters:
'every xx' the number (not the index) is divisible by xx.
- 'never xx' the number is not divisible by xx.
+ 'skip xx' the number is not divisible by xx.
'gt xx' the number is greater than xx.
'lt xx' the number is less than xx.
'eq xx' the number is equal to xx.
@@ -61,7 +61,9 @@ modifiers. The following sets are planned for inclusion:
- Text, same as the :text set from Petal::Utils
- Logic, same as the :logic set from Petal::Utils
- - List, same as the :list set from Petal::Utils
+ - List, same as the :list set from Petal::Utils, plus group: which
+ turns a flat Array into a nested Array, where each inner Array has a
+ set number of elements in it.
- Hash, same as the :hash set from Petal::Utils
- Date, similar to the :date set from Petal::Utils, using on DateTime::Utils
Not exactly the same, it offers strftime: and rfc: and works on
View
14 lib/Flower.pm
@@ -42,11 +42,15 @@ class Flower::Repeat {
## Flower exclusive methods below here, make lists and tables easier.
method every ($num) { $.number % $num == 0 }
- method never ($num) { $.number % $num != 0 }
+ method skip ($num) { $.number % $num != 0 }
method lt ($num) { $.number < $num }
method gt ($num) { $.number > $num }
method eq ($num) { $.number == $num }
method ne ($num) { $.number != $num }
+
+ ## Versions of every and skip that also match on start.
+ method repeat-every ($num) { $.start || $.every($num) }
+ method repeat-skip ($num) { $.start || $.every($num) }
}
method new (:$find is copy, :$file, :$template is copy) {
@@ -78,6 +82,14 @@ method new (:$find is copy, :$file, :$template is copy) {
self.bless(*, :$template, :$find, :%modifiers);
}
+## A way to spawn another Flower using the same find and modifiers.
+method another (:$file, :$template) {
+ if ( ! $file && ! $template ) { die "a file or template must be specified."; }
+ my $new = Flower.new(:$file, :$template, :find($!find));
+ $new.add-modifiers(%!modifiers);
+ return $new;
+}
+
method !xml-ns ($ns) {
return $ns.subst(/^xmlns\:/, '');
}
View
7 lib/Flower/Utils/Debug.pm
@@ -1,9 +1,4 @@
-module Flower::Utils::Perl;
-
-## Like it's cousins 'python:' and 'php:', the 'perl:' modifier allows
-## you to query the data using the native Perl 6 format instead of the
-## path syntax. Example: <eg tal:replace="perl:my-hash<key>.method('param')" />
-## The 'my-hash' in the example must be a valid key in the Flower data.
+module Flower::Utils::Debug;
our sub all() {
my %modifiers = {
View
37 lib/Flower/Utils/List.pm
@@ -0,0 +1,37 @@
+module Flower::Utils::List;
+
+our sub all() {
+ my %modifiers = {
+ group => &array_group,
+ 'sort' => &array_sort,
+ };
+ return %modifiers;
+}
+
+our sub array_sort ($parent, $query, *%opts) {
+ my $array = $parent.query($query);
+ if $array ~~ Array {
+ my @newarray = $array.sort;
+ return @newarray;
+ }
+}
+
+our sub array_group ($parent, $query, *%opts) {
+ my ($num, $subquery) = $query.split(/\s+/, 2);
+ my $array = $parent.query($subquery);
+ if $array ~~ Array {
+ my @nest = ([]);
+ my $level = 0;
+ loop (my $i=0; $i < $array.elems; $i++) {
+ if $level > @nest.end {
+ @nest.push: [];
+ }
+ @nest[$level].push: $array[$i];
+ if ($i+1) % $num == 0 {
+ $level++;
+ }
+ }
+ return @nest;
+ }
+}
+
View
14 t/01-basics.t
@@ -14,24 +14,24 @@ my $flower = Flower.new(:template($template));
is $flower.parse(my_test_var => 'Hello World'), $xml~'<test><item>Hello World</item></test>', 'tal:define and tal:content';
$template = '<test><item tal:define="test1 my_test_var1; test2 my_test_var2" tal:content="string:${test1} = ${test2}"/></test>';
-$flower = Flower.new(:template($template));
+$flower.=another(:template($template));
is $flower.parse(my_test_var1 => 'Hello', my_test_var2 => 'World'), $xml~'<test><item>Hello = World</item></test>', 'tal:define with multiple definitions.';
$template = '<test><replaced tal:replace="hello">This will be replaced</replaced></test>';
-$flower = Flower.new(:template($template));
+$flower.=another(:template($template));
is $flower.parse(hello => 'Hello World'), $xml~'<test>Hello World</test>', 'tal:replace';
$template = '<test><true tal:condition="hello">This is true</true><false tal:condition="notreal">This is false</false><right tal:condition="true:hello"/><wrong tal:condition="false:hello"/></test>';
-$flower = Flower.new(:template($template));
+$flower.=another(:template($template));
is $flower.parse(hello => 'Hello World'), $xml~'<test><true>This is true</true><right/></test>', 'tal:condition';
$template = '<test><div tal:omit-tag="">Good <b tal:omit-tag="hello">Day</b> Mate</div></test>';
#$template = '<test><div tal:omit-tag="string:1">Good Day Mate</div></test>';
-$flower = Flower.new(:template($template));
+$flower.=another(:template($template));
is $flower.parse(hello => 'hello world'), $xml~'<test>Good Day Mate</test>', 'tal:omit-tag';
$template = '<test><attrib tal:attributes="hello hello; cya goodbye"/></test>';
-$flower = Flower.new(:template($template));
+$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; }
@@ -39,10 +39,10 @@ elsif $output eq $xml~'<test><attrib cya="Goodbye Universe" hello="Hello World"/
ok $matched, 'tal:attributes';
$template = '<test><hello/><goodbye tal:replace=""/></test>';
-$flower = Flower.new(:template($template));
+$flower.=another(:template($template));
is $flower.parse(), $xml~'<test><hello/></test>', 'tal:replace when empty.';
$template = '<test xmlns:petal="http://xml.zope.org/namespaces/tal"><div petal:replace="test"/></test>';
-$flower = Flower.new(:template($template));
+$flower.=another(:template($template));
is $flower.parse(test=>'Hello World'), $xml~'<test xmlns:petal="http://xml.zope.org/namespaces/tal">Hello World</test>', 'tal:replace with custom namespace.';
View
18 t/02-repeat.t
@@ -20,12 +20,12 @@ my @items = (
is $flower.parse(:items(@items)), $xml~'<test><item alt="One">First</item><item alt="Two">Second</item><item alt="Three">Third</item></test>', 'tal:repeat';
$template = '<test><div tal:repeat="item items" tal:omit-tag=""><tr><td tal:content="item/alt"/><td tal:content="item/content"/></tr></div></test>';
-$flower = Flower.new(:template($template));
+$flower.=another(:template($template));
is $flower.parse(:items(@items)), $xml~'<test><tr><td>One</td><td>First</td></tr><tr><td>Two</td><td>Second</td></tr><tr><td>Three</td><td>Third</td></tr></test>', 'tal:repeat with nested elements and omit-tag';
## Here we test tal:block as well.
$template = '<test><tal:block tal:repeat="item items"><tr><td tal:content="item/alt"/><td tal:content="item/content"/></tr></tal:block></test>';
-$flower = Flower.new(:template($template));
+$flower.=another(:template($template));
is $flower.parse(:items(@items)), $xml~'<test><tr><td>One</td><td>First</td></tr><tr><td>Two</td><td>Second</td></tr><tr><td>Three</td><td>Third</td></tr></test>', 'tal:block used in repeat';
## Now we're going to test the repeat object.
@@ -38,30 +38,30 @@ my @rows = (
[ '3.1', '3.2', '3.3' ],
);
-$flower = Flower.new(:template($template));
+$flower.=another(:template($template));
is $flower.parse(:rows(@rows)), $xml~'<table><tr><td>1 / 1 = 1.1</td><td>1 / 2 = 1.2</td><td>1 / 3 = 1.3</td></tr><tr><td>2 / 1 = 2.1</td><td>2 / 2 = 2.2</td><td>2 / 3 = 2.3</td></tr><tr><td>3 / 1 = 3.1</td><td>3 / 2 = 3.2</td><td>3 / 3 = 3.3</td></tr></table>', 'nested repeat numbers';
$template = '<test><tal:block tal:repeat="item items"><item tal:condition="repeat/item/odd" tal:attributes="id repeat/item/index">Odd</item><item tal:condition="repeat/item/even" tal:attributes="id repeat/item/index">Even</item></tal:block></test>';
-$flower = Flower.new(:template($template));
+$flower.=another(:template($template));
is $flower.parse(:items([1..4])), $xml~'<test><item id="0">Odd</item><item id="1">Even</item><item id="2">Odd</item><item id="3">Even</item></test>', 'repeat with odd and even conditionals';
$template = '<test><tal:block tal:repeat="item items"><item tal:condition="repeat/item/start">First</item><item tal:condition="repeat/item/inner">Inner</item><item tal:condition="repeat/item/end" tal:attributes="length repeat/item/length">Last</item></tal:block></test>';
-$flower = Flower.new(:template($template));
+$flower.=another(:template($template));
is $flower.parse(:items([1..4])), $xml~'<test><item>First</item><item>Inner</item><item>Inner</item><item length="4">Last</item></test>', 'repeat with start, end, inner and length.';
-$template = '<test><tal:block tal:repeat="item items"><item tal:condition="repeat/item/every \'3\'">Every third</item><item tal:condition="repeat/item/never \'3\'">Normal item</item></tal:block></test>';
+$template = '<test><tal:block tal:repeat="item items"><item tal:condition="repeat/item/every \'3\'">Every third</item><item tal:condition="repeat/item/skip \'3\'">Normal item</item></tal:block></test>';
-$flower = Flower.new(:template($template));
+$flower.=another(:template($template));
-is $flower.parse(:items([1..7])), $xml~'<test><item>Normal item</item><item>Normal item</item><item>Every third</item><item>Normal item</item><item>Normal item</item><item>Every third</item><item>Normal item</item></test>', 'repeat with every and never';
+is $flower.parse(:items([1..7])), $xml~'<test><item>Normal item</item><item>Normal item</item><item>Every third</item><item>Normal item</item><item>Normal item</item><item>Every third</item><item>Normal item</item></test>', 'repeat with every and skip';
$template = '<test><tal:block tal:repeat="item items"><item tal:condition="repeat/item/lt \'3\'">lt 3</item><item tal:condition="repeat/item/gt \'3\'">gt 3</item><item tal:condition="repeat/item/eq \'3\'">the third</item></tal:block></test>';
-$flower = Flower.new(:template($template));
+$flower.=another(:template($template));
is $flower.parse(:items([1..5])), $xml~'<test><item>lt 3</item><item>lt 3</item><item>the third</item><item>gt 3</item><item>gt 3</item></test>', 'repeat with gt, lt and eq';
View
2  t/06-debug-modifier.t
@@ -17,7 +17,7 @@ my %ahash = {
'anarray' => [ 'one', 'two', 'three' ],
}
-$flower.add-modifiers(Flower::Utils::Perl::all());
+$flower.add-modifiers(Flower::Utils::Debug::all());
is $flower.parse(object => %ahash), $xml~'<test><dump type="Hash">{"anarray" => ["one", "two", "three"]}</dump></test>', 'dump: and what: modifiers';
View
28 t/08-utils-list.t
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl6
+
+BEGIN { @*INC.unshift: './lib' }
+
+use Test;
+use Flower;
+use Flower::Utils::List;
+
+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 $flower = Flower.new(:template($template));
+
+$flower.add-modifiers(Flower::Utils::List::all());
+
+is $flower.parse(:items(['a'..'d'])), $xml~'<table><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>', 'group: modifier';
+
+$template = '<fresh><i tal:repeat="item sort:items" tal:content="item"/></fresh>';
+
+$flower.=another(:template($template));
+
+my @items = 5,3,7,1,2;
+
+is $flower.parse(:items(@items)), $xml~'<fresh><i>1</i><i>2</i><i>3</i><i>5</i><i>7</i></fresh>', 'sort: modifier';
+
Please sign in to comment.
Something went wrong with that request. Please try again.