File Coverage

blib/lib/SGML/PYX.pm
Criterion Covered Total %
statement 78 79 98.7
branch 23 24 95.8
condition 6 6 100.0
subroutine 12 12 100.0
pod 2 2 100.0
total 121 123 98.3


line stmt bran cond sub pod time code
1             package SGML::PYX;
2              
3 12     12   406393 use strict;
  12         113  
  12         350  
4 12     12   61 use warnings;
  12         38  
  12         347  
5              
6 12     12   1616 use Class::Utils qw(set_params);
  12         88754  
  12         477  
7 12     12   7583 use Encode qw(encode_utf8);
  12         128959  
  12         829  
8 12     12   89 use Error::Pure qw(err);
  12         20  
  12         463  
9 12     12   6246 use Tag::Reader::Perl;
  12         28269  
  12         438  
10 12     12   5339 use PYX qw(comment end_element char instruction start_element);
  12         100545  
  12         224  
11 12     12   1136 use PYX::Utils qw(decode entity_decode);
  12         30  
  12         18910  
12              
13             our $VERSION = 0.06;
14              
15             # Constructor.
16             sub new {
17 46     46 1 35719 my ($class, @params) = @_;
18              
19             # Create object.
20 46         114 my $self = bless {}, $class;
21              
22             # Output callback.
23             $self->{'output'} = sub {
24 50     50   1584 my (@data) = @_;
25              
26 50         112 print join "\n", map { encode_utf8($_) } @data;
  84         408  
27 50         3046 print "\n";
28              
29 50         272 return;
30 46         306 };
31              
32             # Process params.
33 46         195 set_params($self, @params);
34              
35             # Object.
36 46         464 $self->{'_tag_reader'} = Tag::Reader::Perl->new;
37              
38             # Object.
39 46         1297 return $self;
40             }
41              
42             # Parse file.
43             sub parsefile {
44 45     45 1 38091 my ($self, $sgml_file) = @_;
45              
46             # Set file.
47 45         207 $self->{'_tag_reader'}->set_file($sgml_file);
48              
49             # Process.
50 45         3824 while (my ($data, $tag_type, $line, $column)
51             = $self->{'_tag_reader'}->gettoken) {
52              
53             # Data.
54 47 100       41986 if ($tag_type eq '!data') {
    100          
    100          
    100          
    100          
    100          
    50          
55 3         30 $self->{'output'}->(char(decode(entity_decode($data))));
56              
57             # Comment.
58             } elsif ($tag_type eq '!--') {
59 2         21 $data =~ s/^$//ms;
61 2         13 $self->{'output'}->(comment($data));
62              
63             # End of element.
64             } elsif ($tag_type =~ m/^\//ms) {
65 3         15 my $element = $data;
66 3         26 $element =~ s/^<\///ms;
67 3         16 $element =~ s/>$//ms;
68 3         14 $self->{'output'}->(end_element($element));
69              
70             # Begin of element.
71             } elsif ($tag_type =~ m/^\w+/ms) {
72 31         214 $data =~ s/^
73 31         139 $data =~ s/>$//ms;
74 31         57 my $end = 0;
75 31 100       82 if ($data =~ s/\/$//ms) {
76 5         8 $end = 1;
77             }
78 31         132 (my $element, $data) = ($data =~ m/^([^\s]+)\s*(.*)$/ms);
79 31         89 my @attrs = $self->_parse_attributes($data);
80 30         90 $self->{'output'}->(start_element($element, @attrs));
81 30 100       199 if ($end) {
82 5         30 $self->{'output'}->(end_element($element));
83             }
84              
85             # Doctype.
86             } elsif ($tag_type eq '!doctype') {
87             # Nop.
88              
89             # CData.
90             } elsif ($tag_type eq '![cdata[') {
91 4         30 $data =~ s/^
92 4         30 $data =~ s/\]\]>$//ms;
93 4         18 $self->{'output'}->(char(decode(entity_decode($data))));
94              
95             # Instruction.
96             } elsif ($tag_type =~ m/^\?/ms) {
97 3         58 $data =~ s/^<\?//ms;
98 3         32 $data =~ s/\s*\?>$//ms;
99 3         16 my ($target, $code) = split m/\s+/ms, $data, 2;
100 3         15 $self->{'output'}->(instruction($target, $code));
101              
102             } else {
103 0         0 err "Unsupported tag type '$tag_type'.";
104             }
105             }
106              
107 44         1963 return;
108             }
109              
110             # Parse attributes.
111             sub _parse_attributes {
112 31     31   62 my ($self, $data) = @_;
113              
114 31         44 my $original_data = $data;
115 31         47 my @attrs;
116 31         63 while ($data) {
117              
118             # or
119 35 100 100     261 if ($data =~ m/^([_\w:][\.\-\w:]*)\s*=\s*"(.*?)"\s*(.*)$/ms
    100 100        
    100          
120              
121             # or
122             || $data =~ m/^([_\w:][\.\-\w:]*)\s*=\s*'(.*?)'\s*(.*)$/ms
123              
124             # or .
125             || $data =~ m/^([_\w:][\.\-\w:]*)\s*=\s*([^\s]+)\s*(.*)$/ms) {
126              
127 27         90 push @attrs, $1, $2;
128 27         69 $data = $3;
129              
130             #
131             } elsif ($data =~ m/^([_\w:][\.\-\w:]*)\s*=\s*$/ms) {
132 3         11 push @attrs, $1, '';
133 3         9 $data = '';
134              
135             #
136             } elsif ($data =~ m/^([_\w:][\.\-\w:]*)\s*(.*)$/ms) {
137 4         11 push @attrs, $1, $1;
138 4         13 $data = $2;
139             } else {
140 1         8 err 'Problem with attribute parsing.',
141             'data', $original_data;
142             }
143             }
144              
145 30         95 return (@attrs);
146             }
147              
148             1;
149              
150             __END__