Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Query parsing changed, Handlers support query options now.

  • Loading branch information...
commit 2bce131d3f018095c5bc5207a71d01246f76b9c2 1 parent 7a88035
@supernovus authored
View
6 README
@@ -31,9 +31,13 @@ Note: Flower is not yet complete, and is still missing a lot of functionality.
There is no equivelant to the Petal::Parser::HTB, and no plans for one.
Use well-formed XML, it's just better.
* Flower supports petal:block elements, as per the PHPTAL project.
+ * There is no 'structure' keyword. Strings are turned into Exemel::Text
+ objects. If you really want to use XML structures in a replace or content
+ statement, return an Exemel::Element.
The above list will be updated as this project is developed, as I'm sure
-other changes will be introduced that be a gotchya for Petal users.
+other changes will be introduced that will be a gotchya for users of Petal,
+Zope or PHPTAL.
= Flower::Utils
View
3  docs/TODO.txt
@@ -1,5 +1,6 @@
Short Term (October 2010)
- - Make Handlers able to support options such as noxml and forcexml.
+ - Implement 'repeat' magic variable.
+ - Implement XML escaping on text nodes.
- Finish implementing Flower::Utils, as per the README.
- Add dump: handler to DefaultHandlers, as per the README.
View
36 lib/Flower.pm
@@ -149,7 +149,7 @@ method !parse-define ($xml is rw, $tag) {
}
method !parse-condition ($xml is rw, $tag) {
- if self.query($xml.attribs{$tag}) {
+ if self.query($xml.attribs{$tag}, :bool) {
$xml.unset($tag);
} else {
$xml = Nil;
@@ -211,7 +211,7 @@ method !parse-repeat ($xml is rw, $tag) {
method !parse-omit-tag ($xml is rw, $tag) {
my $nodes = $xml.nodes;
my $query = $xml.attribs{$tag};
- if self.query($query) {
+ if self.query($query, :bool) {
$xml = $nodes;
}
else {
@@ -219,12 +219,16 @@ method !parse-omit-tag ($xml is rw, $tag) {
}
}
-## This is a stub, expand it into a proper method.
-## Changed it from private to public so that the handler subs
-## could call this method.
-method query ($query, :$noxml, :$forcexml) {
- if $query eq '' { return True; } # empty text is true.
- if $query eq 'nothing' { return False; } # nothing is false.
+## Query data, this
+method query ($query, :$noxml, :$forcexml, :$bool) {
+ if $query eq '' {
+ if ($bool) { return True; }
+ else { return ''; }
+ }
+ if $query eq 'nothing' {
+ if ($bool) { return False; }
+ else { return ''; }
+ }
if $query ~~ /^\'(.*?)\'$/ {
if ($forcexml) {
return Exemel::Text.new(:text(~$0));
@@ -234,16 +238,22 @@ method query ($query, :$noxml, :$forcexml) {
if $query ~~ /^<.ident>+\:/ {
my ($handler, $subquery) = $query.split(/\:\s*/, 2);
if %!modifiers.exists($handler) {
- return %!modifiers{$handler}(self, $subquery);
+ my $data = %!modifiers{$handler}(self, $subquery, :$noxml, :$forcexml, :$bool);
+ return self!query-xml($data, :$forcexml, :$noxml);
}
}
my @paths = $query.split('/');
- #$query.split(/\s+/, 2)[0].split('/');
my $data = self!lookup(@paths, %.data);
- if ($forcexml && $data ~~ Str|Numeric) {
- $data = Exemel::Text.new(:text($data));
+ return self!query-xml($data, :$forcexml, :$noxml);
+}
+
+## Enforce forcexml and noxml rules for query().
+method !query-xml($data, :$forcexml, :$noxml) {
+ ## Default rule for forcexml converts non-XML objects into Exemel::Text.
+ if ($forcexml && $data !~~ Exemel) {
+ return Exemel::Text.new(:text(~$data));
}
- if ($noxml && $data !~~ Str|Numeric) {
+ elsif ($noxml && $data !~~ Str|Numeric) {
return; ## With noxml set, we only accept Strings or Numbers.
}
return $data;
View
10 lib/Flower/DefaultModifiers.pm
@@ -10,18 +10,18 @@ our sub all() {
return %modifiers;
}
-our sub true ($parent, $query) {
- my $result = $parent.query($query);
+our sub true ($parent, $query, *%opts) {
+ my $result = $parent.query($query, :bool);
return ?$result;
}
-our sub false ($parent, $query) {
- my $result = $parent.query($query);
+our sub false ($parent, $query, *%opts) {
+ my $result = $parent.query($query, :bool);
if $result { return False; }
else { return True; }
}
-our sub string ($parent, $query) {
+our sub string ($parent, $query, *%opts) {
my $string = $query;
# $string ~~ s:g/ '${' (.*?) '}' / $parent.query($0) /; # NYI in rakudo.
$string.=subst(:g, rx/'${' (.*?) '}'/, -> $/ { $parent.query($0) });
View
5 lib/Flower/Utils/Text.pm
@@ -12,12 +12,13 @@ our sub all() {
return %modifiers;
}
-our sub upper ($parent, $query) {
+our sub upper ($parent, $query, *%opts) {
my $result = $parent.query($query);
return $result.uc;
}
-our sub lower ($parent, $query) {
+our sub lower ($parent, $query, *%opts) {
my $result = $parent.query($query);
return $result.lc;
}
+
View
8 t/01-basics.t
@@ -1,11 +1,11 @@
#!/usr/bin/env perl6
-BEGIN { @*INC.push: './lib' }
+BEGIN { @*INC.unshift: './lib' }
use Test;
use Flower;
-plan 5;
+plan 6;
my $xml = '<?xml version="1.0"?>';
@@ -34,3 +34,7 @@ if $output eq $xml~'<test><attrib hello="Hello World" cya="Goodbye Universe"/></
elsif $output eq $xml~'<test><attrib cya="Goodbye Universe" hello="Hello World"/></test>' { $matched = True; }
ok $matched, 'petal:attributes';
+$template = '<test><hello/><goodbye petal:replace=""/></test>';
+$flower = Flower.new(:template($template));
+is $flower.parse(), $xml~'<test><hello/></test>', 'petal:replace when empty.';
+
View
2  t/02-repeat.t
@@ -1,6 +1,6 @@
#!/usr/bin/env perl6
-BEGIN { @*INC.push: './lib' }
+BEGIN { @*INC.unshift: './lib' }
use Test;
use Flower;
View
2  t/03-custom-modifiers.t
@@ -4,7 +4,7 @@
## Add a separate set of tests for Flower::Utils::Text, and one
## for all modifiers in the DefaultModifiers set.
-BEGIN { @*INC.push: './lib' }
+BEGIN { @*INC.unshift: './lib' }
use Test;
use Flower;
View
2  t/04-methods.t
@@ -1,6 +1,6 @@
#!/usr/bin/env perl6
-BEGIN { @*INC.push: './lib' }
+BEGIN { @*INC.unshift: './lib' }
use Test;
use Flower;
View
2  t/05-block.t
@@ -2,7 +2,7 @@
## Based on the second repeat test, but using petal:block instead of omit-tag.
-BEGIN { @*INC.push: './lib' }
+BEGIN { @*INC.unshift: './lib' }
use Test;
use Flower;
Please sign in to comment.
Something went wrong with that request. Please try again.