ASP4

 view release on metacpan or  search on metacpan

lib/ASP4/PageParser.pm  view on Meta::CPAN


package
ASP4::PageParser;

use strict;
use warnings 'all';
use ASP4::ConfigLoader;
use ASP4::Page;
use ASP4::MasterPage;


sub new
{
  my ($class, %args) = @_;
  
  my $s = bless {
    script_name   => $args{script_name},
    filename      => undef,
    package       => undef,
    compiled_as   => undef,
    base_class    => undef,
    source_code   => \"",
  }, $class;
  $s->_init();
  
  return $s;
}# end new()

sub source_code { shift->{source_code} }


sub _init
{
  my $s = shift;
  
  my $config = ASP4::ConfigLoader->load();
  my $filename = $config->web->www_root . $s->{script_name};
  (my $package = $s->{script_name}) =~ s/[^a-z0-9]/_/ig;
  $package = $config->web->application_name . '::' . $package;
  (my $compiled_as = "$package.pm") =~ s/::/\//g;
  
  # What we know so far:
  $s->{filename}    = $filename;
  $s->{package}     = $package;
  $s->{compiled_as} = $compiled_as;
  $s->{saved_to}    = $config->web->page_cache_root . "/$compiled_as";
}# end _init()


sub parse
{
  my $s = shift;
  
  # Open up the file:
  open my $ifh, '<', $s->{filename}
    or die "Cannot open '$s->{filename}' for reading: $!";
  local $/;
  $s->{source_code} = \scalar(<$ifh>);
  
  my $directives = $s->_get_directives;
  if( my $master_uri = $directives->{page}->{usemasterpage} )
  {
    $s->{masterpage} = ASP4::PageLoader->load( script_name => $master_uri );
    $s->{base_class} = $s->{masterpage}->{package};
  }
  elsif( $directives->{masterpage} )
  {
    $s->{base_class} = 'ASP4::MasterPage';
  }
  else
  {
    $s->{base_class} = 'ASP4::Page';
  }# end if()
  
  $s->_parse_scriptlet_tags;
  $s->_parse_include_tags;
  my $ref = $s->source_code;
  
  # The <asp:ContentPlaceHolder ...>...</asp:ContentPlaceHolder> tags:
  my $ident = 0;
  my @placeholder_tags = ( );

lib/ASP4/PageParser.pm  view on Meta::CPAN

  $$ref =~ s/(\$Response\->End)/return $1/gs;
  
  $$ref = ';$Response->Write(q~' . $$ref . '~);';
  
  # Now do the final ~ substitution:
  $$ref =~ s{(\(q~)(.*?)(~\);)}{
    my $pre = $1;
    my $post = $3;
    (my $txt = $2) =~ s/~/\\~/g;
    "$pre$txt$post"
  }xsge;
}# end _parse_scriptlet_tags()


sub _get_directives
{
  my ($s) = @_;
  
  my $ref = $s->source_code;
  my %directives = ( );
  while( my ($tag, $directive, $attr_str) = $$ref =~ m/(<%@\s*(.*?)\s+(.*?)\s*%>)/ )
  {
    my $attrs = $s->_parse_tag_attrs( $attr_str );
    $$ref =~ s/\Q$tag\E//;
    $directives{ lc($directive) } = $attrs;
  }# end while()
  
  $directives{page} ||= { };
  
  return \%directives;
}# end _get_directives()


sub _parse_tag_attrs
{
  my ($s, $str) = @_;
  
  my $attr = { };
  while( $str =~ m@([^\s\=\"\']+)(\s*=\s*(?:(")(.*?)"|(')(.*?)'|([^'"\s=]+)['"]*))?@sg ) #@
  {
    my $key = $1;
    my $test = $2;
    my $val  = ( $3 ? $4 : ( $5 ? $6 : $7 ));
    if( $test )
    {
      $attr->{ lc($key) } = $val;
    }
    else
    {
      $attr->{ lc($key) } = $key;
    }# end if()
  }# end while()
  
  return $attr;
}# end _parse_tag_attrs()


sub DESTROY
{
  my $s = shift;
  undef(%$s);
}# end DESTROY()

1;# return true:



( run in 1.547 second using v1.01-cache-2.11-cpan-d80b1682f3f )