import html from dataclasses import dataclass from typing import Iterable from buster.formatter.base import Response, ResponseFormatter, Source @dataclass class HTMLResponseFormatter(ResponseFormatter): """Format the answer in HTML.""" source_template: str = """
  • 🔗 {source.source}
  • """ error_msg_template: str = """
    Something went wrong:\n

    {response.error_msg}

    """ error_fallback_template: str = """
    Something went very wrong.
    """ sourced_answer_template: str = ( """

    {response.text}

    \n""" """
    \n{sources}
    \n""" """""" ) unsourced_answer_template: str = ( """
    {response.text}
    \n""" ) def sources_list(self, sources: Iterable[Source]) -> str | None: """Format sources into a list.""" items = [self.source_item(source) for source in sources] if not items: return None # No list needed. return "\n".join(items) def __call__(self, response: Response, sources: Iterable[Source]) -> str: # Escape any html in the text. response = Response( html.escape(response.text) if response.text else response.text, response.error, html.escape(response.error_msg) if response.error_msg else response.error_msg, ) sources = (Source(html.escape(source.title), source.url, source.question_similarity) for source in sources) return super().__call__(response, sources)