| 1 | package PYX::SGML::Tags; | ||||||||||
| 2 | |||||||||||
| 3 | 18 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 18 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 18 | ▶ | use Class::Utils qw(set_params); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 18 | ▶ | use Error::Pure qw(err); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 18 | ▶ | use PYX::Parser; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 18 | ▶ | use PYX::Utils; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 18 | ▶ | use Tags::Output::Raw; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 18 | our $VERSION = 0.08; | |||||||||
| 13 | |||||||||||
| 14 | # Constructor. | ||||||||||
| 15 | sub new { | ||||||||||
| 16 | 16 | ▶ | my ($class, @params) = @_; | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 17 | |||||||||||
| 18 | 16 | my $self = bless {}, $class; | |||||||||
| 19 | |||||||||||
| 20 | # Input encoding. | ||||||||||
| 21 | 16 | $self->{'input_encoding'} = 'utf-8'; | |||||||||
| 22 | |||||||||||
| 23 | # Input 'Tags' item callback. | ||||||||||
| 24 | 16 | $self->{'input_tags_item_callback'} = undef; | |||||||||
| 25 | |||||||||||
| 26 | # Output encoding. | ||||||||||
| 27 | 16 | $self->{'output_encoding'} = 'utf-8'; | |||||||||
| 28 | |||||||||||
| 29 | # Tags object. | ||||||||||
| 30 | 16 | $self->{'tags'} = undef; | |||||||||
| 31 | |||||||||||
| 32 | # Process params. | ||||||||||
| 33 | 16 | set_params($self, @params); | |||||||||
| 34 | |||||||||||
| 35 | 16 | ▶ | if (! defined $self->{'tags'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | $self->{'tags'} = Tags::Output::Raw->new( | ||||||||||
| 37 | 'input_tags_item_callback' | ||||||||||
| 38 | => $self->{'input_tags_item_callback'}, | ||||||||||
| 39 | 1 | 'output_encoding' => $self->{'output_encoding'}, | |||||||||
| 40 | 'output_handler' => \*STDOUT, | ||||||||||
| 41 | ); | ||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | # Check for Tags::Output object. | ||||||||||
| 45 | 16 | ▶ | if (! $self->{'tags'}->isa('Tags::Output')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 2 | 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 | 'tags' => $self->{'tags'}, | |||||||||
| 62 | }, | ||||||||||
| 63 | ); | ||||||||||
| 64 | |||||||||||
| 65 | # Object. | ||||||||||
| 66 | 14 | return $self; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | # Parse pyx text or array of pyx text. | ||||||||||
| 70 | sub parse { | ||||||||||
| 71 | 16 | ▶ | my ($self, $pyx, $out) = @_; | ||||||||
|
Subroutine
parse: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 72 | |||||||||||
| 73 | 16 | $self->{'pyx_parser'}->parse($pyx, $out); | |||||||||
| 74 | 16 | $self->{'tags'}->flush; | |||||||||
| 75 | |||||||||||
| 76 | 16 | return; | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | # Parse file with pyx text. | ||||||||||
| 80 | sub parse_file { | ||||||||||
| 81 | 15 | ▶ | my ($self, $file, $out) = @_; | ||||||||
|
Subroutine
parse_file: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 82 | |||||||||||
| 83 | 15 | $self->{'pyx_parser'}->parse_file($file, $out); | |||||||||
| 84 | 15 | $self->{'tags'}->flush; | |||||||||
| 85 | |||||||||||
| 86 | 15 | return; | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | # Parse from handler. | ||||||||||
| 90 | sub parse_handler { | ||||||||||
| 91 | 0 | ▶ | my ($self, $input_file_handler, $out) = @_; | ||||||||
|
Subroutine
parse_handler: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 92 | |||||||||||
| 93 | 0 | $self->{'pyx_parser'}->parse_handler($input_file_handler, $out); | |||||||||
| 94 | 0 | $self->{'tags'}->flush; | |||||||||
| 95 | |||||||||||
| 96 | 0 | return; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | sub finalize { | ||||||||||
| 100 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
finalize: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 101 | |||||||||||
| 102 | 1 | $self->{'tags'}->finalize; | |||||||||
| 103 | |||||||||||
| 104 | 1 | return; | |||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | # Process start of element. | ||||||||||
| 108 | sub _start_element { | ||||||||||
| 109 | 17 | ▶ | my ($self, $elem) = @_; | ||||||||
|
Subroutine
_start_element: called CC=1 SCAR=0.0
| |||||||||||
| 110 | |||||||||||
| 111 | 17 | my $tags = $self->{'non_parser_options'}->{'tags'}; | |||||||||
| 112 | 17 | $tags->put(['b', $elem]); | |||||||||
| 113 | |||||||||||
| 114 | 17 | return; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | # Process end of element. | ||||||||||
| 118 | sub _end_element { | ||||||||||
| 119 | 16 | ▶ | my ($self, $elem) = @_; | ||||||||
|
Subroutine
_end_element: called CC=1 SCAR=0.0
| |||||||||||
| 120 | |||||||||||
| 121 | 16 | my $tags = $self->{'non_parser_options'}->{'tags'}; | |||||||||
| 122 | 16 | $tags->put(['e', $elem]); | |||||||||
| 123 | |||||||||||
| 124 | 16 | return; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | # Process data. | ||||||||||
| 128 | sub _data { | ||||||||||
| 129 | 8 | ▶ | my ($self, $data) = @_; | ||||||||
|
Subroutine
_data: called CC=1 SCAR=0.0
| |||||||||||
| 130 | |||||||||||
| 131 | 8 | my $tags = $self->{'non_parser_options'}->{'tags'}; | |||||||||
| 132 | 8 | $tags->put(['d', PYX::Utils::encode($data)]); | |||||||||
| 133 | |||||||||||
| 134 | 8 | return; | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | # Process attribute. | ||||||||||
| 138 | sub _attribute { | ||||||||||
| 139 | 12 | ▶ | my ($self, $attr, $value) = @_; | ||||||||
|
Subroutine
_attribute: called CC=1 SCAR=0.0
| |||||||||||
| 140 | |||||||||||
| 141 | 12 | my $tags = $self->{'non_parser_options'}->{'tags'}; | |||||||||
| 142 | 12 | $tags->put(['a', $attr, $value]); | |||||||||
| 143 | |||||||||||
| 144 | 12 | return; | |||||||||
| 145 | } | ||||||||||
| 146 | |||||||||||
| 147 | # Process instruction tag. | ||||||||||
| 148 | sub _instruction { | ||||||||||
| 149 | 4 | ▶ | my ($self, $target, $code) = @_; | ||||||||
|
Subroutine
_instruction: called CC=1 SCAR=0.0
| |||||||||||
| 150 | |||||||||||
| 151 | 4 | my $tags = $self->{'non_parser_options'}->{'tags'}; | |||||||||
| 152 | 4 | $tags->put(['i', $target, $code]); | |||||||||
| 153 | |||||||||||
| 154 | 4 | return; | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | # Process comments. | ||||||||||
| 158 | sub _comment { | ||||||||||
| 159 | 4 | ▶ | my ($self, $comment) = @_; | ||||||||
|
Subroutine
_comment: called CC=1 SCAR=0.0
| |||||||||||
| 160 | |||||||||||
| 161 | 4 | my $tags = $self->{'non_parser_options'}->{'tags'}; | |||||||||
| 162 | 4 | $tags->put(['c', PYX::Utils::encode($comment)]); | |||||||||
| 163 | |||||||||||
| 164 | 4 | return; | |||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | 18 | 1; | |||||||||
| 168 | |||||||||||
| 169 | __END__ | ||||||||||