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

Function extract_header_permalinks

scripts/doc_parsing_utils.py:141–189  ·  view source on GitHub ↗

Extract list of header permalinks from the given lines. Return list of HeaderPermalinkInfo, where each dict contains: - `line_no` - line number (1-based) - `hashes` - string of hashes representing header level (e.g., "###") - `permalink` - permalink string (e.g., "{#permalink}"

(lines: list[str])

Source from the content-addressed store, hash-verified

139
140
141def extract_header_permalinks(lines: list[str]) -> list[HeaderPermalinkInfo]:
142 """
143 Extract list of header permalinks from the given lines.
144
145 Return list of HeaderPermalinkInfo, where each dict contains:
146 - `line_no` - line number (1-based)
147 - `hashes` - string of hashes representing header level (e.g., "###")
148 - `permalink` - permalink string (e.g., "{#permalink}")
149 """
150
151 headers: list[HeaderPermalinkInfo] = []
152 in_code_block3 = False
153 in_code_block4 = False
154
155 for line_no, line in enumerate(lines, start=1):
156 if not (in_code_block3 or in_code_block4):
157 if line.startswith("```"):
158 count = len(line) - len(line.lstrip("`"))
159 if count == 3:
160 in_code_block3 = True
161 continue
162 elif count >= 4:
163 in_code_block4 = True
164 continue
165
166 header_match = HEADER_WITH_PERMALINK_RE.match(line)
167 if header_match:
168 hashes, title, permalink = header_match.groups()
169 headers.append(
170 HeaderPermalinkInfo(
171 hashes=hashes, line_no=line_no, permalink=permalink, title=title
172 )
173 )
174
175 elif in_code_block3:
176 if line.startswith("```"):
177 count = len(line) - len(line.lstrip("`"))
178 if count == 3:
179 in_code_block3 = False
180 continue
181
182 elif in_code_block4:
183 if line.startswith("````"):
184 count = len(line) - len(line.lstrip("`"))
185 if count >= 4:
186 in_code_block4 = False
187 continue
188
189 return headers
190
191
192def remove_header_permalinks(lines: list[str]) -> list[str]:

Callers 1

check_translationFunction · 0.85

Calls 1

HeaderPermalinkInfoClass · 0.85

Tested by

no test coverage detected