hishidaの開発blog

EBシリーズ(EBPocket,EBWin,EBMac,EBStudio),KWIC Finder,xdoc2txt,読書尚友の開発者ブログ

読書尚友に OpenSearch-1.1 を実装

拙作の青空文庫ビューアである「読書尚友」にはOPDSクライアントの機能があるが、あるきっかけがあって、OpenSearch対応を行った。

そのきっかけというのは、青空文庫でヘンリー・デイビッド・ソローの『森の生活』が追加されていることに気づいたことである。思い入れのある作品なので読もうと試みたが、翻訳が難解で、少しも頭に入ってこない。ふとオリジナルのテキストを見たくなって*1、読書尚友のOPDSライブラリでProject Gutenbergから探そうとしたが、検索機能がないと全く探せないことが分かった。OpenSearchの存在は以前から知っていたが、なんとなく億劫で対応を避けていたのだが、これがきっかけで調べてみる気になった。

まずOpenSearchの仕様はこちら:

opensearch/opensearch-1-1-draft-6.md at master · dewitt/opensearch · GitHub

翻訳はこちら:

Open Search 仕様書 1.1 ドラフト4版 - Walrus, Googling

OpenSearchは、利用する側にとっては特に難しくはない。Project Gutenbergを例にとって説明してみたい。

まず、Project GutenbergのOPDSのURLを実行すると、ルートカタログがRSSで返される。

https://m.gutenberg.org/ebooks.opds/

このなかに、OpenSearchのエントリがある。

<link rel="search" type="application/opensearchdescription+xml" title="Project Gutenberg Catalog Search" href="https://www.gutenberg.org/catalog/osd-books.xml"/>

 間接参照になっているので、href=のURLを実行すると、OpenSearchxmlが返却される。

<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<LongName>Project Gutenberg</LongName>
<ShortName>Gutenberg</ShortName>
<Description>Search the Project Gutenberg ebook catalog.</Description>
<Tags>free ebooks books public domain</Tags>
<Developer>Marcello Perathoner</Developer>
<Contact>webmaster@gutenberg.org</Contact>
<Url type="text/html" template="http://www.gutenberg.org/ebooks/search/?query={searchTerms}"/>
<Url type="application/atom+xml" template="http://m.gutenberg.org/ebooks/search.opds/?query={searchTerms}"/>
<Url type="application/x-suggestions+json" rel="suggestions" 
template="http://www.gutenberg.org/ebooks/suggest/?query={searchTerms}"/> <!-- <Url type="application/rss+xml" template="http://example.com/?q={searchTerms}&pw={startPage?}&format=rss"/> <Image height="64" width="64" type="image/png">http://example.com/websearch.png <Image height="16" width="16" type="image/vnd.microsoft.icon">http://example.com/websearch.ico --> <Query role="example" searchTerms="shakespeare hamlet"/> <Query role="example" searchTerms="doyle detective"/> <Query role="example" searchTerms="love stories"/> <Attribution>Search Data Copyright 1971-2012, Project Gutenberg, All Rights Reserved.</Attribution> <SyndicationRight>open</SyndicationRight> <Language>en-us</Language> <OutputEncoding>UTF-8</OutputEncoding> <InputEncoding>UTF-8</InputEncoding> </OpenSearchDescription >

 このなかで、

 <Url type="application/atom+xml" template="http://m.gutenberg.org/ebooks/search.opds/?query={searchTerms}"/>

 が検索のテンプレートである。{searchTerms}の部分を検索語に置き換えて実行すれば、検索結果のRSSが得られるので、OPDSと同様に表示すればいい。

Project Gutenbergから "walden" で検索する例:

http://m.gutenberg.org/ebooks/search.opds/?query=walden

連語を検索する場合はURLEndodingにする。”shakespeare hamlet”を検索する例:

 http://m.gutenberg.org/ebooks/search.opds/?query=shakespeare%20hamlet

 
サイトによっては、間接参照のtype="application/opensearchdescription+xml"ではなく、OPDSのカタログの中に直接参照のtype="application/atom+xml" でOpenSearchのテンプレートがある場合がある。

(例:ManyBooks.net)。

<link rel="search" title="Search Catalog" type="application/atom+xml" href="http://manybooks.net/opds/search.php?q={searchTerms}"/>

 

画面の検索イメージ:

f:id:hishida:20201125185720j:plain

f:id:hishida:20201125185737j:plain

f:id:hishida:20201125185746j:plain

*1:もちろん、『森の生活』の英語版を読み通すような英語力は全くない。冒頭だけでも比較してみたくなっただけだ。