From 3fcca93c1391c570e71a81f9467b95d7fcd1910e Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 12 Sep 2015 01:23:31 +0000 Subject: searchview: hoist out subroutines for clarity We'll be expanding the search view to handle expanded views. --- lib/PublicInbox/SearchView.pm | 93 +++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 38 deletions(-) (limited to 'lib/PublicInbox/SearchView.pm') diff --git a/lib/PublicInbox/SearchView.pm b/lib/PublicInbox/SearchView.pm index 13f4de2a..82b97f32 100644 --- a/lib/PublicInbox/SearchView.pm +++ b/lib/PublicInbox/SearchView.pm @@ -38,52 +38,19 @@ sub sres_top_html { my $foot = $ctx->{footer} || ''; $foot = qq{Back to index.}; if ($err) { - my $u = 'http://xapian.org/docs/queryparser.html'; $code = 400; - $err =~ s/^\s*Exception:\s*//; # bad word to show users :P - $err = PublicInbox::Hval->new_oneline($err)->as_html; - $res .= "\n\nBad query: $err\n"; - $res .= qq{See $u for Xapian query syntax}; - $res .= "
$foot";
+		$res .= err_txt($err) . "

$foot";
 	} elsif ($total == 0) {
 		$code = 404;
 		$res .= "\n\n[No results found]

$foot";
 	} else {
 		$q = $query->as_href;
 		$q =~ s/%20/+/g; # improve URL readability
-		my $qp = "?q=$q";
-		$qp .= "&o=$o" if $o;
+		$res .= search_nav_top($q, $o, $r);
+		$res .= "\n\n";
 
-		$res .= "Search results ordered by [";
-		if ($r) {
-			$res .= qq{date|relevance};
-		} else {
-			$qp .= '&r';
-			$res .= qq{date|relevance};
-		}
-		$res .= "]\n\n";
-
-		dump_mset(\$res, $mset);
-		my $nr = scalar $mset->items;
-		my $end = $o + $nr;
-		my $beg = $o + 1;
-		$res .= "
";
-		$res .= "Results $beg-$end of $total";
-
-		my $n = $o + $LIM;
-		if ($n < $total) {
-			$qp = "q=$q&o=$n";
-			$qp .= "&r" if $r;
-			$res .= qq{, next}
-		}
-		if ($o > 0) {
-			$res .= $n < $total ? '/' : ',      ';
-			my $p = $o - $LIM;
-			$qp = "q=$q";
-			$qp .= "&o=$p" if $p > 0;
-			$qp .= "&r" if $r;
-			$res .= qq{prev};
-		}
+		dump_mset(\$res, $mset, $o);
+		$res .= search_nav_bot($mset, $q, $o, $r);
 		$res .= "\n\n" . $foot;
 	}
 
@@ -112,4 +79,54 @@ sub dump_mset {
 	}
 }
 
+sub err_txt {
+	my ($err) = @_;
+	my $u = 'http://xapian.org/docs/queryparser.html';
+	$err =~ s/^\s*Exception:\s*//; # bad word to show users :P
+	$err = PublicInbox::Hval->new_oneline($err)->as_html;
+	"\n\nBad query: $err\n" .
+		qq{See $u for Xapian query syntax};
+}
+
+sub search_nav_top {
+	my ($q, $o, $r) = @_;
+	my $qs = "q=$q";
+	$qs .= "&o=$o" if $o;
+
+	my $rv = "Search results ordered by [";
+	if ($r) {
+		$rv .= qq{date|relevance};
+	} else {
+		$qs .= '&r';
+		$rv .= qq{date|relevance};
+	}
+	$rv .= ']';
+}
+
+sub search_nav_bot {
+	my ($mset, $q, $o, $r) = @_;
+	my $total = $mset->get_matches_estimated;
+	my $nr = scalar $mset->items;
+	my $end = $o + $nr;
+	my $beg = $o + 1;
+
+	my $rv = "
Results $beg-$end of $total";
+
+	my $n = $o + $LIM;
+	if ($n < $total) {
+		my $qs = "q=$q&o=$n";
+		$qs .= "&r" if $r;
+		$rv .= qq{, next}
+	}
+	if ($o > 0) {
+		$rv .= $n < $total ? '/' : ',      ';
+		my $p = $o - $LIM;
+		my $qs = "q=$q";
+		$qs .= "&o=$p" if $p > 0;
+		$qs .= "&r" if $r;
+		$rv .= qq{prev};
+	}
+	$rv;
+}
+
 1;
-- 
cgit v1.2.3-24-ge0c7