=head1 NAME

Parse::Template::Base - 

=head1 SYNOPSIS



=head1 EXPORTS

Nothing exported by default

=over



=back

=head1 DEPENDENCIES

This module requires these other modules and libraries:

    Data::Hub::Courier
    Parse::Padding
    Parse::Template::Arguments
    Error::Logical
    Error::Programatic
    Perl::Module
    Data::Hub::Util
    Parse::Template::ArgumentStack
    Algorithm::KeyGen

=head1 DESCRIPTION



=head1 EXAMPLES

This example:

  # Simple replacement
  use Parse::Template::Standard;
  my $t = 'Hello [#name]';
  my $p = new Parse::Template::Standard();
  my $o = $p->compile_text(\$t, {name => 'World'});
  return $$o;



I<will return>:

  Hello World


This example:

  # Variable expansion
  use Parse::Template::Standard;
  my $t = 'Hello [#name]';
  my $p = new Parse::Template::Standard();
  my $o = $p->compile_text(\$t, {name => '[#next]', next => 'World'});
  return $$o;



I<will return>:

  Hello World


This example:

  # Nested elements
  use Parse::Template::Standard;
  my $t = 'Hello [#data/name]';
  my $p = new Parse::Template::Standard();
  my $o = $p->compile_text(\$t, {data => {name => 'World'}});
  return $$o;



I<will return>:

  Hello World


This example:

  # Dynamic expansion
  use Parse::Template::Standard;
  my $t = 'Hello [#[#name]]';
  my $p = new Parse::Template::Standard();
  my $o = $p->compile_text(\$t, {name => 'next', next => 'World'});
  return $$o;



I<will return>:

  Hello World



=head1 PUBLIC INTERFACE

=head2 new

=over

=item Constructor




options:


  -begin => $begin_str          # Begins match (default '[#')
  -end => $end_str              # Ends match (default ']')
  -close => $close_str          # Closes a block (default 'end')
  -directive => $directive_str  # Identifies a directive (default ':')
  -out => \$output              # Where output is written



=back


=head2 Default_Context

=over

=item Default (initial) context






=back


=head2 set_directives

=over

=item Set the directive handlers for each given name-value pair






=back


=head2 set_directive

=over

=item Set the directive handler for a given name-value pair






=back


=head2 get_directive

=over

=item Get a directive handler



where:

  $name   directive name
  $idx    0 for begin, 1 for end



=back


=head2 compile

=over

=item Main invokation method






=back


=head2 compile_text

=over

=item Main invokation method






=back


=head2 get_ctx

=over

=item Get the current context from the context stack






=back


=head2 use

=over

=item Add a namespace on to the local stack



Calls to use must match calls to un-use within the same context



=back


=head2 unuse

=over

=item Remove the last used namespace from the local stack



Calls to use must match calls to un-use within the same context



=back


=head2 eval

=over

=item Evaluate the given element






=back


=head2 eval_fields

=over

=item Walk through fields locically returning the [uncompiled] value




This routine does the work of processing the variable spec with consideration
of the `x ? y : z`, `&&` and `||` operations. For instance:


 a
 a || b
 t ? a : b
 t ? f || a : b && c
 f ? f || c : b && a

See also: `src/test/perl/t-eval-fields.pl`



=back


=head2 get_compiled_value

=over

=item Get a variable as a string and compile if necessary






=back


=head2 get_value_str

=over

=item Get a variable value as a string



See also: L</get_value>



=back


=head2 value_to_string

=over

=item Get the string representation of a value






=back


=head2 get_value

=over

=item Get a variable value



C<$addr> is passed by-reference because we will update it when applying
local path information.



=back


=head2 end_pos

=over

=item Find the position of the corresponding end directive




All directives nested SHOULD be either inline (they do not slurp) or blocks 
(they have an end directive).

All nested directives MUST also have their own end directive. This is not 
currently the case, so we do our best. Take this scenario:


  [#:set foo]
    This is a block
  [#:end set]

Which is just fine. However, this is difficult:


  [#:set foo]
    [#:set bar = 'Bar']
    This is a block
  [#:end set]

Because we don't know that the nested `:set` directive is inline. So, we ask
for the `$to_eof` parameter. If this is true, then we will return the normal
end position, that is -1 when nested occurences are not paired up. Otherwise
we will return the end position of the *last* corresponding end directive.
This is the best we can do without more information, and passing or garnering
that information is considered too expensive an operation at this level.

Provisions have been made only considering the `:set` and `:into` directives. 
-Ryan 11/2012



=back


=head2 block_pos

=over

=item Return the begin and end block positions



Similar in spirit to L</substr_pos> except this routine uses regular
expressions to ensure a nexted '[#abcd' does not count as a nested '[#ab' and
this routine does not track all sub-indexes.



=back


=head2 substr_pos

=over

=item Return positions of begin and end markers




This routine recognizes C<$begin_marker> and C<$end_marker> as a balanced
pair.

Example 1:


  $begin_marker = [#
  $end_marker   = ]
  a [#b [#c] [#d]] e
    ^            ^
  returns [[2, 15]]

Example 2:


  $begin_marker = [#if
  $end_marker   = [#end if]
  [#if true]do something[#end if]
  ^                     ^
  returns [[0, 22]]


This example:


  use Parse::Template::Base;
  my $p = new Parse::Template::Base;
  my $text = '<<>>';
  my @pos = $p->substr_pos(\$text, '<', '>', 0);
  join ',', map {join '-', @$_} @pos;



I<will return>:

  0-3,1-2



=back


=head1 PACKAGE INTERNALS

=head2 _create_context

=over

=item Add a new context on to the context stack



where C<%args> can be any combination of:

  text      => \$text,    # Template text
  path      => $path,     # Template path
  name      => $name,     # Template name
  out       => \$out,     # Where to write the result
  scope     => $scope,    # Stack index limit (invokation is being deferred)
  regions   => \@regions, # Known regions (\$text is being re-used)
  esc       => $chars     # Escape chars in resolved values



=back


=head2 _find_regions

=over

=item Glean the begin and end indexes into template text



returns:

  [\@match1, \@match2, ...]
where each C<\@match> is:

  [
    $beg,       # Begin index
    $end,       # End index
    $nested,    # Has nested regions (1|undef)
  ]



=back


=head2 _remove_context

=over

=item Remove the current context from the context stack






=back


=head2 _invoke

=over

=item Implementation method



See _create_context for C<%args>



=back


=head2 _slurp

=over

=item Slurp the contents of for block-type directive




If C<$to_eof> is a true value and an end directive is not found, C<_slurp>
will read to the end of the template text.

If C<$to_eol> is not defined, it is true, which means the newlines after
the closing tag will be consumed.



=back


=head2 _padding

=over

=item Get number of preceeding and trailing whitespace characters




Returns an array of widths: ($w1, $w2)

  $w1 = Number of preceeding whitespace characters
  $w2 = Number of trailing whitespace characters

Returns an (0, 0) if non-whitespace characters are immediately found in the 
preceeding or trailing regions.

We will look up to 80 characters in front of the current position.



=back



=head1 AUTHORS

    Ryan Gies <ryangies@cpan.org>

=head1 COPYRIGHT

    Copyright (C) 2014-2016 by Ryan Gies. All rights reserved.
    Copyright (C) 2006-2013 by Livesite Networks, LLC. All rights reserved.
    Copyright (C) 2000-2005 by Ryan Gies. All rights reserved.
    Redistribution and use in source and binary forms, with or without 
    modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright notice, 
    this list of conditions and the following disclaimer.
    * The origin of this software must not be misrepresented; you must not 
    claim that you wrote the original software. If you use this software in a 
    product, an acknowledgment in the product documentation would be 
    appreciated but is not required.
    * Altered source versions must be plainly marked as such, and must not be 
    misrepresented as being the original software.
    * The name of the author may not be used to endorse or promote products 
    derived from this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED 
    WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
    MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
    EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
    OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
    OF SUCH DAMAGE.
    To the best of our knowledge, no patented algorithms have been used. However, we
    do not have the resources to carry out a patent search, and therefore cannot 
    give any guarantee of the above statement.

=cut

