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