Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

extract text from node; add tests for levels

  • Loading branch information...
commit a62f46ddc85b3056561456d96ac70010003af305 1 parent 6bb897a
@zag authored zag committed
Showing with 55 additions and 29 deletions.
  1. +43 −1 lib/WriteAt.pm
  2. +12 −28 t/01.bookinfo.t
View
44 lib/WriteAt.pm
@@ -116,12 +116,14 @@ sub get_book_info_blocks {
#convert =Include $n to DOM if To::* passed
if ( $to && $n->name eq 'Include' ) {
$n = $to->_make_dom_node($n);
+
#set current path
$to->context->custom->{src} = $n->{PATH};
}
if ( my $converted_block_name = &get_name_from_locale( $n->name ) ) {
- push @{$res->{$converted_block_name}}, $n;
+ push @{ $res->{$converted_block_name} }, $n;
+
# overwrite original name
$n->{name} = $converted_block_name;
}
@@ -134,6 +136,7 @@ sub get_book_info_blocks {
}
=pod
+
{
=tagname
=childs
@@ -180,10 +183,28 @@ Make tree using levels
my $levels = &WriteAt::make_levels( "CHAPTER", 0, $tree1 );
+ return
+
+ [
+ {
+ node => {ref to object},
+ childs => [ array of childs]
+
+ },
+ ...
+ ]
=cut
sub make_levels {
my ( $name, $level, $tree ) = @_;
+
+ #check if root node pod
+ # call for childs
+ if ( my $first = $tree->[0] ) {
+ return &make_levels( $name, $level, $first->childs )
+ if $first->name eq 'pod';
+ }
+
my @res = ();
while ( my $current = shift @$tree ) {
next unless $current->name eq $name;
@@ -205,6 +226,27 @@ sub make_levels {
return \@res;
}
+=head2 get_text(node1, node2, ...)
+
+return string of all childs texts nodes
+
+=cut
+
+sub get_text {
+ my @nodes = @_;
+ my $txt = '';
+ foreach my $n (@nodes) {
+ if ( $n->{type} eq 'text' ) {
+ $txt .= join "" => @{ $n->childs };
+ }
+ else {
+ $txt .= &get_text( @{ $n->childs } );
+ }
+ }
+ chomp($txt);
+ return $txt;
+}
+
=head1 METHODS
=cut
View
40 t/01.bookinfo.t
@@ -16,23 +16,6 @@ use WriteAt;
use utf8;
use_ok('WriteAt');
-=head2 flat_pod $tree, &sub_ref( $node )
-
-=cut
-
-sub flat_pod {
- my $tree = shift;
- my $sub = shift || return;
- my @nodes = ref($tree) eq 'ARRAY' ? @$tree : ($tree);
- foreach my $n (@nodes) {
- next unless ref($n); #skip text
- return 1 if ( $sub->($n) );
- if ( my $sub_tree = $n->childs ) {
- return 1 if &flat_pod( $n->childs, $sub );
- }
- }
-}
-
my $t = <<T;
=begin pod
=ЗАГОЛОВОК asdasd
@@ -51,7 +34,7 @@ Jan 08th 2011(v0.5)[zag] Подпрограммы и сигнатуры
firstname: Александр
surname: Загацкий
-=CHAPTER Test chpater
+=CHAPTER Test chapter
Ok
=head1 Test name
@@ -63,7 +46,7 @@ yes
ok
-=CHAPTER Test chpater2
+=CHAPTER Test chapter2
Ok
=head1 Teste
@@ -71,23 +54,24 @@ Ok
=end pod
T
-utf8::decode($t) unless utf8::is_utf8($t);
+#utf8::decode($t) unless utf8::is_utf8($t);
my $tree = Perl6::Pod::Utl::parse_pod( $t, default_pod => 1 )
|| die "Can't parse ";
my %res = ();
$tree = &WriteAt::get_book_info_blocks( $tree, \%res );
-
-my ($root) = @$tree;
-my $tree1 = $tree;
-if ( $root->name eq 'pod' ) {
- $tree1 = $root->childs;
-}
-
#print Dumper $tree; exit;
-print Dumper [ &WriteAt::make_levels( "CHAPTER", 0, $tree1 ) ];
+my $res = &WriteAt::make_levels( "CHAPTER", 0, $tree );
+is scalar(@$res), 2 , 'Get semantic nodes';
+is &WriteAt::get_text($res->[0]->{node}), 'Test chapter', 'get text content of node';
+#print Dumper $res->[0]->{node};
+#use Perl6::Pod::Test;
+#my $dump = &Perl6::Pod::Test::dump_tree($result);
+#print Dumper $result;
+
exit;
+
use_ok "WriteAt::To::Atom";
my $atom = new WriteAt::To::Atom:: lang => 'en';
diag $atom->writer;
Please sign in to comment.
Something went wrong with that request. Please try again.