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   447410 use strict;
  18         175  
  18         582  
4 18     18   100 use warnings;
  18         39  
  18         684  
5              
6 18     18   4621 use Class::Utils qw(set_params);
  18         270872  
  18         644  
7 18     18   766 use Error::Pure qw(err);
  18         47  
  18         728  
8 18     18   9289 use PYX::Parser;
  18         234493  
  18         670  
9 18     18   9141 use PYX::Utils;
  18         135369  
  18         903  
10 18     18   11034 use Tags::Output::Raw;
  18         154065  
  18         13673  
11              
12             our $VERSION = 0.07;
13              
14             # Constructor.
15             sub new {
16 16     16 1 9248 my ($class, @params) = @_;
17              
18 16         51 my $self = bless {}, $class;
19              
20             # Input encoding.
21 16         72 $self->{'input_encoding'} = 'utf-8';
22              
23             # Input 'Tags' item callback.
24 16         41 $self->{'input_tags_item_callback'} = undef;
25              
26             # Output encoding.
27 16         40 $self->{'output_encoding'} = 'utf-8';
28              
29             # Tags object.
30 16         40 $self->{'tags'} = undef;
31              
32             # Process params.
33 16         88 set_params($self, @params);
34              
35 16 100       236 if (! defined $self->{'tags'}) {
36             $self->{'tags'} = Tags::Output::Raw->new(
37             'input_tags_item_callback'
38             => $self->{'input_tags_item_callback'},
39 1         13 'output_encoding' => $self->{'output_encoding'},
40             'output_handler' => \*STDOUT,
41             );
42             }
43              
44             # Check for Tags::Output object.
45 16 100       294 if (! $self->{'tags'}->isa('Tags::Output')) {
46 2         8 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         818 return $self;
67             }
68              
69             # Parse pyx text or array of pyx text.
70             sub parse {
71 16     16 1 7595 my ($self, $pyx, $out) = @_;
72              
73 16         70 $self->{'pyx_parser'}->parse($pyx, $out);
74 16         182 $self->{'tags'}->flush;
75              
76 16         452 return;
77             }
78              
79             # Parse file with pyx text.
80             sub parse_file {
81 15     15 1 8682 my ($self, $file, $out) = @_;
82              
83 15         68 $self->{'pyx_parser'}->parse_file($file, $out);
84 15         645 $self->{'tags'}->flush;
85              
86 15         448 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 6 my $self = shift;
101              
102 1         5 $self->{'tags'}->finalize;
103              
104 1         123 return;
105             }
106              
107             # Process start of element.
108             sub _start_element {
109 17     17   3211 my ($self, $elem) = @_;
110              
111 17         143 my $tags = $self->{'non_parser_options'}->{'tags'};
112 17         104 $tags->put(['b', $elem]);
113              
114 17         1039 return;
115             }
116              
117             # Process end of element.
118             sub _end_element {
119 16     16   1196 my ($self, $elem) = @_;
120              
121 16         34 my $tags = $self->{'non_parser_options'}->{'tags'};
122 16         61 $tags->put(['e', $elem]);
123              
124 16         1433 return;
125             }
126              
127             # Process data.
128             sub _data {
129 8     8   1341 my ($self, $data) = @_;
130              
131 8         49 my $tags = $self->{'non_parser_options'}->{'tags'};
132 8         28 $tags->put(['d', PYX::Utils::encode($data)]);
133              
134 8         1263 return;
135             }
136              
137             # Process attribute.
138             sub _attribute {
139 12     12   895 my ($self, $attr, $value) = @_;
140              
141 12         24 my $tags = $self->{'non_parser_options'}->{'tags'};
142 12         50 $tags->put(['a', $attr, $value]);
143              
144 12         2958 return;
145             }
146              
147             # Process instruction tag.
148             sub _instruction {
149 4     4   936 my ($self, $target, $code) = @_;
150              
151 4         38 my $tags = $self->{'non_parser_options'}->{'tags'};
152 4         27 $tags->put(['i', $target, $code]);
153              
154 4         252 return;
155             }
156              
157             # Process comments.
158             sub _comment {
159 4     4   905 my ($self, $comment) = @_;
160              
161 4         33 my $tags = $self->{'non_parser_options'}->{'tags'};
162 4         16 $tags->put(['c', PYX::Utils::encode($comment)]);
163              
164 4         221 return;
165             }
166              
167             1;
168              
169             __END__