File Coverage

blib/lib/PYX/SGML/Tags.pm
Criterion Covered Total %
statement 69 73 94.5
branch 4 4 100.0
condition n/a
subroutine 17 18 94.4
pod 5 5 100.0
total 95 100 95.0


line stmt bran cond sub pod time code
1             package PYX::SGML::Tags;
2              
3 18     18   435499 use strict;
  18         174  
  18         533  
4 18     18   96 use warnings;
  18         40  
  18         577  
5              
6 18     18   4666 use Class::Utils qw(set_params);
  18         271022  
  18         728  
7 18     18   776 use Error::Pure qw(err);
  18         36  
  18         698  
8 18     18   9440 use PYX::Parser;
  18         229341  
  18         668  
9 18     18   8895 use PYX::Utils;
  18         130982  
  18         861  
10 18     18   10033 use Tags::Output::Raw;
  18         151297  
  18         13750  
11              
12             our $VERSION = 0.08;
13              
14             # Constructor.
15             sub new {
16 16     16 1 9451 my ($class, @params) = @_;
17              
18 16         49 my $self = bless {}, $class;
19              
20             # Input encoding.
21 16         75 $self->{'input_encoding'} = 'utf-8';
22              
23             # Input 'Tags' item callback.
24 16         39 $self->{'input_tags_item_callback'} = undef;
25              
26             # Output encoding.
27 16         39 $self->{'output_encoding'} = 'utf-8';
28              
29             # Tags object.
30 16         40 $self->{'tags'} = undef;
31              
32             # Process params.
33 16         89 set_params($self, @params);
34              
35 16 100       225 if (! defined $self->{'tags'}) {
36             $self->{'tags'} = Tags::Output::Raw->new(
37             'input_tags_item_callback'
38             => $self->{'input_tags_item_callback'},
39 1         15 'output_encoding' => $self->{'output_encoding'},
40             'output_handler' => \*STDOUT,
41             );
42             }
43              
44             # Check for Tags::Output object.
45 16 100       307 if (! $self->{'tags'}->isa('Tags::Output')) {
46 2         9 err "Bad 'Tags::Output::*' object.";
47             }
48              
49             # PYX::Parser object.
50             $self->{'pyx_parser'} = PYX::Parser->new(
51             'callbacks' => {
52             'attribute' => \&_attribute,
53             'comment' => \&_comment,
54             'data' => \&_data,
55             'end_element' => \&_end_element,
56             'instruction' => \&_instruction,
57             'start_element' => \&_start_element,
58             },
59             'input_encoding' => $self->{'input_encoding'},
60             'non_parser_options' => {
61 14         207 'tags' => $self->{'tags'},
62             },
63             );
64              
65             # Object.
66 14         838 return $self;
67             }
68              
69             # Parse pyx text or array of pyx text.
70             sub parse {
71 16     16 1 7742 my ($self, $pyx, $out) = @_;
72              
73 16         72 $self->{'pyx_parser'}->parse($pyx, $out);
74 16         185 $self->{'tags'}->flush;
75              
76 16         442 return;
77             }
78              
79             # Parse file with pyx text.
80             sub parse_file {
81 15     15 1 8281 my ($self, $file, $out) = @_;
82              
83 15         64 $self->{'pyx_parser'}->parse_file($file, $out);
84 15         576 $self->{'tags'}->flush;
85              
86 15         434 return;
87             }
88              
89             # Parse from handler.
90             sub parse_handler {
91 0     0 1 0 my ($self, $input_file_handler, $out) = @_;
92              
93 0         0 $self->{'pyx_parser'}->parse_handler($input_file_handler, $out);
94 0         0 $self->{'tags'}->flush;
95              
96 0         0 return;
97             }
98              
99             sub finalize {
100 1     1 1 5 my $self = shift;
101              
102 1         6 $self->{'tags'}->finalize;
103              
104 1         122 return;
105             }
106              
107             # Process start of element.
108             sub _start_element {
109 17     17   2153 my ($self, $elem) = @_;
110              
111 17         110 my $tags = $self->{'non_parser_options'}->{'tags'};
112 17         99 $tags->put(['b', $elem]);
113              
114 17         1001 return;
115             }
116              
117             # Process end of element.
118             sub _end_element {
119 16     16   807 my ($self, $elem) = @_;
120              
121 16         32 my $tags = $self->{'non_parser_options'}->{'tags'};
122 16         61 $tags->put(['e', $elem]);
123              
124 16         1324 return;
125             }
126              
127             # Process data.
128             sub _data {
129 8     8   906 my ($self, $data) = @_;
130              
131 8         65 my $tags = $self->{'non_parser_options'}->{'tags'};
132 8         32 $tags->put(['d', PYX::Utils::encode($data)]);
133              
134 8         1263 return;
135             }
136              
137             # Process attribute.
138             sub _attribute {
139 12     12   687 my ($self, $attr, $value) = @_;
140              
141 12         24 my $tags = $self->{'non_parser_options'}->{'tags'};
142 12         47 $tags->put(['a', $attr, $value]);
143              
144 12         2705 return;
145             }
146              
147             # Process instruction tag.
148             sub _instruction {
149 4     4   667 my ($self, $target, $code) = @_;
150              
151 4         35 my $tags = $self->{'non_parser_options'}->{'tags'};
152 4         30 $tags->put(['i', $target, $code]);
153              
154 4         208 return;
155             }
156              
157             # Process comments.
158             sub _comment {
159 4     4   651 my ($self, $comment) = @_;
160              
161 4         48 my $tags = $self->{'non_parser_options'}->{'tags'};
162 4         18 $tags->put(['c', PYX::Utils::encode($comment)]);
163              
164 4         220 return;
165             }
166              
167             1;
168              
169             __END__