.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.result_types.code.Code Class Reference
Inheritance diagram for searx.result_types.code.Code:
Collaboration diagram for searx.result_types.code.Code:

Public Member Functions

 __post_init__ (self)
 __hash__ (self)
 get_lexer (self)
str HTML (self, **options)
Public Member Functions inherited from searx.result_types._base.MainResult
int __hash__ (self)
 normalize_result_fields (self)
Public Member Functions inherited from searx.result_types._base.Result
 __post_init__ (self)
 filter_urls (self, "Callable[[Result | LegacyResult, str, str], str | bool]" filter_func)
int __hash__ (self)
 __eq__ (self, object other)
 __setitem__ (self, str field_name, t.Any value)
t.Any __getitem__ (self, str field_name)
 __iter__ (self)
 as_dict (self)
 defaults_from (self, "Result" other)

Static Public Attributes

str repository = None
list codelines = []
set hl_lines = set()
str code_language = "<guess>"
str filename = None
bool strip_new_lines = True
bool strip_whitespace = False
Static Public Attributes inherited from searx.result_types._base.MainResult
str title = ""
str content = ""
str img_src = ""
str thumbnail = ""
datetime publishedDate = None
str pubdate = ""
time length = None
str views = ""
str author = ""
str metadata = ""
 PriorityType = t.Literal["", "high", "low"]
str priority = ""
set engines = set()
bool open_group = False
bool close_group = False
list positions = []
float score = 0
str category = ""
Static Public Attributes inherited from searx.result_types._base.Result
str url = None
str template = "default.html"
str engine = ""
urllib parsed_url = None

Detailed Description

Result type suitable for displaying code passages.

Definition at line 46 of file code.py.

Member Function Documentation

◆ __hash__()

searx.result_types.code.Code.__hash__ ( self)
The hash value is build up from URL and code lines. :py:obj:`Code
<Result.__eq__>` objects are equal, when the hash values of both objects
are equal.

Definition at line 103 of file code.py.

103 def __hash__(self):
104 """The hash value is build up from URL and code lines. :py:obj:`Code
105 <Result.__eq__>` objects are equal, when the hash values of both objects
106 are equal.
107 """
108 return hash(f"{self.url} {self.codelines}")
109

◆ __post_init__()

searx.result_types.code.Code.__post_init__ ( self)

Definition at line 94 of file code.py.

94 def __post_init__(self):
95 super().__post_init__()
96
97 if not self.title and self.filename:
98 self.title = self.filename
99
100 if self.code_language != "<guess>" and not is_valid_language(self.code_language):
101 raise ValueError(f"unknown code_language: {self.code_language}")
102

References __post_init__(), searx.exceptions.SearxSettingsException.filename, searx.metrics.error_recorder.ErrorContext.filename, filename, and searx.result_types._base.MainResult.title.

Referenced by __post_init__().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_lexer()

searx.result_types.code.Code.get_lexer ( self)

Definition at line 110 of file code.py.

110 def get_lexer(self):
111 if self.code_language != "<guess>":
112 return get_lexer_by_name(self.code_language)
113
114 src_code = "\n".join([l[1] for l in self.codelines])
115 if self.filename:
116 try:
117 return guess_lexer_for_filename(self.filename, src_code)
118 except ClassNotFound:
119 pass
120 try:
121 return guess_lexer(src_code)
122 except ClassNotFound:
123 pass
124 return get_lexer_by_name("text")
125

References code_language, codelines, searx.exceptions.SearxSettingsException.filename, searx.metrics.error_recorder.ErrorContext.filename, and filename.

Referenced by HTML().

Here is the caller graph for this function:

◆ HTML()

str searx.result_types.code.Code.HTML ( self,
** options )
Rendered HTML, additional options are accepted, for more details have
a look at HtmlFormatter_.

.. _HtmlFormatter: https://pygments.org/docs/formatters/#HtmlFormatter

Definition at line 126 of file code.py.

126 def HTML(self, **options) -> str: # pyright: ignore[reportUnknownParameterType, reportMissingParameterType]
127 """Rendered HTML, additional options are accepted, for more details have
128 a look at HtmlFormatter_.
129
130 .. _HtmlFormatter: https://pygments.org/docs/formatters/#HtmlFormatter
131 """
132 lexer = self.get_lexer()
133
134 line_no: int = 0 # current line number
135 code_block_start: int = 0 # line where the current code block starts
136 code_block_end: int | None = None # line where the current code ends
137 code_block: list[str] = [] # lines of the current code block
138 html_code_blocks: list[str] = [] # HTML representation of all code blocks
139
140 def _render(**kwargs): # pyright: ignore[reportUnknownParameterType, reportMissingParameterType]
141 for k, default in [
142 ("linenos", "inline"),
143 ("linenostart", code_block_start),
144 ("cssclass", "code-highlight"),
145 ("hl_lines", [hl - code_block_start + 1 for hl in self.hl_lines]),
146 ]:
147 kwargs[k] = kwargs.get(k, default) # pyright: ignore[reportUnknownMemberType]
148
149 # Wrap the code inside <pre> blocks using <code>, as recommended by
150 # the HTML5 specification (default is False). Do we need this?
151 kwargs["wrapcode"] = kwargs.get("wrapcode", True)
152
153 html_code_blocks.append(
154 highlight(
155 "\n".join(code_block),
156 lexer,
157 HtmlFormatter(**kwargs), # pyright: ignore[reportUnknownArgumentType]
158 )
159 )
160
161 for line_no, code_line in self.codelines:
162 if code_block_end is None:
163 # initial start condition
164 code_block_start = line_no
165
166 if code_block_end is not None and code_block_end + 1 != line_no:
167 # new code block is detected, render current code block
168 _render(**options) # pyright: ignore[reportUnknownArgumentType]
169 # reset conditions for next code block, which first line is the
170 # current code line
171 code_block = [code_line]
172 code_block_start = line_no
173 code_block_end = line_no
174 continue
175
176 # add line to the current code block and update last line n
177 code_block.append(code_line)
178 code_block_end = line_no
179
180 # highlight (last) code block
181 _render(**options) # pyright: ignore[reportUnknownArgumentType]
182 return "\n".join(html_code_blocks)

References codelines, get_lexer(), and hl_lines.

Here is the call graph for this function:

Member Data Documentation

◆ code_language

searx.result_types.code.Code.code_language = "<guess>"
static

Definition at line 61 of file code.py.

Referenced by get_lexer().

◆ codelines

list searx.result_types.code.Code.codelines = []
static

Definition at line 54 of file code.py.

Referenced by get_lexer(), and HTML().

◆ filename

searx.result_types.code.Code.filename = None
static

Definition at line 80 of file code.py.

Referenced by __post_init__(), and get_lexer().

◆ hl_lines

set searx.result_types.code.Code.hl_lines = set()
static

Definition at line 58 of file code.py.

Referenced by HTML().

◆ repository

str searx.result_types.code.Code.repository = None
static

Definition at line 51 of file code.py.

◆ strip_new_lines

bool searx.result_types.code.Code.strip_new_lines = True
static

Definition at line 85 of file code.py.

◆ strip_whitespace

bool searx.result_types.code.Code.strip_whitespace = False
static

Definition at line 89 of file code.py.


The documentation for this class was generated from the following file:
  • /home/andrew/Documents/code/public/searxng/searx/result_types/code.py