Skip to content

Commit

Permalink
Merge pull request #67 from raku-community-modules/support-io-names
Browse files Browse the repository at this point in the history
Support IO::Path filenames
  • Loading branch information
2colours committed Jan 18, 2023
2 parents 96d3212 + 349c1e0 commit a5643f5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 36 deletions.
7 changes: 2 additions & 5 deletions lib/XML.pm6
Expand Up @@ -14,7 +14,7 @@ module XML
return XML::Document.new($input.slurp-rest);
}

sub from-xml-file (Str $file) is export
sub from-xml-file (IO::Path() $file) is export
{
return XML::Document.load($file);
}
Expand All @@ -24,7 +24,7 @@ module XML
return XML::Element.craft($name, |@contents, |%attribs);
}

multi sub open-xml (Str $src where .IO.e && !.IO.d) is export {
multi sub open-xml (IO::Path(Str) $src where :f) is export {
from-xml-file $src
}

Expand All @@ -36,7 +36,4 @@ module XML
from-xml-stream $src
}

multi sub open-xml (IO::Path $src) is export {
from-xml-stream $src.open
}
}
56 changes: 25 additions & 31 deletions lib/XML/Document.pm6
Expand Up @@ -15,7 +15,7 @@ class XML::Document does XML::Node
appendNode insertNode insertBefore insertAfter
replaceChild removeChild craft
>;
has $.filename; ## Optional, used for new load() and save() methods.
has IO::Path() $.filename; ## Optional, used for new load() and save() methods.

method cloneNode ()
{
Expand All @@ -38,42 +38,36 @@ class XML::Document does XML::Node
$.root{$offset};
}

multi method new (Str $xml, :$filename)
multi method new (Str $xml, IO::Path() :$filename)
{
my $version = '1.0';
my $encoding;
my %doctype;
my $root;
my $doc = XML::Grammar.parse($xml);
if ($doc)
die "could not parse XML" unless $doc;
#$*ERR.say: "We parsed the doc";
if ($doc<xmldecl>)
{
#$*ERR.say: "We parsed the doc";
if ($doc<xmldecl>)
$version = ~$doc<xmldecl><version><value>;
$version ~~ s:g/\"//; ## get rid of any quotes in the version
$version ~~ s:g/\'//;
if ($doc<xmldecl><encoding>)
{
$version = ~$doc<xmldecl><version><value>;
$version ~~ s:g/\"//; ## get rid of any quotes in the version
$version ~~ s:g/\'//;
if ($doc<xmldecl><encoding>)
{
$encoding = ~$doc<xmldecl><encoding><value>;
$encoding ~~ s:g/\"//; ## get rid of any quotes in the version
$encoding ~~ s:g/\'//;
}
$encoding = ~$doc<xmldecl><encoding><value>;
$encoding ~~ s:g/\"//; ## get rid of any quotes in the version
$encoding ~~ s:g/\'//;
}
if ($doc<doctypedecl>)
{
%doctype<type> = ~$doc<doctypedecl><name>;
%doctype<value> = ~$doc<doctypedecl><content>;
}
$root = XML::Element.parse-node($doc<root>);
my $this = self.new(:$version, :$encoding, :%doctype, :$root, :$filename);
$root.parent = $this;
return $this;
}
else
if ($doc<doctypedecl>)
{
die "could not parse XML";
%doctype<type> = ~$doc<doctypedecl><name>;
%doctype<value> = ~$doc<doctypedecl><content>;
}
$root = XML::Element.parse-node($doc<root>);
my $this = self.new(:$version, :$encoding, :%doctype, :$root, :$filename);
$root.parent = $this;
return $this;
}

multi method new (XML::Element $root)
Expand Down Expand Up @@ -107,9 +101,9 @@ class XML::Document does XML::Node
## load() is used instead of new() to create a new object.
## e.g.: my $doc = XML::Document.load("myfile.xml");
##
method load (Str $filename)
method load (IO::Path() $filename)
{
my $text = $filename.IO.slurp.Str;
my $text = $filename.slurp.Str;
return self.new($text, :$filename);
}

Expand All @@ -127,14 +121,14 @@ class XML::Document does XML::Node
## Saves the XML to a new file. Does not override the existing filename,
## so future calls to save() will save to the original file, not the new one.
##
method save (Str $filename?, Bool :$copy)
method save (IO::Path() $filename?, Bool :$copy)
{
my $fname = $filename || $!filename;
if (!$copy)
my $fname = $filename // $!filename;
unless ($copy)
{
$!filename = $fname;
}
$fname.IO.spurt: self.Str;
$fname.spurt: self.Str;
}

}

0 comments on commit a5643f5

Please sign in to comment.