Add typehints
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import urllib.parse
|
||||
from typing import Iterable
|
||||
|
||||
from reader import EntrySearchResult, HighlightedString
|
||||
from reader import EntrySearchResult, Feed, HighlightedString
|
||||
|
||||
from discord_rss_bot.settings import reader
|
||||
|
||||
@ -15,12 +15,14 @@ def create_html_for_search_results(search_results: Iterable[EntrySearchResult])
|
||||
Returns:
|
||||
str: The HTML.
|
||||
"""
|
||||
html = ""
|
||||
# TODO: There is a .content that also contains text, we should use that if .summary is not available.
|
||||
# TODO: We should also add <span> tags to the title.
|
||||
html: str = ""
|
||||
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)
|
||||
result_summary: str = add_span_with_slice(result.content[".summary"])
|
||||
feed: Feed = reader.get_feed(result.feed_url)
|
||||
feed_url: str = urllib.parse.quote(feed.url)
|
||||
|
||||
html += f"""
|
||||
<a class="text-muted text-decoration-none" href="/feed?feed_url={feed_url}">
|
||||
@ -41,9 +43,12 @@ def add_span_with_slice(highlighted_string: HighlightedString) -> str:
|
||||
Returns:
|
||||
str: The string with added <span> tags.
|
||||
"""
|
||||
# TODO: We are looping through the highlights and only using the last one. We should use all of them.
|
||||
before_span, span_part, after_span = ""
|
||||
|
||||
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}"
|
||||
before_span: str = f"{highlighted_string.value[: txt_slice.start]}"
|
||||
span_part: str = f"<span class='bg-warning'>{highlighted_string.value[txt_slice.start: txt_slice.stop]}</span>"
|
||||
after_span: str = f"{highlighted_string.value[txt_slice.stop:]}"
|
||||
|
||||
return f"{before_span}{span_part}{after_span}"
|
||||
|
Reference in New Issue
Block a user