package Text::Wigwam::Library::Filter;

    use strict;
    use warnings;
    our $VERSION = '0.01';

    use base q/Exporter/;

    use Text::Wigwam::Const( qw/ NUM STRING / );

    our %EXPORT_TAGS = (
        all => [
          qw/
            _proto_format _format
            _proto_indent _indent
          /
        ],
        format => [ qw/ _proto_format _format / ],
        indent => [ qw/ _proto_indent _indent / ],
    );
    our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
    our @EXPORT = qw( );

=head1 Name

Text::Wigwam::Library::Misc - A Wigwam directive library.

=head1 Description

This library provides filter directives.

=head1 Usage

The directives within this library can be imported into specific branches of
the directive tree (which transcends from  C<Text::Wigwam::Directives>) via
plug-ins or modules using one of the following techniques.

 package Text::Wigwam::Directives::some::arbitrary::branch;
 # import everything
 use Text::Wigwam::Library::Filter qw/:all/;

 package Text::Wigwam::Directives::another::arbitrary::branch;
 # import selectively
 use Text::Wigwam::Library::Filter qw/ :indent /;

=head1 Directives

=cut

sub _proto_format { [ STRING, STRING ] }
sub _format {
    my ($Api) = @_;
    my $format = $Api->get_arg;
    my $text   = $Api->get_arg;
    $format = '%s'
      unless length $format;
    local $_;
    return join(
        "\n",
        map{ sprintf($format, $_) } split(/\n/, $text)
    );
}

=head2 #format I<string1> I<string2>

Formats a block of text specified by I<string2> using sprintf style format specified by I<string2>.

 [!!
    #format '%04d' ~!>
        0
        10
        200
        3000
    <!~
    /*
     0000
     0010
     0200
     3000
    */
 !!]

=cut

sub _proto_indent { [ NUM, STRING ] }
sub _indent {
    my ($Api) = @_;
    my $pad = $Api->get_arg;
    my $text = $Api->get_arg;
    return $text unless $pad;
    $pad = ' ' x $pad;
    $text =~ s/^/$pad/mg;
    return $text;
}

=head2 #indent I<num> I<string>

Indents a block of text specified by I<string> by I<num> spaces.

 [!!
  #indent 4 "Your words"
  /*
    Your words
  */
 !!]

=cut

1;
