162def extract_code(code_matches: list[dict[str, t.Any]]) -> tuple[list[str], set[int]]:
164 Iterate over multiple possible matches, for each extract a code fragment.
165 Github additionally sends context for _word_ highlights; pygments supports
166 highlighting lines, as such we calculate which lines to highlight while
169 lines: list[str] = []
170 highlighted_lines_index: set[int] = set()
172 for i, match
in enumerate(code_matches):
173 if i > 0
and ghc_insert_block_separator:
175 buffer: list[str] = []
176 highlight_groups = [highlight_group[
'indices']
for highlight_group
in match[
'matches']]
178 code: str = match[
'fragment']
179 original_code_lenght = len(code)
181 if ghc_strip_whitespace:
183 if ghc_strip_new_lines:
184 code = code.lstrip(
"\n")
186 offset = original_code_lenght - len(code)
188 if ghc_strip_whitespace:
190 if ghc_strip_new_lines:
191 code = code.rstrip(
"\n")
193 for i, letter
in enumerate(code):
194 if len(highlight_groups) > 0:
198 [after, before] = highlight_groups[0]
199 if after <= (i + offset) < before:
201 highlighted_lines_index.add(len(lines) + 1)
202 highlight_groups.pop(0)
205 lines.append(
"".join(buffer))
209 buffer.append(letter)
210 lines.append(
"".join(buffer))
211 return lines, highlighted_lines_index
217 if resp.status_code == 422:
222 raise_for_httperror(resp)
224 for item
in resp.json().get(
'items', []):
225 repo: dict[str, str] = item[
'repository']
226 text_matches: list[dict[str, str]] = item[
'text_matches']
229 match
for match
in text_matches
if match[
"object_type"] ==
"FileContent" and match[
"property"] ==
"content"
231 lines, highlighted_lines_index =
extract_code(code_matches)
232 if not ghc_highlight_matching_lines:
233 highlighted_lines_index: set[int] = set()
237 url=item[
"html_url"],
238 title=f
"{repo['full_name']} ยท {item['name']}",
239 filename=f
"{item['path']}",
240 content=repo[
'description'],
241 repository=repo[
'html_url'],
242 codelines=[(i + 1, line)
for (i, line)
in enumerate(lines)],
243 hl_lines=highlighted_lines_index,
244 strip_whitespace=ghc_strip_whitespace,
245 strip_new_lines=ghc_strip_new_lines,