Builder

 view release on metacpan or  search on metacpan

MANIFEST  view on Meta::CPAN

lib/Builder.pm
lib/Builder/Utils.pm
lib/Builder/XML.pm
lib/Builder/XML/Utils.pm
MANIFEST
README
README.pod
t/00-load.t
t/boilerplate.t
t/builder_xml_basic.t
t/builder_xml_cdata.t
t/builder_xml_namespace.t
t/builder_xml_output.t
t/builder_xml_pretty.t
t/lib/IO/Scalar.pm
t/lib/IO/WrapTie.pm
t/pod-coverage.t
t/pod.t
TODO
META.yml
META.json

lib/Builder.pm  view on Meta::CPAN



=head2 block

Creates a block in this stack.  

First arg is the block to use, for eg.  'Builder::XML'.  Second arg must be a hashref of options (named parameters).

    my $builder = Builder->new();

    my $xm = $builder->block( 'Builder::XML', { cdata => 1 } );


For options that can be passed as args please see relevant Builder::* documentation.


=head2 render

Renders all the blocks for the requested builder stack returning the information.

    my $output = $builder->render;

lib/Builder/XML.pm  view on Meta::CPAN

}

sub __element__ {
    my ( $self, %param ) = @_;
    $param{ text    } ||= '';
    $param{ context } ||= 'element';
    $self->{ context }->{ $param{ context } }->( $self, \%param );
    return;
}

sub __cdata__ {
    my $self = shift;
    return $_[0]  if $self->{ cdata } == 1;
    return $self->__cdatax__( $_[0] );
}

sub __cdatax__ {
    my $self = shift;
    return q{<![CDATA[} . $_[0] . q{]]>};
}

sub __say__ {
    my ( $self, @say ) = @_;
    for my $said ( @say ) { $self->__push__( sub { $said } ) }
    $self;
}

lib/Builder/XML.pm  view on Meta::CPAN

    # and then provide example of what can go wrong!

...but recommend $builder->render for best practise.


=head2 __element__

Private


=head2 __cdata__

Wraps content in <![CDATA[ ]]> element.  
Useful for quick ditties like....

    $xm->span( $xm->__cdata__( 'yada yada' ) );
    
    # => <span><!CDATA[yada yada]]></span>
    
But for best practise you probably still find building a block more useful in the long run...

    my $xm = $builder->block( 'Builder::XML', { cdata => 1 } );
    
    $xm->span( 'yada yada' );


=head2 __cdatax__

PRIVATE - used with __cdata__


=head2 __say__

Really a Private method but as mentioned in I<Gotchas> it can be useful for working around some implementation issues.

=head2 __push__

Private

lib/Builder/XML/Utils.pm  view on Meta::CPAN

            $self->__push__( sub { 
                $self->__dec__;
                $self->__end_tab__ . q{</}.$tag.q{>} . $self->__close_newline__;
            });
        },
        
        'element' => sub {
            my ( $self, $param ) = @_;
            my $tag  = $self->{ns} . $param->{ element };
            my $text = $param->{text};
            $text    = $self->__cdatax__( $text )  if $self->{ cdata };
            
            my $attrib = q{};
            for my $k ( keys %{ $param->{ attr } } ) { 
                $attrib .= sprintf( ' %s%s="%s"', $self->{attr_ns}, $k, $param->{attr}->{$k} );  
            }
            
            return $self->__push__( sub {
                $self->__tab__ . q{<}.$tag.$attrib.q{>}.$text.q{</}.$tag.q{>} . $self->__close_newline__ 
            })  if $text;
            

lib/Builder/XML/Utils.pm  view on Meta::CPAN

    return $context;
}


sub get_args {
    my ( %arg ) = @_;
    $arg{ns}      = defined $arg{namespace}      ? $arg{namespace} . q{:}         : q{};
    $arg{attr_ns} = defined $arg{attr_namespace} ? ( $arg{attr_namespace} . ':' ) : q{};
    $arg{attr_ns} = $arg{qualified_attr}         ? $arg{ns}                       : $arg{attr_ns};
    $arg{cr}      = $arg{ newline }              ? "\n" x $arg{ newline }         : q{}; 
    $arg{cdata} ||= 0;   
    
    $arg{ open_newline  } = defined $arg{ open_newline }  ? $arg{ open_newline }  : 1;
    $arg{ close_newline } = defined $arg{ close_newline } ? $arg{ close_newline } : 1;
    
    $arg{ pre_indent } ||= 0;
    
    $arg{ empty_tag } ||= q{ />};
    
    return %arg;
}

t/builder_xml_cdata.t  view on Meta::CPAN

use Test::More tests => 2;
use Builder;

my $builder = Builder->new();
my $with_cdata = $builder->block( 'Builder::XML', { cdata => 1 } );
my $without_cdata = $builder->block( 'Builder::XML' );
my $text  = "Tom, Dick & Harry";
my $text2 = lc $text;

# test 1
$with_cdata->body( sub {
    $with_cdata->span($text);
    $with_cdata->span( $with_cdata->__cdata__( $text2 ) );
});
is $builder->render, data("<![CDATA[$text]]>", $text2), "xml cdata test 1";

# test 2
$without_cdata->body( sub {
    $without_cdata->span($text);
    $without_cdata->span( $without_cdata->__cdata__( $text2 ) );
});
is $builder->render, data($text, $text2), "xml cdata test 2";

sub data {
    return qq{<body><span>$_[0]</span><span><![CDATA[$_[1]]]></span></body>};
}


__END__

 '<body><span><!CDATA[[Tom, Dick & Harry]]></span><span><!CDATA[[<!CDATA[[tom, dick & harry]]>]]></span></body>'
 '<body><span><!CDATA[[Tom, Dick & Harry]]></span><span><!CDATA[[tom, dick & harry]]></span></body>'



( run in 0.667 second using v1.01-cache-2.11-cpan-454fe037f31 )