| 1 | package Markup::MatchTree; | ||
| 2 | $VERSION = '1.0.0'; | ||
| 3 | |||
| 4 | #################################################### | ||
| 5 | # This module is protected under the terms of the | ||
| 6 | # GNU GPL. Please see | ||
| 7 | # http://www.opensource.org/licenses/gpl-license.php | ||
| 8 | # for more information. | ||
| 9 | #################################################### | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use Markup::MatchNode; | ||
| 13 | |||
| 14 | require Carp; | ||
| 15 | require Exporter; | ||
| 16 | require Markup::Tree; | ||
| 17 | |||
| 18 | our @ISA = qw(Markup::Tree); | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | my $invocant = shift(); | ||
| 22 | my $class = ref($invocant) || $invocant; | ||
| 23 | $class = bless { | ||
| 24 | _parser => undef, | ||
| 25 | _globals => { | ||
| 26 | relpath => '' | ||
| 27 | }, | ||
| 28 | _tree => Markup::MatchNode->new( element_type => '-->root', tagname => '-->root', level => 0 ), | ||
| 29 | |||
| 30 | callbacks => {}, | ||
| 31 | no_squash_whitespace => 0, | ||
| 32 | parser_options => {} | ||
| 33 | }, $class; | ||
| 34 | $class->init(@_); | ||
| 35 | return $class; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub init { | ||
| 39 | my $self = shift(); | ||
| 40 | my %arg = @_; | ||
| 41 | |||
| 42 | foreach (keys %arg) { | ||
| 43 | if (exists $self->{$_}) { | ||
| 44 | $self->{$_} = $arg{$_}; | ||
| 45 | } | ||
| 46 | else { | ||
| 47 | Carp::croak ("unrecognized option $_"); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | $self->{'_parser'} = XML::Parser->new( %{ $self->{'parser_options'} } ); | ||
| 52 | $self->_init_hookups (); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub parse_file { | ||
| 56 | my ($self, $glob) = (shift(), Markup::Tree::_mk_filehandle(shift())); | ||
| 57 | $self->parse($glob); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _init_xml_hookups { | ||
| 61 | Carp::croak ('_init_xml_hookups is not a MatchTree method'); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _init_html_hookups { | ||
| 65 | Carp::croak ('HTML can not be transformed to a MatchTree'); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _init_hookups { | ||
| 69 | my $self = shift(); | ||
| 70 | |||
| 71 | $self->{'_globals'}->{'level'} = 0; | ||
| 72 | $self->{'_globals'}->{'last_node'} = $self->{'_tree'}; | ||
| 73 | $self->{'_globals'}->{'new'} = [ 1, undef ]; | ||
| 74 | |||
| 75 | $self->{'_parser'}->setHandlers('Start', sub { | ||
| 76 | my ($expat, $tag, @attrs) = @_; | ||
| 77 | my (%attrs, %options); | ||
| 78 | return if ($tag eq 'template'); # throw out root node | ||
| 79 | |||
| 80 | while (scalar(@attrs)) { $attrs{pop(@attrs)} = pop(@attrs); } | ||
| 81 | |||
| 82 | delete $attrs{'/'}; | ||
| 83 | |||
| 84 | foreach (keys %attrs) { | ||
| 85 | if (s/^_//) { | ||
| 86 | $attrs{$_} = delete $attrs{'_'.$_}; | ||
| 87 | next; | ||
| 88 | } | ||
| 89 | |||
| 90 | foreach my $et (qw(element_type tagname text)) { | ||
| 91 | if ($_ eq $et) { | ||
| 92 | $options{$_} = delete $attrs{$_}; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | if ($_ eq 'options') { | ||
| 97 | @_ = split (',', $attrs{$_}); | ||
| 98 | foreach (@_) { | ||
| 99 | if ($_ =~ m/call_filter(?:\s+)?\((.+?)\)/i) { | ||
| 100 | if (exists $self->{'callbacks'}->{$1} && | ||
| 101 | ref($self->{'callbacks'}->{$1}) eq 'CODE') | ||
| 102 | { | ||
| 103 | push @{ $options{'options'}->{'call_filter'} }, | ||
| 104 | $self->{'callbacks'}->{$1}; | ||
| 105 | } | ||
| 106 | else { | ||
| 107 | Carp::croak ("callback '$1' does not exist or is" | ||
| 108 | ." not a CODE ref"); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | elsif ($_ =~ m/ignore_attrs/i) { | ||
| 112 | $options{'options'}->{'ignore_attrs'} = 1; | ||
| 113 | } | ||
| 114 | elsif ($_ =~ m/text_not_null/i) { | ||
| 115 | $options{'options'}->{'text_not_null'} = 1; | ||
| 116 | } | ||
| 117 | elsif ($_ =~ m/ignore_children/i) { | ||
| 118 | $options{'options'}->{'ignore_children'} = 1; | ||
| 119 | } | ||
| 120 | elsif ($_ =~ m/optional/i) { | ||
| 121 | $options{'options'}->{'optional'} = 1; | ||
| 122 | } | ||
| 123 | else { | ||
| 124 | Carp::croak ("Unrecognized option $_"); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | delete $attrs{'options'}; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | if ($tag eq 'section') { | ||
| 133 | $options{'element_type'} = '-->section'; | ||
| 134 | } | ||
| 135 | |||
| 136 | $options{'level'} = $self->{'_globals'}->{'level'}; | ||
| 137 | $options{'attr'} = \%attrs; | ||
| 138 | $options{'tagname'} = $options{'element_type'} if (!$options{'tagname'}); | ||
| 139 | my $node = Markup::MatchNode->new ( %options ); | ||
| 140 | my $parent = $self->{'_globals'}->{'last_node'}; | ||
| 141 | |||
| 142 | while (($node->{'level'} - 1) != $parent->{'level'}) { | ||
| 143 | if ($parent->{'parent'} eq '(empty)') { last; } | ||
| 144 | $parent = $parent->{'parent'}; | ||
| 145 | } | ||
| 146 | |||
| 147 | $node->attach_parent($parent); | ||
| 148 | |||
| 149 | $self->{'_globals'}->{'last_node'} = $node; | ||
| 150 | $self->{'_globals'}->{'level'}++; | ||
| 151 | $self->{'_globals'}->{'new'} = [ 1, undef ]; | ||
| 152 | }); | ||
| 153 | |||
| 154 | $self->{'_parser'}->setHandlers('Char', sub { | ||
| 155 | my ($expat, $text) = @_; | ||
| 156 | |||
| 157 | if ($self->{'no_squash_whitespace'}) { | ||
| 158 | if (ref($self->{'no_squash_whitespace'}) eq 'ARRAY') { | ||
| 159 | my $squash = 1; | ||
| 160 | foreach (@{ $self->{'no_squash_whitespace'} }) { | ||
| 161 | if ($_ eq $self->{'_globals'}->{'last_node'}->{'tagname'}) { | ||
| 162 | $squash = 0; | ||
| 163 | last; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | $text = Markup::Tree::_squash_whitespace ($text) if ($squash); | ||
| 167 | return if (!$text); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | else { | ||
| 171 | $text = Markup::Tree::_squash_whitespace ($text); | ||
| 172 | return if (!$text); | ||
| 173 | } | ||
| 174 | |||
| 175 | if ($self->{'_globals'}->{'new'}->[0]) { | ||
| 176 | my $estruct = Markup::MatchNode->new(element_type => '-->text', tagname => '-->text', | ||
| 177 | level => ($self->{'_globals'}->{'last_node'}->{'level'} + 1), | ||
| 178 | text => $text); | ||
| 179 | |||
| 180 | $self->{'_globals'}->{'last_node'}->attach_child($estruct); | ||
| 181 | |||
| 182 | $self->{'_globals'}->{'new'} = [ 0, $estruct ]; | ||
| 183 | } | ||
| 184 | else { | ||
| 185 | $self->{'_globals'}->{'new'}->[1]->{'text'} .= $text; | ||
| 186 | } | ||
| 187 | } ); | ||
| 188 | |||
| 189 | $self->{'_parser'}->setHandlers('End', sub { | ||
| 190 | $self->{'_globals'}->{'level'}--; | ||
| 191 | }); | ||
| 192 | } | ||
| 193 | |||
| 194 | 1; | ||
| 195 | |||
| 196 | __END__ |