Skip to content

Commit

Permalink
Ignore some punctuation in term highlighting
Browse files Browse the repository at this point in the history
Currently only - and ". Should be made more robust at some point if it
turns out to not be enough.

Closes #47.
  • Loading branch information
dellsystem committed Jul 5, 2017
1 parent 40b9dc5 commit a52debc
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bookmarker/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def get_definition(request):
num_occurrences = existing_term.occurrences.count()
else:
definition = lookup_term(language, term)
highlights = term.lower()
highlights = term.lower().replace('-', ' ')

return JsonResponse({
'term': term,
Expand Down
2 changes: 1 addition & 1 deletion templates/add_term.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ <h3>
{% with error=term_form.highlights.errors %}
<div class="{% if error %}error{% endif %} four wide field">
<label>
Highlights{% if error %} (REQUIRED){% endif %}
Highlights{% if error %} ({{ error|join:"" }}){% endif %}
</label>
{{ term_form.highlights }}
</div>
Expand Down
8 changes: 8 additions & 0 deletions vocab/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def clean(self):
term = cleaned_data.get('text')
language = cleaned_data.get('language')
definition = cleaned_data.get('definition')
highlights = cleaned_data.get('highlights')

# Make sure the highlights are specified and don't contain " or -.
if not highlights:
self.add_error('highlights', 'REQUIRED')
else:
if '-' in highlights or '"' in highlights:
self.add_error('highlights', 'INVALID CHARS: - OR "')

if not definition:
term_exists = Term.objects.filter(
Expand Down
7 changes: 4 additions & 3 deletions vocab/management/commands/test_quote_highlighting.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ def test_equality(self):
highlighted_quote = o.get_highlighted_quote()
if '<span class="highlight">' not in highlighted_quote:
self.fail(
u"Failed: looking for {highlights} in '{quote}'".format(
quote=o.quote,
highlights=o.term.highlights
u"Failed: looking for {h} in '{q}' (pk: {pk})'".format(
q=o.quote,
h=o.term.highlights,
pk=o.term.pk,
)
)
6 changes: 4 additions & 2 deletions vocab/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ def __unicode__(self):

def get_highlighted_quote(self):
q = self.quote
q_lower = self.quote.lower() # for more efficient (?) searching
# For more efficient searching.
q_search = self.quote.lower().replace('-', ' ').replace('"', '')
highlights = self.term.highlights.splitlines()
for h in highlights:
if h in q_lower:
if h in q_search:
h = h.replace(' ', '[-" ]*')
q = re.sub('(' + h + ')', r'<span class="highlight">\1</span>', q,
flags=re.I)

Expand Down

0 comments on commit a52debc

Please sign in to comment.