Code Results

Back to top

Typification of the code results. Results of this type are rendered in the template. For highlighting the code passages, Pygments is used.


final class searx.result_types.code.Code(title: str = '', content: str = '', img_src: str = '', iframe_src: str = '', audio_src: str = '', thumbnail: str = '', publishedDate: datetime | None = None, pubdate: str = '', length: timedelta | None = None, views: str = '', author: str = '', metadata: str = '', priority: Literal['', 'high', 'low']='', engines: set[str] = <factory>, open_group: bool = False, close_group: bool = False, positions: list[int] = <factory>, score: float = 0, category: str = '', *, template: str = 'code.html', url: str | None = None, engine: str | None = '', parsed_url: ParseResult | None = None, repository: str | None = None, codelines: list[tuple[int, str]]=<factory>, hl_lines: set[int] = <factory>, code_language: str = '<guess>', filename: str | None = None, strip_new_lines: bool = True, strip_whitespace: bool = False)[source] : Bases: MainResult

Result type suitable for displaying code passages.

repository: [str](https://docs.python.org/3/builtins/stdtypes.html#str) | [None](https://docs.python.org/3/builtins/constants.html#None)
:   A link related to a repository related to the *result*.

codelines: [list](https://docs.python.org/3/builtins/stdtypes.html#list)[[tuple](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[int](https://docs.python.org/3/builtins/functions.html#int), [str](https://docs.python.org/3/builtins/stdtypes.html#str)]]
:   A list of two digit tuples where the first item is the line number and
    the second item is the code line.

hl\_lines: [set](https://docs.python.org/3/builtins/stdtypes.html#set)[[int](https://docs.python.org/3/builtins/functions.html#int)]
:   A list of line numbers to highlight.

code\_language: [str](https://docs.python.org/3/builtins/stdtypes.html#str)
:   Pygment’s short name of the lexer, e.g. `text` for the
    [`pygments.lexers.special.TextLexer`](https://pygments.org/docs/lexers/#pygments.lexers.special.TextLexer). For a list of available
    languages consult: [Pygments languages](https://pygments.org/languages/). If the language is not in this
    list, a [`ValueError`](https://docs.python.org/3/builtins/exceptions.html#ValueError) is raised.

    The default is `<guess>` which has a special meaning;

    - If [`Code.filename`](#searx.result_types.code.Code.filename) is set, Pygment’s factory method
      [`pygments.lexers.guess_lexer_for_filename`](https://pygments.org/docs/api/#pygments.lexers.guess_lexer_for_filename) is used to determine
      the language of the `codelines`.
    - else Pygment’s [`pygments.lexers.guess_lexer`](https://pygments.org/docs/api/#pygments.lexers.guess_lexer) factory is used.

    In case the language can’t be detected, the fallback is `text`.

filename: [str](https://docs.python.org/3/builtins/stdtypes.html#str) | [None](https://docs.python.org/3/builtins/constants.html#None)
:   Optional file name, can help to `<guess>` the language of the code (in
    case of ambiguous short code examples). If `Code.title` is not set,
    its default is the filename.

strip\_new\_lines: [bool](https://docs.python.org/3/builtins/functions.html#bool)
:   Strip leading and trailing newlines for each returned fragment (default:
    `True`). Single file might return multiple code fragments.

strip\_whitespace: [bool](https://docs.python.org/3/builtins/functions.html#bool)
:   Strip all leading and trailing whitespace for each returned fragment
    (default: `False`). Single file might return multiple code fragments.
    Enabling this might break code indentation.

HTML(*\*\*options*) → [str](https://docs.python.org/3/builtins/stdtypes.html#str)[[source]](https://docs.searxng.org/_modules/searx/result_types/code.html#Code.HTML)
:   Rendered HTML, additional options are accepted, for more details have
    a look at [HtmlFormatter](https://pygments.org/docs/formatters/#HtmlFormatter).

Copyright © SearXNG team

Made with Furo

💾 🌐