/[jquery]/no_pager/index.cgi
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /no_pager/index.cgi

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 5 by dpavlin, Tue Aug 15 16:56:49 2006 UTC revision 18 by dpavlin, Wed Aug 16 23:37:51 2006 UTC
# Line 6  use CGI::Simple; Line 6  use CGI::Simple;
6  use CGI::Carp qw(fatalsToBrowser warningsToBrowser);  use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
7  use Search::Estraier;  use Search::Estraier;
8  use YAML::Syck;  use YAML::Syck;
9    use JSON::Syck;
10  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
11    
12  my $q = new CGI::Simple;  my $q = new CGI::Simple;
# Line 13  print qq{Content-type: text/html\n\r\n\r Line 14  print qq{Content-type: text/html\n\r\n\r
14    
15  my $config = LoadFile('config.yml');  my $config = LoadFile('config.yml');
16    
17    my $v = {
18            search => '',
19            hits => 0,
20            page => 0,
21            max_page => 0,
22            time => '',
23            id => time() . rand(99),
24    };
25    
26    my $json;
27    
28  #warn "config = ", dump($config);  #warn "config = ", dump($config);
29    
30  if ($q->path_info() eq '/snippet') {  sub json {
31            return
32            '<textarea id="json" style="display:none">' .
33            $q->escapeHTML( JSON::Syck::Dump( $v ) ) .
34            '</textarea>';
35    }
36    
37  print qq{  sub get_results {
38  <HTML>          my $p = {@_};
 <HEAD>  
 <META CONTENT="text/html; charset=utf-8" HTTP-EQUIV="Content-Type">  
 <META CONTENT="no-cache" HTTP-EQUIV="Pragma">  
 <META CONTENT="-1" HTTP-EQUIV="Expires">  
 </HEAD>  
   
 <div>  
         <div class="post">  
         <ul>  
 };  
39    
40  my $node = new Search::Estraier::Node(%{ $config->{estraier} });          my ($search,$page) = ( $p->{search} || '', $p->{page} || 0);
41    
42  my $o = $q->param('index') || 0;          warn "get_results( $search , $page )\n";
 my $search = $q->param('q');  
43    
44  my $on_page = 30;          sub next_page {
45  my $skip = $o * $on_page;                  return '<div id="next_page">' .
46                            join("\n", @_) . json() . '</div>';
47            }
48    
49  my $cond = new Search::Estraier::Condition;          if (! $search || $search =~ m/^\s*$/) {
50  $cond->set_phrase( $search );                  $v->{status} = 'Enter search query';
51  $cond->set_max( $on_page );                  return next_page();
52  $cond->set_skip( $skip );          }
53    
54  my $nres = $node->search($cond, 0);          if (! $page) {
55                    $v->{status} = 'Error: no page number?';
56  my $max = 0;                  return next_page();
   
 if (defined($nres)) {  
         $max = $nres->hits;  
         print "Got ", $nres->hits, " results for $search\n";  
   
         sub html_snippet {  
                 my $text = shift || return;  
                 my $out = '';  
                 foreach my $s (split(/[\n\r]{2}/, $text)) {  
                         $out .= ' ... ' if ($out);  
                         my ($pre,$hit,$post) = split(/\n/,$s,3);  
                         $hit =~ s/\t.*$//;  
                         $out .=  
                                 $q->escapeHTML( $pre || '' ) . '<b>' .  
                                 $q->escapeHTML( $hit || '' ) . '</b>' .  
                                 $q->escapeHTML( $post || '');  
                 }  
                 return $out;  
57          }          }
58    
59            $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
60            $v->{search} = $search;
61    
62          # for each document in results          $v->{page} = $page;
         for my $i ( 0 ... $nres->doc_num - 1 ) {  
63    
64                  my $rdoc = $nres->get_doc($i);          my $node = new Search::Estraier::Node(%{ $config->{estraier} });
65    
66                  print "<li>";          my $on_page = 30;
67            my $skip = ( $page - 1 ) * $on_page;
68    
69            my $cond = new Search::Estraier::Condition;
70            $cond->set_phrase( $search );
71            $cond->set_max( $on_page );
72            $cond->set_skip( $skip );
73    
74            my $nres = $node->search($cond, ( $config->{estraier}->{depth} || 0 ) );
75    
76            my $out;
77    
78            if (defined($nres)) {
79                    $v->{hits} = $nres->hits;
80                    $v->{time} = $nres->hint('TIME');
81                    $v->{max_page} = int( ($nres->hits + $on_page - 1) / $on_page );
82    
83                    $v->{status} = qq{
84                            Got <b>$v->{hits}</b> results for <tt>$v->{search}</tt>
85                            in <em>$v->{time} s</em>
86                    };
87    
88                    sub html_snippet {
89                            my $text = shift || return;
90                            my $out = '';
91                            foreach my $s (split(/[\n\r]{2}/, $text)) {
92                                    $out .= ' ... ' if ($out);
93                                    my ($pre,$hit,$post) = split(/\n/,$s,3);
94                                    $hit =~ s/\t.*$//;
95                                    $out .=
96                                            $q->escapeHTML( $pre || '' ) . '<b>' .
97                                            $q->escapeHTML( $hit || '' ) . '</b>' .
98                                            $q->escapeHTML( $post || '');
99                            }
100                            return $out;
101                    }
102    
103                    sub attr_regex {
104                            my ($rdoc,$attr) = @_;
105                            my $text = $rdoc->attr( $attr );
106                            return unless defined($text);
107    
108                            if (my $r = $config->{estraier}->{attr_regex}->{$attr} ) {
109                                    my $do = '$text =~ ' . $r . ';';
110                                    eval $do;
111                                    if ($@) {
112                                            warn "eval $do failed: $@\n";
113                                    }
114                            }
115                            return $text;
116                    }
117    
118                  print "<h1>", $rdoc->attr('@title'),"</h1>\n";                  # for each document in results
119                  print "<h2>", $rdoc->attr('source'),"</h2>\n";                  for my $i ( 0 ... $nres->doc_num - 1 ) {
120                  print "", html_snippet( $rdoc->snippet ),"<br/>\n";  
121                  print "[", $skip + $i, "] ";                          my $rdoc = $nres->get_doc($i);
122                  print "<tt>", $rdoc->attr('@uri'),"</tt>";  
123                  print "</li>";                          $out .= '<div class="item">' .
124                                    '<h1>' . $rdoc->attr('@title') . '</h1>' .
125                                    '<p>' . html_snippet( $rdoc->snippet ) . '</p>' .
126                                    '<h2>' . attr_regex( $rdoc, 'source' ) . '</h2>';
127                            my $uri = attr_regex( $rdoc, '@uri' );
128                            $out .=
129                                    qq{<a href="$uri"><tt>$uri</tt></a> } .
130                                    attr_regex( $rdoc, '@mdate' ) .
131                                    ' [' . ( $skip + $i + 1 ) . ']';
132                    }
133    
134            } else {
135                    $out .= 'error: ' . $node->status;
136          }          }
 } else {  
         die "error: ", $node->status,"\n";  
 }  
137    
138            if ($v->{page} == $v->{max_page}) {
139                    $out .= next_page('<strong>All results shown</strong>');
140            } else {
141                    $out .= next_page(
142                            '<strong>Loading results...</strong><br/>',
143                            'If you are using the scroll bar, release the mouse to see more results.'
144                    );
145            }
146    
147  print qq{          return $out;
         </ul>  
         </div>  
148    
149  </div>  }
150    
151    if ($q->path_info() eq '/snippet') {
152    
153            print get_results(
154                    search => $q->param('search'),
155                    page => $q->param('page'),
156            );
157    
 </html>  
 };  
158    
159  } else {  } else {
160    
161          sub page_id {          my $get_results = get_results(
162                  my $page_id = time() . rand(99);                  search => $q->param('search'),
163                  warn "page_id = $page_id\n";                  page => 1,
164                  return $page_id;          );
         };  
165    
166          my $f = $q->path_info;          my $f = $q->path_info;
167          $f =~ s/\W+//g;          $f =~ s/\W+//g;
# Line 109  print qq{ Line 169  print qq{
169          $f .= '.html';          $f .= '.html';
170          open(my $s, $f)  || die "$f: $!";          open(my $s, $f)  || die "$f: $!";
171          while(<$s>) {          while(<$s>) {
                 no strict 'vars';  
172                  s/<%(.+?)%>/eval "$1"/ge;                  s/<%(.+?)%>/eval "$1"/ge;
173                  print;                  print;
174          }          }

Legend:
Removed from v.5  
changed lines
  Added in v.18

  ViewVC Help
Powered by ViewVC 1.1.26