Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added recursive crafting, and make-xml() function.

  • Loading branch information...
commit 0a98c16a7e26fbb3b65c330f174905df59c098e1 1 parent 2d2aede
Timothy Totten authored
Showing with 38 additions and 3 deletions.
  1. +13 −3 README.md
  2. +10 −0 lib/XML.pm6
  3. +15 −0 t/make.t
View
16 README.md
@@ -39,6 +39,10 @@ Slurp the IO, parse the contents, and return an XML::Document object.
Return an XML::Document object representing the specified file.
You will be able to call $xml.save(); to save back to the original file.
+#### make-xml(Str $name, ... --> XML::Element)
+
+See the _XML::Element.craft()_ function for details on how this works.
+
### XML::Node
A _role_ used by the rest of the XML Node classes.
@@ -173,11 +177,17 @@ Insert an XML::Node at the beginning of our _@.nodes_ list.
#### insert(Str $name, ...)
Create a new XML::Element with the given name, and insert it to the
-beginning of our nodes list.
+beginning of our nodes list. Uses _craft()_ to build the element.
Any named parameters will be used as attributes, any positional parameters
-will be used as child nodes. If the positional parameters are not XML::Node
-using objects, they will be stringified and included as a new XML::Text node.
+will be used as child nodes.
+
+Positional parameters can be one of the following:
+
+ * An XML::Node object. Will be added as is.
+ * A String. Will be included as an XML::Text node.
+ * A Capture. Calls _craft()_ using the Capture as the signature.
+ * Anything else, will be stringified, and added as an XML::Text node.
#### append(XML::Node $node)
View
10 lib/XML.pm6
@@ -289,6 +289,10 @@ class XML::Element does XML::Node
{
$new.append($what);
}
+ elsif $what ~~ Capture
+ { ## In the case of a Capture, pass it to craft().
+ $new.append(self.craft(|$what));
+ }
elsif $what ~~ Str
{
my $text = XML::Text.new(:text($what));
@@ -1076,5 +1080,11 @@ module XML
{
return XML::Document.load($file);
}
+
+ sub make-xml (Str $name, *@contents, *%attribs) is export
+ {
+ return XML::Element.craft($name, |@contents, |%attribs);
+ }
+
}
View
15 t/make.t
@@ -0,0 +1,15 @@
+#!/usr/bin/env perl6
+
+use v6;
+
+use Test;
+use XML;
+
+plan 1;
+
+my $xml = make-xml('test', :type<embedded>,
+ \('hello', :lang<en>, "world"),
+ \('aurevoir', :lang<fr>, "univers")
+);
+
+is ~$xml, '<test type="embedded"><hello lang="en">world</hello><aurevoir lang="fr">univers</aurevoir></test>', 'make-xml worked.';
Please sign in to comment.
Something went wrong with that request. Please try again.