Commit 98479a62 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Add support for multiple regexp search

parent 619c2ec8
...@@ -92,21 +92,32 @@ def main_page(): ...@@ -92,21 +92,32 @@ def main_page():
if tags is None or len(set(entry.tags) & tags) > 0] if tags is None or len(set(entry.tags) & tags) > 0]
else: else:
fentries = entries.values() fentries = entries.values()
search = request.args.get('search', None) search = request.args.get('search', "")
if search == "": search = search.strip()
search = None if search != "":
if search is not None: match = []
match = re.compile(search) for s in re.split(r'(?<!\\)&', search):
def score(vals): try:
match.append(re.compile(s.strip(), re.IGNORECASE))
except re.error:
pass
def score_match(m, vals):
n = 0 n = 0
for v in vals: for v in vals:
if v is None: if v is None:
continue continue
if isinstance(v, str): if isinstance(v, str):
n += len(match.findall(v)) n += len(m.findall(v))
else: else:
n += score(v) n += score(v)
return n return n
def score(vals):
n = 1
for m in match:
n *= score_match(m, vals)
if n == 0:
break
return n
fentries = [(entry, score([entry.name, entry.title, entry.description, fentries = [(entry, score([entry.name, entry.title, entry.description,
entry.url, entry.upstream_prefix, entry.url, entry.upstream_prefix,
entry.citation, entry.upstream_license, entry.citation, entry.upstream_license,
......
...@@ -14,8 +14,8 @@ active ...@@ -14,8 +14,8 @@ active
<div class="control-group all-100" style="margin-bottom: 0"> <div class="control-group all-100" style="margin-bottom: 0">
<div class="control append-button" role="search"> <div class="control append-button" role="search">
<span> <span>
{% if search is none %} {% if search == "" %}
<input type="text" name="search" id="search" placeholder="Enter arbitrary regexp term"> <input type="text" name="search" id="search" placeholder="Multiple regexp terms separated by '&'">
{% else %} {% else %}
<input type="text" name="search" id="search", value="{{search}}"> <input type="text" name="search" id="search", value="{{search}}">
{% endif %} {% endif %}
...@@ -32,7 +32,7 @@ active ...@@ -32,7 +32,7 @@ active
{% endfor %} {% endfor %}
<br/> <br/>
{% endif %} {% endif %}
{% if search is not none %} {% if search != "" %}
Showing only entries that match regexp: {{search}}<br/> Showing only entries that match regexp: {{search}}<br/>
{% endif %} {% endif %}
<span style="font-size:x-small">Tip: click on the table header to sort the list. Hover your mouse to obtain a legend.</span> <span style="font-size:x-small">Tip: click on the table header to sort the list. Hover your mouse to obtain a legend.</span>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment