From d45b4c0b89cd4b774f639fc6b24025725c19d9dc Mon Sep 17 00:00:00 2001 From: Mathias Rav Date: Sun, 5 Aug 2018 14:56:17 +0200 Subject: [PATCH] Show first match instead of last in search results --- src/wiki/templatetags/wiki_tags.py | 2 +- tests/core/test_template_filters.py | 26 ++++++-------------------- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/src/wiki/templatetags/wiki_tags.py b/src/wiki/templatetags/wiki_tags.py index c9cd8c81..560f1355 100644 --- a/src/wiki/templatetags/wiki_tags.py +++ b/src/wiki/templatetags/wiki_tags.py @@ -116,7 +116,7 @@ def get_content_snippet(content, keyword, max_words=30): max_words = int(max_words) - match_position = content.lower().rfind(keyword.lower()) + match_position = content.lower().find(keyword.lower()) if match_position != -1: try: diff --git a/tests/core/test_template_filters.py b/tests/core/test_template_filters.py index 207b6108..6e76855c 100644 --- a/tests/core/test_template_filters.py +++ b/tests/core/test_template_filters.py @@ -40,18 +40,9 @@ class GetContentSnippet(TemplateTestCase): self.assertEqual(output, expected) - def test_whole_content_is_consist_from_keywords(self): + def test_whole_content_consists_of_keywords(self): content = 'lorem ' * 80 - expected = ( - 'lorem lorem ' - 'lorem lorem ' - 'lorem lorem ' - 'lorem lorem ' - 'lorem lorem ' - 'lorem lorem ' - 'lorem lorem ' - 'lorem lorem' - ) + expected = 'lorem' + 30 * ' lorem' output = get_content_snippet(content, 'lorem') @@ -79,11 +70,7 @@ class GetContentSnippet(TemplateTestCase): text = 'dolorum ' * 80 content += text + ' list' - expected = ( - 'dolorum dolorum dolorum dolorum dolorum dolorum dolorum ' - 'dolorum dolorum dolorum dolorum dolorum dolorum dolorum dolorum ' - 'list' - ) + expected = 'list' + 30 * ' lorem' output = get_content_snippet(content, 'list') @@ -145,8 +132,7 @@ class GetContentSnippet(TemplateTestCase): keyword = 'maybe' content = """ -

Some dummy

text.
Actually
I don't what to write, - heh. Don't now, maybe I should citate Shakespeare or Byron. + I should citate Shakespeare or Byron. Or maybe copy paste from python or django documentation. Maybe. """ @@ -168,8 +154,8 @@ class GetContentSnippet(TemplateTestCase): content = """ knight eggs spam ham eggs guido python eggs circus """ - expected = ('eggs guido python ' - 'eggs circus') + expected = ('knight eggs spam ham ' + 'eggs guido') output = get_content_snippet(content, keyword, 5) -- 2.45.2