Pod-POM-Web

 view release on metacpan or  search on metacpan

lib/Pod/POM/Web.pm  view on Meta::CPAN

  return $self->respond_html($html);
}



sub nav_links {
  my ($self, $base_url, $start_record, $end_record, $n_slice, $n_total) = @_;

  my $prev_idx  = max($start_record - $n_slice, 0);
  my $next_idx  = $start_record + $n_slice;
  my $prev_link = $start_record > 0    ? "$base_url&start=$prev_idx" : "";
  my $next_link = $next_idx < $n_total ? "$base_url&start=$next_idx" : "";

  # URI escape
  s{([^;\/?:@&=\$,A-Za-z0-9\-_.!~*'()])}
   {sprintf("%%%02X", ord($1))         }ge for $prev_link, $next_link;

  $_ += 1 for $start_record, $end_record;
  my $nav_links = "";
  $nav_links .= "<a href='$prev_link'>[Previous &lt;&lt;]</a> " if $prev_link;
  $nav_links .= "Results <b>$start_record</b> to <b>$end_record</b> "
              . "from <b>$n_total</b>";
  $nav_links .= " <a href='$next_link'>[&gt;&gt; Next]</a> " if $next_link;
  return $nav_links;
}


sub modules_matching_prefix { # called by Ajax
  my ($self, $search_string) = @_;

  $self->indexer->has_index
    or die "module list : no index";

  my @matching_modules = $self->indexer->modules_matching_prefix($search_string);
  my $json    = "[" . join(",", map {qq{"$_"}} sort @matching_modules) . "]";

  return $self->respond(content   => $json,
                        mime_type => 'application/x-json');
}



#----------------------------------------------------------------------
# updating the fulltext index
#----------------------------------------------------------------------


sub ft_index {
  my ($self, $req) = @_;

  # free the indexer
  delete $self->{indexer};

  # start another process for updating or building the fulltext index
  my $command = "index";
  $command .= "(from_scratch=>1)" if $req->parameters->get('from_scratch');
  warn "STARTING COMMAND $command\n";
  open my $pipe, '-|', qq{perl -Ilib -MPod::POM::Web -e "$command"};

  # pipe progress reports from the subprocess into the HTTP response,
  # using Plack's streaming API
  my $res = Plack::Response->new(200);
  $res->content_type('text/plain');
  $res->body($pipe);
  return $res->finalize;
}

#----------------------------------------------------------------------
# encapsulation of Plack response
#----------------------------------------------------------------------


sub respond_html {
  my ($self, $html, $mtime) = @_;

  $self->respond(content => encode_utf8($html),
                 code    => 200,
                 mtime   => $mtime,
                 charset => 'UTF-8');
}



sub respond {
  my ($self, %args) = @_;

  my $charset   = $args{charset};
  my $length    = length $args{content};
  my $mime_type = $args{mime_type} || "text/html";
     $mime_type .= "; charset=$charset" if $charset and $mime_type =~ /html/;
  my $code      = $args{code} || 200;
  my $headers   = {Content_type   => $mime_type,
                   Content_length => $length};
  $headers->{Last_modified} = gmtime($args{mtime}) if $args{mtime};
  my $r = Plack::Response->new($code, $headers, $args{content});
  $r->finalize;
}


#----------------------------------------------------------------------
# miscellaneous
#----------------------------------------------------------------------

sub mk_view {
  my ($self, %args) = @_;

  my $view = Pod::POM::View::HTML::_PerlDoc->new(
    script_name => $self->{script_name},
    css_links   => $self->css_links,
    js_scripts  => $self->js_scripts,
    %args
   );

  return $view;
}

sub find_module { 
  my ($self, $path) = @_;
  return $self->find_files(module_dirs => "$path.pod", "$path.pm",
                                          "pod/$path.pod", "pods/$path.pod");
}



( run in 0.982 second using v1.01-cache-2.11-cpan-39bf76dae61 )