/[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 10 by dpavlin, Wed Aug 16 01:02:00 2006 UTC revision 15 by dpavlin, Wed Aug 16 21:34:37 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 get_results {
31            my ($search, $page) = @_;
32    
33  print qq{          if (! $search) {
34  <HTML>                  $v->{status} = 'Enter search query';
35  <HEAD>                  return;
36  <META CONTENT="text/html; charset=utf-8" HTTP-EQUIV="Content-Type">          }
37  <META CONTENT="no-cache" HTTP-EQUIV="Pragma">  
38  <META CONTENT="-1" HTTP-EQUIV="Expires">          if (! $page) {
39  </HEAD>                  $v->{status} = 'Error: no page number?';
40                    return;
41  <div>          }
42          <div class="post">  
43          <ul>          $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
44  };          $v->{search} = $search || '';
45    
46            $v->{page} = $page;
47    
48  my $node = new Search::Estraier::Node(%{ $config->{estraier} });          my $node = new Search::Estraier::Node(%{ $config->{estraier} });
49    
50  my $o = $q->param('index') || 0;          my $on_page = 30;
51  my $search = $q->param('q');          my $skip = ( $page - 1 ) * $on_page;
52  $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);  
53            my $cond = new Search::Estraier::Condition;
54  my $on_page = 30;          $cond->set_phrase( $search );
55  my $skip = $o * $on_page;          $cond->set_max( $on_page );
56            $cond->set_skip( $skip );
57  my $cond = new Search::Estraier::Condition;  
58  $cond->set_phrase( $search );          my $nres = $node->search($cond, ( $config->{estraier}->{depth} || 0 ) );
59  $cond->set_max( $on_page );  
60  $cond->set_skip( $skip );          my $out;
61    
62  my $nres = $node->search($cond, ( $config->{estraier}->{depth} || 0 ) );          if (defined($nres)) {
63                    $v->{hits} = $nres->hits;
64  my $max = 0;                  $v->{time} = $nres->hint('TIME');
65                    $v->{max_page} = int( ($nres->hits + $on_page - 1) / $on_page );
66  if (defined($nres)) {  
67          $max = $nres->hits;                  $v->{status} = qq{
68          my $time = $nres->hint('TIME');                          Got <b>$v->{hits}</b> results for <tt>$v->{search}</tt>
69          print qq{                          in <em>$v->{time} s</em>
70                  <div id="status_update" style="display:none;">                  };
71                          Got <b>$max</b> results for <tt>$search</tt> in <em>$time s</em>  
72                  </div>                  sub html_snippet {
73          };                          my $text = shift || return;
74                            my $out = '';
75          sub html_snippet {                          foreach my $s (split(/[\n\r]{2}/, $text)) {
76                  my $text = shift || return;                                  $out .= ' ... ' if ($out);
77                  my $out = '';                                  my ($pre,$hit,$post) = split(/\n/,$s,3);
78                  foreach my $s (split(/[\n\r]{2}/, $text)) {                                  $hit =~ s/\t.*$//;
79                          $out .= ' ... ' if ($out);                                  $out .=
80                          my ($pre,$hit,$post) = split(/\n/,$s,3);                                          $q->escapeHTML( $pre || '' ) . '<b>' .
81                          $hit =~ s/\t.*$//;                                          $q->escapeHTML( $hit || '' ) . '</b>' .
82                          $out .=                                          $q->escapeHTML( $post || '');
83                                  $q->escapeHTML( $pre || '' ) . '<b>' .                          }
84                                  $q->escapeHTML( $hit || '' ) . '</b>' .                          return $out;
                                 $q->escapeHTML( $post || '');  
85                  }                  }
                 return $out;  
         }  
86    
87                    sub attr_regex {
88                            my ($rdoc,$attr) = @_;
89                            my $text = $rdoc->attr( $attr );
90                            return unless defined($text);
91    
92                            if (my $r = $config->{estraier}->{attr_regex}->{$attr} ) {
93                                    my $do = '$text =~ ' . $r . ';';
94                                    eval $do;
95                                    if ($@) {
96                                            warn "eval $do failed: $@\n";
97                                    }
98                            }
99                            return $text;
100                    }
101    
102          # for each document in results                  # for each document in results
103          for my $i ( 0 ... $nres->doc_num - 1 ) {                  for my $i ( 0 ... $nres->doc_num - 1 ) {
104    
105                  my $rdoc = $nres->get_doc($i);                          my $rdoc = $nres->get_doc($i);
106    
107                  print "<li>";                          $out .= '<div class="item">' .
108                                    '<h1>' . $rdoc->attr('@title') . '</h1>' .
109                                    '<p>' . html_snippet( $rdoc->snippet ) . '</p>' .
110                                    '<h2>' . attr_regex( $rdoc, 'source' ) . '</h2>';
111                            my $uri = attr_regex( $rdoc, '@uri' );
112                            $out .=
113                                    qq{<a href="$uri"><tt>$uri</tt></a> } .
114                                    attr_regex( $rdoc, '@mdate' ) .
115                                    ' [' . ( $skip + $i + 1 ) . ']';
116                    }
117    
118                  print "<h1>", $rdoc->attr('@title'),"</h1>\n";          } else {
119                  print "<h2>", $rdoc->attr('source'),"</h2>\n";                  $out .= 'error: ' . $node->status;
                 print "<h3>", $rdoc->attr('@mdate'),"</h3>\n";  
                 print "<p>", html_snippet( $rdoc->snippet ),"</p>\n";  
                 print "[", $skip + $i, "] ";  
                 print "<tt>", $rdoc->attr('@uri'),"</tt>";  
                 print "</li>";  
120          }          }
 } else {  
         die "error: ", $node->status,"\n";  
 }  
121    
122            $json = '<textarea id="json" style="display:none">' .
123                    $q->escapeHTML( JSON::Syck::Dump( $v ) ) .
124                    '</textarea>';
125    
126  print qq{          return ($out,$json);
         </ul>  
         </div>  
127    
128  </div>  }
129    
130    if ($q->path_info() eq '/snippet') {
131    
132            print join("\n<!-- json data -->",
133                    get_results(
134                            $q->param('search'),
135                            $q->param('page'),
136                    )
137            );
138    
 </html>  
 };  
139    
140  } else {  } else {
141    
142          sub page_id {          my ($get_results, $json) = get_results( $q->param('search'), 1 );
                 my $page_id = time() . rand(99);  
                 warn "page_id = $page_id\n";  
                 return $page_id;  
         };  
143    
144          my $f = $q->path_info;          my $f = $q->path_info;
145          $f =~ s/\W+//g;          $f =~ s/\W+//g;
# Line 116  print qq{ Line 147  print qq{
147          $f .= '.html';          $f .= '.html';
148          open(my $s, $f)  || die "$f: $!";          open(my $s, $f)  || die "$f: $!";
149          while(<$s>) {          while(<$s>) {
                 no strict 'vars';  
150                  s/<%(.+?)%>/eval "$1"/ge;                  s/<%(.+?)%>/eval "$1"/ge;
151                  print;                  print;
152          }          }

Legend:
Removed from v.10  
changed lines
  Added in v.15

  ViewVC Help
Powered by ViewVC 1.1.26