Content.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 198 CC 15 SCAR 26.2
CC
15
Cov
0%
mark_content
30.0
init
20.0
extract
20.0
CRAP 13.8
1 package Markup::Content;
2 $VERSION = '1.0.1';
3
4 ####################################################
5 # This module is protected under the terms of the
6 # GNU GPL. Please see
7 # http://www.opensource.org/licenses/gpl-license.php
8 # for more information.
9 ####################################################
10
11 use strict;
12 use Markup::Tree;
13 use Markup::TreeNode;
14 use Markup::MatchTree;
15
16 require Exporter;
17 require Carp;
18
19 our @ISA = qw(Exporter);
20
21 sub new {
22 my $invocant = shift();
23 my $class = ref($invocant) || $invocant;
24 $class = bless {
25 _tree => {
26 tree => undef,
27 minor => [],
28 major => []
29 },
30
31 template => undef,
32 template_options => { },
33 target => undef,
34 target_options => { },
35 template_name => 'default template'
36 }, $class;
37 $class->init(@_);
38 return $class;
39 }
40
41 sub init {
42 my $self = shift();
43 my %arg = @_;
44
45 foreach (keys %arg) {
46 if (exists $self->{$_}) {
47 $self->{$_} = $arg{$_};
48 }
49 else {
50 Carp::croak ("unrecognized option $_");
51 }
52 }
53
54 if (defined $self->{'target'}) {
55 $self->set_target($self->{'target'})
56 }
57
58 if (defined $self->{'template'}) {
59 $self->set_template($self->{'template'});
60 }
61 }
62
63 sub set_target {
64 my ($self, $target) = @_;
65
66 if (UNIVERSAL::isa($target, 'Markup::Tree')) {
67 $self->{'target'} = $self->{'_tree'};
68 $self->{'target'}->{'tree'} = $target->copy_of();
69 }
70
71 $_ = $self->{'target'};
72 $self->{'target'} = $self->{'_tree'};
73 $self->{'target'}->{'tree'} = Markup::Tree->new(%{ $self->{'target_options'} });
74 $self->{'target'}->{'tree'}->parse_file($_);
75
76 return ($self->{'target'}->{'tree'});
77 }
78
79 sub set_template {
80 my ($self, $template) = @_;
81
82 $self->{'template'} = $template and return($self->{'template'})
83 if (UNIVERSAL::isa($template, 'Markup::MatchTree'));
84
85 $_ = $self->{'template'};
86 $self->{'template'} = Markup::MatchTree->new(%{ $self->{'template_options'} });
87 $self->{'template'}->parse_file($_);
88
89 return ($self->{'template'});
90 }
91
92 sub extract {
93 my $self = shift();
94
95 Carp::croak ('No target or template') if (!($self->{'target'}->{'tree'} || $self->{'template'}));
96
97 $self->mark_content ('forward');
98
99 $self->mark_content ('backward');
100
101 $self->{'_tree'}->{'tree'}->foreach_node (sub {
102 my $node = shift();
103
104 if ($node->{'element_type'} eq '-->section' && $node->{'tagname'} eq 'CONTENT') {
105 $_[0] = $node->{'child_num'};
106 $_ = $node->next_node();
107 do {
108 if ($_->{'element_type'} eq '-->section' && $_->{'tagname'} eq 'CONTENT') {
109 $_[1] = $_->{'child_num'} - 1;
110 while ($_[0] < $_[1]) {
111 $node->attach_child($node->{'parent'}->{'children'}->[
112 $_[1]--
113 ]->drop());
114 }
115 @{$node->{'children'}} = reverse @{$node->{'children'}};
116 $self->{'_tree'}->{'tree'} = Markup::Tree->new(
117 no_indent => $self->{'_tree'}->{'tree'}->{'no_indent'}
118 );
119 $self->{'_tree'}->{'tree'}->get_node('root')->attach_child($node);
120 return 0;
121 }
122 } while ($_ = $_->next_node());
123 return 0;
124 }
125 return 1;
126 });
127
128 return $self->{'_tree'}->{'tree'};
129 }
130
131 sub tree {
132 shift()->{'_tree'}->{'tree'};
133 }
134
135 sub mark_content {
136 my ($self, $dir) = @_;
137 my ($move_node => $insert_marker);
138 my ($template_node => $target_node);
139 if ($dir eq 'forward') {
140 $move_node = sub { shift()->next_node(); };
141 ($template_node, $target_node) = ($self->{'template'}->get_node('root'),
142 $self->{'_tree'}->{'tree'}->get_node('root'));
143 $insert_marker = sub { shift()->previous_node()->insert(Markup::TreeNode->new(
144 element_type => '-->section',
145 tagname => 'CONTENT'), 'before'); };
146 }
147 else {
148 $move_node = sub { shift()->previous_node(); };
149 ($template_node, $target_node) = ($self->{'template'}->get_node('last'),
150 $self->{'_tree'}->{'tree'}->get_node('last'));
151 $insert_marker = sub { shift()->insert(Markup::TreeNode->new(
152 element_type => '-->section',
153 tagname => 'CONTENT'), 'before'); };
154 }
155
156 do {
157 if ($template_node->{'element_type'} eq '-->section' && $template_node->{'tagname'} eq 'CONTENT') {
158 $insert_marker->($target_node);
159 return;
160 }
161
162 my @res = $template_node->compare_to($target_node);
163 if (scalar(@res) == 2) {
164 $self->{'_tree'}->{'minor'}->[0] += $res[0]->[0];
165 $self->{'_tree'}->{'minor'}->[1] += $res[0]->[1];
166 $self->{'_tree'}->{'minor'}->[2] += $res[0]->[2];
167 $self->{'_tree'}->{'major'}->[0] += $res[1]->[0];
168 $self->{'_tree'}->{'major'}->[1] += $res[1]->[1];
169 $self->{'_tree'}->{'major'}->[2] += $res[1]->[2];
170 }
171 else {
172 if ($res[0] eq 'optional') {
173 $template_node = $move_node->($template_node);
174 }
175 else {
176 $target_node = $move_node->($target_node);
177 }
178 }
179 } while (($template_node = $move_node->($template_node)) && ($target_node = $move_node->($target_node)));
180 }
181
182 # TODO: write this method -> HTML => XML template
183 sub mk_template {
184 my ($self, $out) = @_;
185 my $data;
186 $out = \$data if (!$out);
187
188 Carp::croak ('No target') if (!($self->{'target'}->{'tree'}));
189
190 $self->{'_tree'}->{'tree'}->foreach_node(sub {
191 my $node = shift();
192
193
194 });
195 }
196
197 1;
198
199 __END__