Skip to content

Instantly share code, notes, and snippets.

@ab5tract
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ab5tract/cbf4867b441ab2ea84d7 to your computer and use it in GitHub Desktop.
Save ab5tract/cbf4867b441ab2ea84d7 to your computer and use it in GitHub Desktop.
Adventures in Perl 6 Document Publishing
# The Plural of Advent is Adventerati
use v6;
#use XML;
#use XML::Parser::Tiny;
use HTML::Parser::XML;
use HTTP::UserAgent;
my $advent-f = ("/Users/jhaltiwanger/exp/perl6/advent").IO;
my $adventageous;
if $advent-f ~~ :e {
$advent-f.open(:r);
$adventageous = $advent-f.slurp;
} else {
my $ua = HTTP::UserAgent.new( :timeout(10) );
say "getting the RSS\t\t\t{now}";
my $response = $ua.get("http://perl6advent.wordpress.com");
die "couldn't get the RSS !" unless $response.is-success;
$adventageous = $response.content;
$advent-f.open(:w);
spurt($advent-f, $adventageous);
}
say "parsing the RSS\t\t\t{now}";
my $p = HTML::Parser::XML.new( );
my $treatsy = $p.parse( $adventageous );
say "grokking the RSS\t\t\t{now}";
my @feeds = $treatsy.root.getElementsByTagName('pre');
@feeds.perl.say;
diff --git a/lib/XML.pm6 b/lib/XML.pm6
index 548397e..e7eaa9f 100644
--- a/lib/XML.pm6
+++ b/lib/XML.pm6
@@ -556,6 +556,8 @@ class XML::Element does XML::Node
if %query{'OBJECT'}:exists { $object = %query<OBJECT>; }
if %query{'BYINDEX'}:exists { $byindex = %query<BYINDEX>; }
+ $*OUT.say: "runing with\n\trecurse: $recurse\n\tnodepos: $nodepos\n\telements: {+@elements}";
+
for @.nodes -> $node
{
if $node ~~ XML::Element
@@ -636,6 +638,7 @@ class XML::Element does XML::Node
elsif $key eq 'TAG'
{
if $node.name !~~ $val { $matched = False; last; }
+ else { $*OUT.say: "loking for tag of $val and found {$node.name}"; }
}
elsif $key eq 'NS' | 'URI'
{
@@ -695,9 +698,10 @@ class XML::Element does XML::Node
my $subelements = $node.elements(|%opts);
if $subelements
{
+ $*OUT.say: "found {+$subelements.elems} subelements";
if $subelements ~~ Array
{
- @elements.push: |$subelements;
+ @elements.push: $subelements.list;
}
else
{
@@ -709,6 +713,7 @@ class XML::Element does XML::Node
{
return @elements[0];
}
+ say "matched: $matched and at $nodepos with {@elements.elems} elements";
}
$nodepos++;
}
@@ -725,6 +730,7 @@ class XML::Element does XML::Node
$new.nodes = @elements;
return $new;
}
+ $*OUT.say: "should be returning {+@elements}";
return @elements;
}
@@ -959,19 +965,20 @@ class XML::Document does XML::Node
my $doc = XML::Grammar.parse($xml);
if ($doc)
{
- #$*ERR.say: "We parsed the doc";
+ $*OUT.say: "We parsed the doc";
+ $*OUT.say: "and we have found {$doc<xmldecl><version><value>}";
if ($doc<xmldecl>)
{
- $version = ~$doc<xmldecl>[0]<version><value>;
- if ($doc<xmldecl>[0]<encoding>)
+ $version = ~$doc<xmldecl><version><value>;
+ if ($doc<xmldecl><encoding>)
{
- $encoding = ~$doc<xmldecl>[0]<encoding>[0]<value>;
+ $encoding = ~$doc<xmldecl><encoding><value>;
}
}
if ($doc<doctypedecl>)
{
- %doctype<type> = ~$doc<doctypedecl>[0]<name>;
- %doctype<value> = ~$doc<doctypedecl>[0]<content>;
+ %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);
equest from job control (^C))
➜ perl6 perl6-debug-m adventorator.p6
>>> LOADING adventorator.p6
>>> LOADING /Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/site/lib/HTML/Parser/XML.pm6
>>> LOADING /Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/site/lib/XML.pm6
>>> LOADING /Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/site/lib/XML/Grammar.pm6
Unhandled exception: ctxlexpad needs an MVMContext
at src/gen/m-CORE.setting:10700 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/runtime/CORE.setting.moarvm::33)
from src/gen/m-CORE.setting:10764 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/runtime/CORE.setting.moarvm:at_key:90)
from src/gen/m-CORE.setting:2935 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/runtime/CORE.setting.moarvm:postcircumfix:<{ }>:30)
from /Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/site/lib/Debugger/UI/CommandLine.pm:734 (<ephemeral file>::82)
from src/gen/m-CORE.setting:8369 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/runtime/CORE.setting.moarvm::557)
from src/gen/m-CORE.setting:8292 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/runtime/CORE.setting.moarvm:reify:53)
from src/gen/m-CORE.setting:8205 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/runtime/CORE.setting.moarvm::96)
from src/gen/m-CORE.setting:8201 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/runtime/CORE.setting.moarvm::219)
from src/gen/m-CORE.setting:8177 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/runtime/CORE.setting.moarvm:reify:60)
from src/gen/m-CORE.setting:8688 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/runtime/CORE.setting.moarvm:gimme:155)
from src/gen/m-CORE.setting:9208 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/runtime/CORE.setting.moarvm:sink:0)
from /Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/site/lib/Debugger/UI/CommandLine.pm:728 (<ephemeral file>:unhandled:117)
from /Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/site/lib/Debugger/UI/CommandLine.pm:719 (<ephemeral file>::25)
from src/gen/m-Metamodel.nqp:3173 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/nqp/lib/Perl6/Metamodel.moarvm:enter:28)
from src/gen/m-CORE.setting:3603 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/runtime/CORE.setting.moarvm:invoke:46)
from src/gen/m-BOOTSTRAP.nqp:2622 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/nqp/lib/Perl6/BOOTSTRAP.moarvm::101)
from gen/moar/stage2/NQPHLL.nqp:1365 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/nqp/lib/NQPHLL.moarvm:command_eval:374)
from src/Perl6/Compiler.nqp:17 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/nqp/lib/Perl6/Compiler.moarvm:command_eval:93)
from gen/moar/stage2/NQPHLL.nqp:1305 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/nqp/lib/NQPHLL.moarvm:command_line:116)
from src/gen/m-perl6-debug.nqp:497 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/runtime/perl6-debug.moarvm:MAIN:190)
from src/gen/m-perl6-debug.nqp:440 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/runtime/perl6-debug.moarvm:<mainline>:68)
from <unknown>:1 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/runtime/perl6-debug.moarvm:<main>:8)
from <unknown>:1 (/Users/ab5tract/.rakudobrew/moar-HEAD/install/languages/perl6/runtime/perl6-debug.moarvm:<entry>:9)
matched: False and at 17 with 77 elements
runing with
recurse: Inf
nodepos: 0
elements: 0
runing with
recurse: Inf
nodepos: 0
elements: 0
should be returning 0
matched: False and at 1 with 0 elements
should be returning 0
matched: False and at 27 with 77 elements
should be returning 77
^Cfish: Job 1, 'perl6 --ll-exception adventorator.p6 ' terminated by signal SIGINT (Quit request from job control (^C))
@ab5tract
Copy link
Author

Some of these changes are just stabs in the dark, such as

- @elements.push: |$subelements;

becoming

+ @elements.push: $subelements.list;

The changes between 59 and 73 fix the issue of "uninitialized value $v". I'm currently unsure of where else in the code the XML module is expecting arrays back where the parser only gives hashes..

@ab5tract
Copy link
Author

Also, don't mind the fact that it is using HTML instead of RSS. This breaks when I tried switching to RSS as well as when I used sample XML output files found on the web.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment