MCPcopy Create free account
hub / github.com/fastapi/fastapi / extract_multiline_code_blocks

Function extract_multiline_code_blocks

scripts/doc_parsing_utils.py:482–549  ·  view source on GitHub ↗
(text: list[str])

Source from the content-addressed store, hash-verified

480
481
482def extract_multiline_code_blocks(text: list[str]) -> list[MultilineCodeBlockInfo]:
483 blocks: list[MultilineCodeBlockInfo] = []
484
485 in_code_block3 = False
486 in_code_block4 = False
487 current_block_lang = ""
488 current_block_start_line = -1
489 current_block_lines = []
490
491 for line_no, line in enumerate(text, start=1):
492 stripped = line.lstrip()
493
494 # --- Detect opening fence ---
495 if not (in_code_block3 or in_code_block4):
496 if stripped.startswith("```"):
497 current_block_start_line = line_no
498 count = len(stripped) - len(stripped.lstrip("`"))
499 if count == 3:
500 in_code_block3 = True
501 current_block_lang = get_code_block_lang(stripped)
502 current_block_lines = [line]
503 continue
504 elif count >= 4:
505 in_code_block4 = True
506 current_block_lang = get_code_block_lang(stripped)
507 current_block_lines = [line]
508 continue
509
510 # --- Detect closing fence ---
511 elif in_code_block3:
512 if stripped.startswith("```"):
513 count = len(stripped) - len(stripped.lstrip("`"))
514 if count == 3:
515 current_block_lines.append(line)
516 blocks.append(
517 MultilineCodeBlockInfo(
518 lang=current_block_lang,
519 start_line_no=current_block_start_line,
520 content=current_block_lines,
521 )
522 )
523 in_code_block3 = False
524 current_block_lang = ""
525 current_block_start_line = -1
526 current_block_lines = []
527 continue
528 current_block_lines.append(line)
529
530 elif in_code_block4:
531 if stripped.startswith("````"):
532 count = len(stripped) - len(stripped.lstrip("`"))
533 if count >= 4:
534 current_block_lines.append(line)
535 blocks.append(
536 MultilineCodeBlockInfo(
537 lang=current_block_lang,
538 start_line_no=current_block_start_line,
539 content=current_block_lines,

Callers 1

check_translationFunction · 0.85

Calls 2

get_code_block_langFunction · 0.85

Tested by

no test coverage detected