Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change the search and source-code URLs to match MXR's. Closes bug 834…

…509.
  • Loading branch information...
commit 9fdcaf43be43e23cff165a762688d21fda6b771f 2 parents 3cce1dd + 1914a6d
@erikrose erikrose authored
View
25 dxr/app.py
@@ -39,8 +39,8 @@ def index():
return send_file(current_app.open_instance_resource('trees/index.html'))
-@dxr_blueprint.route('/search')
-def search():
+@dxr_blueprint.route('/<tree>/search')
+def search(tree):
"""Search by regex, caller, superclass, or whatever."""
# TODO: This function still does too much.
querystring = request.values
@@ -49,7 +49,6 @@ def search():
limit = non_negative_int(querystring.get('limit'), 100)
# Get and validate tree:
- tree = querystring.get('tree')
config = current_app.config
# Arguments for the template:
@@ -62,6 +61,7 @@ def search():
'generated_date': config['GENERATED_DATE']}
error = warning = ''
+ status_code = None
if tree in config['TREES']:
arguments['tree'] = tree
@@ -80,14 +80,14 @@ def search():
path, line = result
# TODO: Does this escape qtext properly?
return redirect(
- '%s/%s/%s?from=%s#l%i' %
+ '%s/%s/source/%s?from=%s#l%i' %
(config['WWW_ROOT'], tree, path, qtext, line))
# Return multiple results:
template = 'search.html'
start = time()
try:
- results = list(q.fetch_results(offset, limit))
+ results = list(q.results(offset, limit))
except OperationalError as e:
if e.message.startswith('REGEXP:'):
# Malformed regex
@@ -111,6 +111,7 @@ def search():
error = 'Failed to establish database connection.'
else:
error = "Tree '%s' is not a valid tree." % tree
+ status_code = 404
if warning or error:
arguments['error'] = error or warning
@@ -119,7 +120,7 @@ def search():
if error:
# Return a non-OK code so the live search doesn't try to replace
# the results with our empty ones:
- return jsonify(arguments), 500
+ return jsonify(arguments), status_code or 500
# Tuples are encoded as lists in JSON, and these are not real
# easy to unpack or read in Javascript. So for ease of use, we
@@ -133,14 +134,16 @@ def search():
for icon, path, lines in arguments['results']]
return jsonify(arguments)
- return render_template('error.html' if error else 'search.html',
- **arguments)
+ if error:
+ return render_template('error.html', **arguments), status_code or 500
+ else:
+ return render_template('search.html', **arguments)
-@dxr_blueprint.route('/<path:tree_and_path>')
-def browse(tree_and_path):
+@dxr_blueprint.route('/<tree>/source/')
+@dxr_blueprint.route('/<tree>/source/<path:path>')
+def browse(tree, path=''):
"""Show a directory listing or a single file from one of the trees."""
- tree, _, path = tree_and_path.partition('/')
tree_folder = os.path.join(current_app.instance_path, 'trees', tree)
if isdir(os.path.join(tree_folder, path)):
View
21 dxr/build.py
@@ -236,12 +236,13 @@ def build_folder(tree, conn, folder, indexed_files, indexed_folders):
# Generate list of folders and their mod dates:
folders = [('folder',
- f,
- datetime.fromtimestamp(stat(os.path.join(tree.source_folder,
- folder,
- f)).st_mtime),
- _join_url(tree.name, folder, f))
- for f in indexed_folders]
+ f,
+ datetime.fromtimestamp(stat(os.path.join(tree.source_folder,
+ folder,
+ f)).st_mtime),
+ # TODO: DRY with Flask route. Use url_for:
+ _join_url(tree.name, 'source', folder, f))
+ for f in indexed_folders]
# Generate list of files:
files = []
@@ -250,10 +251,10 @@ def build_folder(tree, conn, folder, indexed_files, indexed_folders):
path = os.path.join(tree.source_folder, folder, f)
file_info = stat(path)
files.append((dxr.mime.icon(path),
- f,
- datetime.fromtimestamp(file_info.st_mtime),
- file_info.st_size,
- _join_url(tree.name, folder, f)))
+ f,
+ datetime.fromtimestamp(file_info.st_mtime),
+ file_info.st_size,
+ _join_url(tree.name, 'source', folder, f)))
# Lay down the HTML:
jinja_env = load_template_env(tree.config.temp_folder,
View
20 dxr/query.py
@@ -121,9 +121,16 @@ def execute_sql(self, sql, *parameters):
# Fetch results using a query,
# See: queryparser.py for details in query specification
- def fetch_results(self,
- offset = 0, limit = 100,
- markup = "<b>", markdown = "</b>"):
+ def results(self,
+ offset=0, limit=100,
+ markup='<b>', markdown='</b>'):
+ """Return search results as an iterable of these::
+
+ (icon,
+ path within tree,
+ (line_number, highlighted_line_of_code)), ...
+
+ """
sql = """
SELECT files.path, files.icon, trg_index.text, files.id,
extents(trg_index.contents)
@@ -253,8 +260,11 @@ def number_lines(arr):
def direct_result(self):
- """ Get a direct result as tuple of (path, line) or None if not direct result
- for query, ie. complex query
+ """Return a single search result that is an exact match for the query.
+
+ If there is such a result, return a tuple of (path from root of tree,
+ line number). Otherwise, return None.
+
"""
term = self.single_term()
if not term:
View
5 dxr/static/advanced-search.js
@@ -144,9 +144,8 @@ function initAdvancedSearch(){
var as = document.getElementById("advanced-search");
as.addEventListener('submit', function(e){
// Change document location
- document.location = createSearchUrl({
- q: buildQueryFromAdvanced(),
- tree: dxr.tree()
+ document.location = createSearchUrl(dxr.tree(), {
+ q: buildQueryFromAdvanced()
});
e.preventDefault();
return false;
View
16 dxr/static/search.js
@@ -13,7 +13,7 @@ var result_template = ""
var lines_template = ""
+ "<a class=\"snippet\" "
- + " href=\"{{wwwroot}}/{{tree}}/{{path}}#l{{line_number}}\">"
+ + " href=\"{{wwwroot}}/{{tree}}/source/{{path}}#l{{line_number}}\">"
+ " <div class=\"line-numbers\">"
+ " <pre><span class=\"ln\">{{line_number}}</span></pre>"
+ " </div>"
@@ -47,7 +47,7 @@ function format_results(data){
for(var j = 0; j < folders.length; j++){
var folder = folders[j];
var p = folders.slice(0, j + 1).join('/');
- var href = wwwroot + '/' + dxr.tree() + '/' + p;
+ var href = wwwroot + '/' + dxr.tree() + '/source/' + p;
pathline += "<a href=\"" + href + "\" ";
if(j + 1 < folders.length){
pathline += "data-path=\"" + p + "/\"";
@@ -237,7 +237,6 @@ function fetch_results(display_fetcher){
// parameters for request
var params = {
q: state.query,
- tree: dxr.tree(),
limit: state.limit,
offset: state.offset,
redirect: 'false',
@@ -245,7 +244,7 @@ function fetch_results(display_fetcher){
};
// Start a new request
- request.open("GET", createSearchUrl(params), true);
+ request.open("GET", createSearchUrl(dxr.tree(), params), true);
request.send();
}
@@ -261,14 +260,14 @@ function parseQuerystring(){
}
/** Create search URL from search parameters as querystring */
-function createSearchUrl(params){
+function createSearchUrl(tree, params){
var elements = []
for(var key in params){
var k = encodeURIComponent(key);
var v = encodeURIComponent(params[key]);
elements.push(k + "=" + v);
}
- return wwwroot + "/search?" + elements.join("&");
+ return wwwroot + "/" + tree + "/search?" + elements.join("&");
}
window.createSearchUrl = createSearchUrl; // used in advanced-search.js
@@ -292,18 +291,17 @@ function initMenu(){
// Parse querystring so we can make some urls
var params = {
- tree: dxr.tree(),
limit: state.limit,
redirect: 'false'
};
// Create url to limit search
params.q = state.query + " path:" + path;
- var limit_url = createSearchUrl(params);
+ var limit_url = createSearchUrl(dxr.tree(), params);
// Create url to exclude path from search
params.q = state.query + " -path:" + path;
- var exclude_url = createSearchUrl(params);
+ var exclude_url = createSearchUrl(dxr.tree(), params);
// Populate menu with links
menu.populate([
View
2  dxr/templates/folder.html
@@ -8,7 +8,7 @@
<table class="folder-content">
<tr><th style="padding-left: 24px;">Name</th><th>Modified</th><th>Size</th></tr>
<tr>
- <td><a href="{{ wwwroot }}/{{ tree }}/{{ path }}/.."
+ <td><a href="{{ wwwroot }}/{{ tree }}/source/{{ path }}/.."
style="background-image: url('{{ wwwroot }}/static/icons/folder.png')">
Parent directory</a></td>
<td></td>
View
2  dxr/templates/layout.html
@@ -81,7 +81,7 @@
{% elif not loop.first and trees|length > 1 -%}
,
{% endif -%}
- <a href="{{ t }}/">{{ t }}</a>
+ <a href="{{ t }}/source/">{{ t }}</a>
{%- endfor -%}
.
</div>
View
19 dxr/templates/search.html
@@ -101,7 +101,7 @@
<div class="path" style="background-image: url({{ wwwroot }}/static/icons/{{ icon }}.png);">
{%- set folders = path.split('/') -%}
{%- for folder in folders -%}
- <a href="{{ wwwroot }}/{{ tree }}/{{ "/".join(folders[0:loop.index]) }}"
+ <a href="{{ wwwroot }}/{{ tree }}/source/{{ "/".join(folders[0:loop.index]) }}"
{% if not loop.last %}
data-path="{{ "/".join(folders[0:loop.index]) }}/"
{% endif %}
@@ -111,12 +111,12 @@
{%- endfor -%}
</div>
{% for number, line in lines %}
- <a class="snippet" href="{{ wwwroot }}/{{ tree }}/{{ path }}#l{{ number }}">
- <div class="line-numbers">
- <pre><span class="ln">{{ number }}</span></pre>
- </div>
- <div class="file-lines"><pre><code>{{ line|safe }}</code></pre></div>
- </a>
+ <a class="snippet" href="{{ wwwroot }}/{{ tree }}/source/{{ path }}#l{{ number }}">
+ <div class="line-numbers">
+ <pre><span class="ln">{{ number }}</span></pre>
+ </div>
+ <div class="file-lines"><pre><code>{{ line|safe }}</code></pre></div>
+ </a>
{%- endfor %}
</div>
{% else %}
@@ -134,9 +134,8 @@
{# We don't show next link if there's no more results,
# and show prev link if offset != 0. Note that these links will
# only be available to non-javascript users. #}
- {% set search_url = wwwroot
- + '/search?tree=' + tree
- + '&q=' + quoted_query + '&redirect=false' %}
+ {% set search_url = wwwroot + '/' + tree + '/search?q=' +
+ quoted_query + '&redirect=false' %}
{% set prev = offset - results|length %}
{% set next = offset + results|length %}
<div id="result-page-switch">
View
2  dxr/testing.py
@@ -114,7 +114,7 @@ def search_results(self, query):
"""
response = self.client().get(
- '/search?format=json&tree=code&q=%s&redirect=false' % quote(query))
+ '/code/search?format=json&q=%s&redirect=false' % quote(query))
return json.loads(response.data)['results']
View
2  tests/test_ignores/test_ignores.py
@@ -8,7 +8,7 @@ class IgnorePatternTests(DxrInstanceTestCase):
def _top_level_index(self):
"""Return the HTML of the front browse page."""
- return self.client().get('/code').data
+ return self.client().get('/code/source/').data
def test_non_path(self):
"""Test that non-path-based ignore patterns are obeyed."""
Please sign in to comment.
Something went wrong with that request. Please try again.