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

Legend:
Removed from v.6  
changed lines
  Added in v.17

  ViewVC Help
Powered by ViewVC 1.1.26