| 1 | package Text::Nyarlax::Element;
|
|---|
| 2 |
|
|---|
| 3 | use strict;
|
|---|
| 4 | use warnings;
|
|---|
| 5 |
|
|---|
| 6 | our $VERSION = '0.1';
|
|---|
| 7 |
|
|---|
| 8 | use Carp;
|
|---|
| 9 | use Text::Nyarlax::ElementUtil qw( join_node );
|
|---|
| 10 | use base qw( Class::Accessor::Fast Exporter );
|
|---|
| 11 |
|
|---|
| 12 | __PACKAGE__->mk_ro_accessors( qw( name attribute ) );
|
|---|
| 13 | {
|
|---|
| 14 | no warnings 'uninitialized';
|
|---|
| 15 | *attr = __PACKAGE__->can('attribute');
|
|---|
| 16 | }
|
|---|
| 17 |
|
|---|
| 18 | our @EXPORT_OK = qw( element );
|
|---|
| 19 |
|
|---|
| 20 | sub new {
|
|---|
| 21 | my ( $class, %args ) = @_;
|
|---|
| 22 |
|
|---|
| 23 | my $name = $args{'name'}
|
|---|
| 24 | or croak "Element name is not specified.";
|
|---|
| 25 |
|
|---|
| 26 | my $attr = $args{'attribute'} || $args{'attr'} || {};
|
|---|
| 27 | croak "Attribute is not HASH reference." if ( ref $attr ne 'HASH' );
|
|---|
| 28 |
|
|---|
| 29 | my $self = bless {
|
|---|
| 30 | name => $name,
|
|---|
| 31 | attribute => $attr,
|
|---|
| 32 | content => [],
|
|---|
| 33 | }, $class;
|
|---|
| 34 |
|
|---|
| 35 | my $content = $args{'content'} || [];
|
|---|
| 36 | $content = [ $content ] if ( ref $content ne 'ARRAY' );
|
|---|
| 37 |
|
|---|
| 38 | $self->content( $content );
|
|---|
| 39 |
|
|---|
| 40 | return $self;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | sub parent {
|
|---|
| 44 | my $self = shift;
|
|---|
| 45 | if ( @_ ) {
|
|---|
| 46 | my $parent = shift @_;
|
|---|
| 47 | croak "Argument is not " . __PACKAGE__ . " object."
|
|---|
| 48 | if ( ref $parent ne __PACKAGE__ );
|
|---|
| 49 | $self->{'parent'} = $parent;
|
|---|
| 50 | }
|
|---|
| 51 | else {
|
|---|
| 52 | $self->{'parent'};
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | sub content {
|
|---|
| 57 | my $self = shift;
|
|---|
| 58 | if ( @_ ) {
|
|---|
| 59 | my $contents = shift @_;
|
|---|
| 60 | $contents = [ $contents ] if ( ref $contents ne 'ARRAY' );
|
|---|
| 61 | for my $content ( @{ $contents } ) {
|
|---|
| 62 | $content->parent( $self ) if ( ref $content eq __PACKAGE__ );
|
|---|
| 63 | }
|
|---|
| 64 | $self->{'content'} = $contents;
|
|---|
| 65 | }
|
|---|
| 66 | else {
|
|---|
| 67 | $self->{'content'};
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | sub shift_content {
|
|---|
| 72 | my ( $self ) = @_;
|
|---|
| 73 | shift @{ $self->{'content'} };
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | sub unshift_content {
|
|---|
| 77 | my ( $self, @contents ) = @_;
|
|---|
| 78 | for my $content ( @contents ) {
|
|---|
| 79 | $content->parent( $self ) if ( ref $content eq __PACKAGE__ );
|
|---|
| 80 | }
|
|---|
| 81 | unshift @{ $self->{'content'} }, @contents;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | sub pop_content {
|
|---|
| 85 | my ( $self ) = @_;
|
|---|
| 86 | pop @{ $self->{'content'} };
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | sub push_content {
|
|---|
| 90 | my ( $self, @contents ) = @_;
|
|---|
| 91 | for my $content ( @contents ) {
|
|---|
| 92 | $content->parent( $self ) if ( ref $content eq __PACKAGE__ );
|
|---|
| 93 | }
|
|---|
| 94 | push @{ $self->{'content'} }, @contents;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | sub join_content {
|
|---|
| 98 | my ( $self, %args ) = @_;
|
|---|
| 99 | $self->content( join_node( $self->content, %args ) );
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | sub element {
|
|---|
| 103 | my $name = shift;
|
|---|
| 104 | __PACKAGE__->new( name => $name );
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | 1;
|
|---|
| 108 | __END__
|
|---|
| 109 |
|
|---|
| 110 | =head1 NAME
|
|---|
| 111 |
|
|---|
| 112 | Text::Nyarlax::Element - Text::Nyarlax element object
|
|---|
| 113 |
|
|---|
| 114 | =head1 SYNOPSIS
|
|---|
| 115 |
|
|---|
| 116 | use Text::Nyarlax::Element;
|
|---|
| 117 |
|
|---|
| 118 | my $elm = Text::Nyarlax::Element->new(
|
|---|
| 119 | name => 'Section',
|
|---|
| 120 | attr => {
|
|---|
| 121 | id => 'MyID',
|
|---|
| 122 | class => 'MyClass',
|
|---|
| 123 | },
|
|---|
| 124 | content => [
|
|---|
| 125 | 'Foo',
|
|---|
| 126 | 'Bar',
|
|---|
| 127 | 'Baz'
|
|---|
| 128 | ],
|
|---|
| 129 | );
|
|---|
| 130 |
|
|---|
| 131 | =head1 DESCRIPTION
|
|---|
| 132 |
|
|---|
| 133 | Text::Nyarlax::Element is the object to compose the element tree.
|
|---|
| 134 | The element tree is generated by the parser of Text::Nyarlax.
|
|---|
| 135 |
|
|---|
| 136 | =head1 METHODS
|
|---|
| 137 |
|
|---|
| 138 | =head2 new
|
|---|
| 139 |
|
|---|
| 140 | The argument is three of name, attribute(attr) and content, and name is necessary.
|
|---|
| 141 |
|
|---|
| 142 | attribute has to be Hash and content has to be one character string,Text::Nyarlax::Element object
|
|---|
| 143 | or ARRAY which consists of a character string and Text::Nyarlax::element object.
|
|---|
| 144 |
|
|---|
| 145 | =head2 name
|
|---|
| 146 |
|
|---|
| 147 | get element name. readonly.
|
|---|
| 148 |
|
|---|
| 149 | =head2 attribute/attr
|
|---|
| 150 |
|
|---|
| 151 | get element attributes. return HASH reference.
|
|---|
| 152 |
|
|---|
| 153 | =head2 parent
|
|---|
| 154 |
|
|---|
| 155 | set/get the Text::Nyarlax::Element object which becomes a parent.
|
|---|
| 156 |
|
|---|
| 157 | =head2 content
|
|---|
| 158 |
|
|---|
| 159 | set/get the contents of Text::Nyarlax::Element object.
|
|---|
| 160 |
|
|---|
| 161 | When setting the contents, it's necessary to use ARRAY reference,
|
|---|
| 162 | and when getting the contents, ARRAY reference is returning.
|
|---|
| 163 |
|
|---|
| 164 | =head2 shift_content
|
|---|
| 165 |
|
|---|
| 166 | The contents are added at the first of obejct contents.
|
|---|
| 167 |
|
|---|
| 168 | =head2 unshift_content
|
|---|
| 169 |
|
|---|
| 170 | The first contents is taken out.
|
|---|
| 171 |
|
|---|
| 172 | =head2 push_content
|
|---|
| 173 |
|
|---|
| 174 | The contents are added at the last of obejct contents.
|
|---|
| 175 |
|
|---|
| 176 | =head2 pop_content
|
|---|
| 177 |
|
|---|
| 178 | The last contents is taken out.
|
|---|
| 179 |
|
|---|
| 180 | Naoki Okamura E<lt>thotep@nayrla.netE<gt>
|
|---|
| 181 |
|
|---|
| 182 | =head1 LICENSE
|
|---|
| 183 |
|
|---|
| 184 | This library is free software; you can redistribute it and/or modify
|
|---|
| 185 | it under the same terms as Perl itself.
|
|---|
| 186 |
|
|---|
| 187 | =head1 SEE ALSO
|
|---|
| 188 |
|
|---|
| 189 | L<Text::Nyarlax::Element::Content>
|
|---|
| 190 |
|
|---|
| 191 | =cut;
|
|---|