/[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 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>          }
         <div class="post">  
         <ul>  
 };  
42    
43  my $node = new Search::Estraier::Node(%{ $config->{estraier} });          $search = join(" AND ", split(/\s+/, $search)) unless ($search =~ m/(?:AND|OR|\[|\])/);
44            $v->{search} = $search || '';
45    
46  my $o = $q->param('index') || 0;          $v->{page} = $page;
 my $search = $q->param('q');  
47    
48  my $on_page = 30;          my $node = new Search::Estraier::Node(%{ $config->{estraier} });
 my $skip = $o * $on_page;  
49    
50  my $cond = new Search::Estraier::Condition;          my $on_page = 30;
51  $cond->set_phrase( $search );          my $skip = ( $page - 1 ) * $on_page;
52  $cond->set_max( $on_page );  
53  $cond->set_skip( $skip );          my $cond = new Search::Estraier::Condition;
54            $cond->set_phrase( $search );
55  my $nres = $node->search($cond, ( $config->{estraier}->{depth} || 0 ) );          $cond->set_max( $on_page );
56            $cond->set_skip( $skip );
57  my $max = 0;  
58            my $nres = $node->search($cond, ( $config->{estraier}->{depth} || 0 ) );
59  if (defined($nres)) {  
60          $max = $nres->hits;          my $out;
61          print "Got ", $nres->hits, " results for $search\n";  
62            if (defined($nres)) {
63          sub html_snippet {                  $v->{hits} = $nres->hits;
64                  my $text = shift || return;                  $v->{time} = $nres->hint('TIME');
65                  my $out = '';                  $v->{max_page} = int( ($nres->hits + $on_page - 1) / $on_page );
66                  foreach my $s (split(/[\n\r]{2}/, $text)) {  
67                          $out .= ' ... ' if ($out);                  $v->{status} = qq{
68                          my ($pre,$hit,$post) = split(/\n/,$s,3);                          Got <b>$v->{hits}</b> results for <tt>$v->{search}</tt>
69                          $hit =~ s/\t.*$//;                          in <em>$v->{time} s</em>
70                          $out .=                  };
71                                  $q->escapeHTML( $pre || '' ) . '<b>' .  
72                                  $q->escapeHTML( $hit || '' ) . '</b>' .                  sub html_snippet {
73                                  $q->escapeHTML( $post || '');                          my $text = shift || return;
74                            my $out = '';
75                            foreach my $s (split(/[\n\r]{2}/, $text)) {
76                                    $out .= ' ... ' if ($out);
77                                    my ($pre,$hit,$post) = split(/\n/,$s,3);
78                                    $hit =~ s/\t.*$//;
79                                    $out .=
80                                            $q->escapeHTML( $pre || '' ) . '<b>' .
81                                            $q->escapeHTML( $hit || '' ) . '</b>' .
82                                            $q->escapeHTML( $post || '');
83                            }
84                            return $out;
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 "", html_snippet( $rdoc->snippet ),"<br/>\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 109  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.6  
changed lines
  Added in v.15

  ViewVC Help
Powered by ViewVC 1.1.26