Move search stuff to own file

This commit is contained in:
2022-12-10 22:30:35 +01:00
parent 1de74f7d8a
commit 2f24de421f
3 changed files with 52 additions and 40 deletions

49
discord_rss_bot/search.py Normal file
View File

@ -0,0 +1,49 @@
import urllib.parse
from typing import Iterable
from reader import EntrySearchResult, HighlightedString
from discord_rss_bot.settings import reader
def create_html_for_search_results(search_results: Iterable[EntrySearchResult]) -> str:
"""Create HTML for the search results.
Args:
search_results: The search results.
Returns:
str: The HTML.
"""
html = ""
for result in search_results:
if ".summary" in result.content:
result_summary = add_span_with_slice(result.content[".summary"])
feed = reader.get_feed(result.feed_url)
feed_url = urllib.parse.quote(feed.url)
html += f"""
<a class="text-muted text-decoration-none" href="/feed?feed_url={feed_url}">
<h2>{result.metadata[".title"]}</h2>
</a>
{result_summary}
<hr>
"""
return html
def add_span_with_slice(highlighted_string: HighlightedString) -> str:
"""Add span tags to the string to highlight the search results.
Args:
highlighted_string: The highlighted string.
Returns:
str: The string with added <span> tags.
"""
for txt_slice in highlighted_string.highlights:
before_span = f"{highlighted_string.value[: txt_slice.start]}"
span_part = f"<span class='bg-warning'>{highlighted_string.value[txt_slice.start: txt_slice.stop]}</span>"
after_span = f"{highlighted_string.value[txt_slice.stop:]}"
return f"{before_span}{span_part}{after_span}"