Extract NAME + SYNOPSIS + first paragraph of DESCRIPTION as preamble. The result is capped at ``_MAX_PREAMBLE_CHARS`` so the chunk budget never goes negative (which would produce one chunk per line).
(text: str)
| 155 | |
| 156 | |
| 157 | def _build_preamble(text: str) -> str: |
| 158 | """Extract NAME + SYNOPSIS + first paragraph of DESCRIPTION as preamble. |
| 159 | |
| 160 | The result is capped at ``_MAX_PREAMBLE_CHARS`` so the chunk budget never |
| 161 | goes negative (which would produce one chunk per line). |
| 162 | """ |
| 163 | sections = _split_sections(text) |
| 164 | preamble_headers = {"# NAME", "# SYNOPSIS", "# DESCRIPTION"} |
| 165 | parts: list[str] = [] |
| 166 | for _start, section_text in sections: |
| 167 | header_line = section_text.split("\n", 1)[0].strip() |
| 168 | if header_line in preamble_headers: |
| 169 | if header_line == "# DESCRIPTION": |
| 170 | paras = section_text.split("\n\n", 2) |
| 171 | parts.append("\n\n".join(paras[:2])) |
| 172 | else: |
| 173 | parts.append(section_text) |
| 174 | preamble = "\n\n".join(parts) |
| 175 | if len(preamble) > _MAX_PREAMBLE_CHARS: |
| 176 | preamble = preamble[:_MAX_PREAMBLE_CHARS].rsplit("\n", 1)[0] + "\n[…truncated]" |
| 177 | return preamble |
| 178 | |
| 179 | |
| 180 | def number_lines(text: str) -> tuple[str, dict[int, str]]: |