CSS-Object
view release on metacpan or search on metacpan
lib/CSS/Object/Format.pm view on Meta::CPAN
##----------------------------------------------------------------------------
## CSS Object Oriented - ~/lib/CSS/Object/Format.pm
## Version v0.2.0
## Copyright(c) 2020 DEGUEST Pte. Ltd.
## Author: Jacques Deguest <jack@deguest.jp>
## Created 2020/06/21
## Modified 2024/09/05
## All rights reserved
##
## This program is free software; you can redistribute it and/or modify it
## under the same terms as Perl itself.
##----------------------------------------------------------------------------
package CSS::Object::Format;
BEGIN
{
use strict;
use warnings;
use parent qw( Module::Generic );
our $VERSION = 'v0.2.0';
};
sub init
{
my $self = shift( @_ );
$self->{new_line} = "\n";
$self->{open_brace_on_new_line} = 1;
$self->{close_brace_on_new_line} = 1;
$self->{open_brace_and_new_line} = 1;
$self->{indent} = '';
$self->{property_separator} = "\n";
$self->{_init_strict_use_sub} = 1;
$self->SUPER::init( @_ );
$self->{_params} = [qw(
close_brace_on_new_line
indent
new_line
open_brace_and_new_line
open_brace_on_new_line
property_separator
)];
return( $self );
}
sub backup_parameters { return( shift->clone ); }
sub class { return( ref( $_[0] ) ); }
sub close_brace_on_new_line { return( shift->_set_get_boolean( 'close_brace_on_new_line', @_ ) ); }
sub comment_as_string
{
my( $self, $elem ) = @_;
# no overloading;
return( $self->error( "No comment object was provided." ) ) if( !defined( $elem ) );
return( $self->error( "Comment object provied is not a CSS::Object::Comment object." ) ) if( !$self->_is_a( $elem, 'CSS::Object::Comment' ) );
return( '/* ' . $elem->values->join( "\n" )->scalar . ' */' );
}
sub copy_parameters_from
{
my $self = shift( @_ );
my $fmt = shift( @_ ) || return( $self->error( "No formatter object was provided to copy the parameters from." ) );
return( $self->error( "Formatter object provided is actually not a formatter object." ) ) if( !$self->_is_a( $fmt, 'CSS::Object::Format' ) );
# my( $p, $f, $l ) = caller();
my $ok_params = $self->{_params};
for( @$ok_params )
{
$self->$_( $fmt->$_ ) if( $fmt->can( $_ ) );
}
return( $self );
}
sub elements_as_string
{
my( $self, $elems ) = @_;
# no overloading;
return( $self->error( "No elements array was provided." ) ) if( !defined( $elems ) );
return( $self->error( "Elements provided is not an array object." ) ) if( !$self->_is_a( $elems, 'Module::Generic::Array' ) );
my $result = Module::Generic::Array->new;
my $nl = $self->new_line->scalar;
my $prop_sep = $self->property_separator->scalar;
## $prop_sep .= $self->indent->scalar;
$elems->foreach(sub
{
$result->push( $_->format->indent->scalar . $_->as_string . ( $_->isa( 'CSS::Object::Comment' ) ? '' : ';' ) );
});
return( $result->join( $prop_sep )->scalar );
}
## sub indent { return( shift->_set_get_scalar_as_object( 'indent', @_ ) ); }
sub indent
{
my $self = shift( @_ );
if( @_ )
{
my $val = shift( @_ );
# my( $p, $f, $l ) = caller();
return( $self->_set_get_scalar_as_object( 'indent', $val ) );
}
return( $self->_set_get_scalar_as_object( 'indent' ) );
}
sub keyframes_as_string
{
lib/CSS/Object/Format.pm view on Meta::CPAN
CSS::Object::Format - CSS Object Oriented Stringificator
=head1 SYNOPSIS
use CSS::Object::Format;
my $format = CSS::Object::Format->new( debug => 3 ) ||
die( CSS::Object::Format->error );
my $prop = CSS::Object::Property->new(
format => $format,
debug => 3,
name => 'display',
value => 'inline-block',
) || die( CSS::Object::Property->error );
print( $prop->as_string );
=head1 VERSION
v0.2.0
=head1 DESCRIPTION
L<CSS::Object::Format::Inline> is a CSS inline stringificator
=head1 CONSTRUCTOR
=head2 new
To instantiate a new L<CSS::Object::Format> object, pass an hash reference of following parameters:
=over 4
=item I<debug>
This is an integer. The bigger it is and the more verbose is the output.
=back
=head1 PARAMETERS
The available parameters used to alter the formatting of the formatters are as follow. Please see each of them methods for their respective purpose and usage.
=over 4
=item * L</close_brace_on_new_line>
=item * L</new_line>
=item * L</open_brace_on_new_line>
=item * L</open_brace_and_new_line>
=item * L</indent>
=item * L</property_separator>
=back
=head1 METHODS
=head2 backup_parameters
This will create a deep copy of this formatter's L</parameters> and return it.
See L</restore_parameters>
=head2 close_brace_on_new_line
This takes a boolean value. If true, this will instruct the formatter to place the closing brace on a new line.
=head2 comment_as_string
Provided with a comment object, and this will return the comment formatted.
=head2 copy_parameters_from
Provided with another L<CSS::Object::Format> object, and this will copy all suitable L</parameters> to it.
This is called from the L<format> methods in CSS element classes when a new format object is provided to the element.
=head2 elements_as_string
Provided with an array object (L<Module::Generic::Array>), and this will format all the elements and return a string.
=head2 indent
This is one of L</parameters> that sets the string to use as indent. Indent string can be provided in rule formatter or property formatter.
This returns the current character value as a L<Module::Generic::Scalar> object.
=head2 keyframes_as_string
This formats the keyframe special rule and returns a string.
=head2 new_line
This sets or gets the new line character to be used for new lines.
This returns the current character value as a L<Module::Generic::Scalar> object.
=head2 open_brace_on_new_line
This takes a boolean value. If true, this will instruct the formatter to place the opening brace on a new line.
=head2 open_brace_and_new_line
This takes a boolean value. If true, this will instruct the formatter to insert a new line after the opening brace.
=head2 properties_as_string
Provided with an array reference of a L<CSS::Object::Property> objects and this will format them and return their string representation.
=head2 property_as_string
Provided with a L<CSS::Object::Property> object and this will format it and return its string representation.
=head2 property_separator
This sets or gets the property separator. By default, this is a new line C<\n>
If you want the formatter to put all properties on a single line, you could replace this default value with an empty string.
This returns the current character value as a L<Module::Generic::Scalar> object.
=head2 restore_parameters
Provided with an hash reference, typically that created by L</backup_parameters> and this will restore this formatter's L</parameters>
It returns our object
=head2 rule_as_string
Provided with a L<CSS::Object::Rule> object and this will format it and return its string representation.
=head2 selectors_as_string
Provided with an array reference of a L<CSS::Object::Selector> objects and this will format them and return their string representation.
=head2 value_as_string
Provided with a L<CSS::Object::Value> object and this will format it and return its string representation.
=head2 values_as_string
Provided with an array reference of a L<CSS::Object::Value> objects and this will format them and return their string representation.
=head1 AUTHOR
Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
=head1 SEE ALSO
L<CSS::Object>
=head1 COPYRIGHT & LICENSE
Copyright (c) 2020 DEGUEST Pte. Ltd.
You can use, copy, modify and redistribute this package and associated
files under the same terms as Perl itself.
=cut
( run in 1.127 second using v1.01-cache-2.11-cpan-d7f47b0818f )