Skip to content

Commit 81cf528

Browse files
kfly8claude
andcommitted
検索機能を改善: /searchエンドポイントと2つのボタンによる検索を追加
- /searchエンドポイントを追加(検索クエリを/$qにリダイレクト) - 検索フォームを「サイト内検索」と「Google検索」の2つのボタンに分離 - 404ページで検索結果が見つからない場合のGoogle検索リンクを追加 - 検索フォームのCSSスタイルを調整(横並び表示) - テストケースを新しい検索動作に合わせて更新 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 5e4bc86 commit 81cf528

File tree

6 files changed

+70
-21
lines changed

6 files changed

+70
-21
lines changed

lib/PJP/Web/Dispatcher.pm

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,25 @@ get '/docs/perl/{path:.[^/]+\.pod}' => sub { # perl
442442

443443
get '/docs/{path:(?:modules|perl|articles)/.+\.pod}' => $display_pod;
444444

445+
# GET /search?q=%s でドキュメント検索を行う
446+
# 設置理由:
447+
# - 検索フォームで利用しやすくするため
448+
# - サイト内検索のショートカットを設定を案内しやすくするため
449+
# Ref: https://support.google.com/chrome/answer/95426?hl=ja#zippy=%2Curls%E6%A4%9C%E7%B4%A2%E8%AA%9E%E5%8F%A5-%E6%AC%84
450+
get '/search' => sub {
451+
my ($c) = @_;
452+
453+
my $q = $c->req->param('q');
454+
455+
if (!$q) {
456+
return $c->res_404();
457+
}
458+
459+
return $c->redirect("/$q");
460+
};
461+
462+
# 以下、ルーティングルールを用いて、ドキュメント検索を行っている
463+
445464
get '/perl*' => sub {
446465
my ($c, $p) = @_;
447466
my ($splat) = @{$p->{splat}};
@@ -469,7 +488,7 @@ get '/{name:[A-Z-a-z][\w:]+}' => sub {
469488
return $c->redirect('/docs/' . $path);
470489
}
471490

472-
return $c->redirect('/func/' . $p->{name});
491+
return $c->res_404({ query => $p->{name} })
473492
};
474493

475494
1;

scss/_class.scss

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@
3333
}
3434
}
3535
form {
36+
display: inline-block;
37+
vertical-align: middle;
38+
}
39+
.search-container {
3640
position: absolute;
3741
right: $gap;
3842
top: 10px;
39-
4043
}
4144
}
4245

static/css/screen.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,10 @@ dd {
631631
}
632632
/* line 35, ../../scss/_class.scss */
633633
.nav form {
634+
display: inline-block;
635+
vertical-align: middle;
636+
}
637+
.nav .search-container {
634638
position: absolute;
635639
right: 7.292%;
636640
top: 10px;

t/endpoints.t

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -332,24 +332,45 @@ subtest 'perldoc.jp/$VALUE のように指定したら、よしなにリダイ
332332
};
333333
};
334334

335-
subtest '/{name} - いずれにも該当しなかった場合、最新ドキュメントか組み込み関数のページへリダイレクトする' => sub {
336-
subtest '/Acme::Bleach は、/docs/modules/Acme-Bleach-1.12/Bleach.pod にリダイレクトされる' => sub {
335+
subtest '/{name} - いずれにも該当しなかった場合、404が返る' => sub {
336+
subtest '/Acme::Bleach は、/docs/modules/Acme-Bleach-*.*/Bleach.pod にリダイレクトされる' => sub {
337337
$mech->get('/Acme::Bleach');
338338
is $mech->status, 200, 'status is 200';
339339
like $mech->title, qr/^Acme::Bleach/;
340340

341-
$mech->base_like(qr{/docs/modules/Acme-Bleach-1.12/Bleach.pod$});
341+
$mech->base_like(qr{/docs/modules/Acme-Bleach-\d+\.\d+/Bleach.pod$});
342342
};
343343

344-
subtest '/fuga は、/func/fuga にリダイレクトされる' => sub {
344+
subtest '/fuga は、404が返る' => sub {
345345
$mech->get('/fuga');
346346
is $mech->status, 404, 'status is 404';
347-
is $mech->title, "'fuga' は Perl の組み込み関数ではありません。 - perldoc.jp";
348-
349-
$mech->base_like(qr{/func/fuga$});
350-
$mech->text_contains("'fuga' は Perl の組み込み関数ではありません。");
347+
$mech->content_contains('fuga');
348+
$mech->content_contains('検索結果が見つかりませんでした');
351349
};
352350
};
353351
};
354352

353+
subtest 'GET /search' => sub {
354+
subtest '検索クエリが指定された場合、/$qへリダイレクトされる' => sub {
355+
# /search?q=chomp -> /chomp -> /func/chomp
356+
$mech->get('/search?q=chomp');
357+
is $mech->status, 200, 'status is 200';
358+
is $mech->title, 'Perlの組み込み関数 chomp の翻訳 - perldoc.jp';
359+
$mech->base_like(qr{/func/chomp$});
360+
};
361+
362+
subtest 'モジュール名を検索した場合も/$qへリダイレクトされる' => sub {
363+
# /search?q=Acme::Bleach -> /Acme::Bleach -> /docs/modules/...
364+
$mech->get('/search?q=Acme::Bleach');
365+
is $mech->status, 200, 'status is 200';
366+
like $mech->title, qr/^Acme::Bleach/;
367+
$mech->base_like(qr{/docs/modules/Acme-Bleach-\d+\.\d+/Bleach\.pod$});
368+
};
369+
370+
subtest 'qパラメータがない場合、404が返る' => sub {
371+
$mech->get('/search');
372+
is $mech->status, 404, 'status is 404';
373+
};
374+
};
375+
355376
done_testing;

tmpl/404.tt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
[% IF is_hash_ref(message) && message.package %]
33
[% message.package %]はみつかりませんでした。
44
<div>
5-
<a href="http://search.cpan.org/dist/[% message.package.replace('::','-') %]">CPANで見る</a><br />
65
<a href="http://metacpan.org/module/[% message.package %]">MetaCPANで見る</a>
76
</div>
7+
[% ELSIF is_hash_ref(message) && message.query %]
8+
「[% message.query %]」の検索結果が見つかりませんでした。
9+
<div>
10+
<a href="https://www.google.co.jp/search?q=[% uri_escape('site:perldoc.jp ' _ message.query) %]">Google でサイト内を検索する</a>
11+
</div>
812
[% ELSE %]
913
[% message || '該当のページは見つかりませんでした。' %]
1014
[% END %]

tmpl/layout.html

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,15 @@
5757
<li><a href="/about">このサイトについて</a></li>
5858
</ul>
5959

60-
<!-- SiteSearch Google -->
61-
<form method=get action="http://www.google.co.jp/search" id="GoogleSearch">
62-
<input type=text name=q size=31 maxlength=255 value="">
63-
<input type=hidden name=ie value=UTF-8>
64-
<input type=hidden name=oe value=UTF-8>
65-
<input type=hidden name=hl value="ja">
66-
<input type=hidden name=domains value="perldoc.jp">
67-
<input type="hidden" name="sitesearch" value="https://perldoc.jp">
68-
<input type=submit name=btnG value="Google 検索">
60+
<!-- SiteSearch -->
61+
<div class="search-container">
62+
<form method="get" action="/search" id="search-form">
63+
<input type="text" name="q" size="31" maxlength="255" value="" id="search-input">
64+
<input type="submit" value="検索">
65+
<button type="button" onclick="window.location.href='https://www.google.co.jp/search?q='+encodeURIComponent('site:perldoc.jp '+document.getElementById('search-input').value)">Google検索</button>
6966
</form>
70-
<!-- SiteSearch Google -->
67+
</div>
68+
<!-- SiteSearch -->
7169
</div>
7270
<div id="main" class="span-24 last">
7371
<div id="content">[% content %]</div>

0 commit comments

Comments
 (0)