Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenSearch support #124

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/controllers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from .messaging import messaging_controller
from .new import new_controller
from .news import news_controller
from .opensearch_suggestions import opensearch_suggestions_controller
from .package import package_controller
from .packages import packages_controller
from .popular import popular_controller
Expand Down
20 changes: 20 additions & 0 deletions app/controllers/opensearch_suggestions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import json

from bottle import route, response

from ..models import package


@route('/opensearch_suggestions', name='opensearch_suggestions_blank')
@route('/opensearch_suggestions/<terms:re:(.*)>', name='opensearch_suggestions')
def opensearch_suggestions_controller(terms=''):
# TODO: Optimize this call, since we're requesting a lot of stuff we don't need
results = package.find.search(terms, 'relevance', 1, 10)

# Convert data to match suggestion extension format
# https://github.com/dewitt/opensearch/blob/master/mediawiki/Specifications/OpenSearch/Extensions/Suggestions/1.1/Draft%201.wiki
data = [terms, [package['name'] for package in results['packages']]]

# Manually convert to JSON, since the response is a list and not a dict
response.content_type = 'application/x-suggestions+json'
return json.dumps(data)
1 change: 1 addition & 0 deletions app/html/five_hundred.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<link rel="stylesheet" href="/css/app.css" type="text/css" media="all" />
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
<meta name="viewport" content="width=380" id="meta-viewport" />
<link rel="search" type="application/opensearchdescription+xml" href="/opensearch.xml" title="Package Control" />
<script>
window.onload = function() {
if (screen.width < 740) { return; } // Make phones use 380
Expand Down
1 change: 1 addition & 0 deletions app/templates/partials/header.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<link rel="stylesheet" href="/css/app.css?v={{__version__}}" type="text/css" media="all" />
<link href="//netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.css" rel="stylesheet">
<meta name="viewport" content="width=380" id="meta-viewport" />
<link rel="search" type="application/opensearchdescription+xml" href="/opensearch.xml" title="Package Control" />
<script>
window.onload = function() {
if (screen.width < 740) { return; } /* Make phones use 380 */
Expand Down
5 changes: 5 additions & 0 deletions dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def server_html(filename):
return add_version(response)


@bottle.route('/opensearch.xml')
def server_opensearch():
return bottle.static_file('opensearch.xml', root=public_root, mimetype='application/opensearchdescription+xml')


@bottle.route('/favicon.ico')
def server_fav():
return bottle.static_file('favicon.ico', root=public_root)
Expand Down
11 changes: 11 additions & 0 deletions public/opensearch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
<ShortName>Package Control</ShortName>
<Description>The Sublime Text package manager that makes it exceedingly simple to find, install and keep packages up-to-date</Description>
<Url type="text/html" method="GET" template="https://packagecontrol.io/search/{searchTerms}" />
<Url type="application/opensearchdescription+xml" rel="self" template="https://packagecontrol.io/opensearch.xml" />
<Url type="application/x-suggestions+json" method="get" template="https://packagecontrol.io/opensearch_suggestions/{searchTerms}.json" />
<Image height="64" width="64" type="image/x-icon">https://packagecontrol.io/favicon.ico</Image>
<Language>en-US</Language>
<moz:SearchForm>https://packagecontrol.io/search</moz:SearchForm>
</OpenSearchDescription>
7 changes: 7 additions & 0 deletions servers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ if [[ $TASK == "start" ]]; then
try_files \$uri @site;
}

# Override OpenSearch content type
location /opensearch.xml {
types {
application/opensearchdescription+xml xml;
}
}

location @site {
access_by_lua_file "$PROJ_DIR/realtime/record_web.lua";
proxy_pass http://localhost:9000;
Expand Down