topmark.processors.builtins.xml¶
topmark / processors / builtins / xml
Processor for files with HTML/XML-style block comments ().
This processor supports files using <!-- ... -->-style comments, such as Markdown.
It delegates header processing to the core pipeline dispatcher.
XmlHeaderProcessor ¶
Bases: XmlPositionalMixin, BlockCommentMixin, HeaderProcessor
Header processor for XML/HTML-like formats (uses XmlPositionalMixin).
This processor uses the character-offset strategy for placement:
get_header_insertion_index() returns NO_LINE_ANCHOR and
get_header_insertion_char_offset() computes the insertion offset. The
pipeline falls back to line-based insertion only when no offset is returned.
It still leverages the base processor for rendering, whitespace alignment, and policy-aware behavior where applicable.
Source code in src/topmark/processors/builtins/xml.py
get_header_insertion_index ¶
Not used: return NO_LINE_ANCHOR.
Rely solely on get_header_insertion_char_offset().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_lines
|
list[str]
|
File content split into lines (unused). |
required |
Returns:
| Type | Description |
|---|---|
int
|
|
Source code in src/topmark/processors/builtins/xml.py
get_header_insertion_char_offset ¶
Return a char offset to insert the header for XML/HTML-like documents.
Handles optional BOM, XML declaration, optional DOCTYPE (including simple internal subsets), and consumes whitespace after the prolog if present. Works even when the root element follows on the same line.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_text
|
str
|
Full file content as a single string. |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
Character offset suitable for insertion, or |
Source code in src/topmark/processors/builtins/xml.py
prepare_header_for_insertion_text ¶
prepare_header_for_insertion_text(
*,
original_text,
insert_offset,
rendered_header_text,
newline_style,
)
Adjust whitespace so the header block sits on its own lines.
Policy: for XML-like formats we always want a leading blank after the prolog/doctype when inserting after it; by default we also want a trailing blank before body content. Trailing can be disabled explicitly via policy.ensure_blank_after_header = False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_text
|
str
|
Full file content as a single string. |
required |
insert_offset
|
int
|
0-based character offset where the header will be inserted. |
required |
rendered_header_text
|
str
|
Header block text (may already include newlines). |
required |
newline_style
|
str
|
Newline style ( |
required |
Returns:
| Type | Description |
|---|---|
str
|
Possibly modified header text to splice at |
Source code in src/topmark/processors/builtins/xml.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
prepare_header_for_insertion ¶
prepare_header_for_insertion(
*,
original_lines,
insert_index,
rendered_header_lines,
newline_style,
)
Ensure sensible padding around the header for HTML/XML-like files.
- If inserting at index 0: no leading blank; ensure at least one trailing blank unless the next line is already blank/EOF.
- If inserting after a prolog (index > 0): ensure exactly one leading blank (by checking the previous line), and ensure at least one trailing blank.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
original_lines
|
list[str]
|
Original file lines. |
required |
insert_index
|
int
|
Line index where the header will be inserted. |
required |
rendered_header_lines
|
list[str]
|
Header lines to insert. |
required |
newline_style
|
str
|
Newline style ( |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Possibly modified header lines including any added padding. |
Source code in src/topmark/processors/builtins/xml.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
validate_header_location ¶
Validate the location of a detected header for XML/HTML-like files.
Applies the base proximity rules to ensure the detected header appears
close to the expected insertion anchor (after prolog/doctype) and not
in an obviously invalid location. Markdown-specific safeguards for
fenced code blocks are handled by MarkdownHeaderProcessor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
list[str]
|
Full file content split into lines. |
required |
header_start_idx
|
int
|
0-based index of the candidate header's first line. |
required |
header_end_idx
|
int
|
0-based index of the candidate header's last line (inclusive). |
required |
anchor_idx
|
int
|
0-based expected insertion line index. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in src/topmark/processors/builtins/xml.py
strip_header_block ¶
Remove the TopMark header with minimal, policy-aware cleanup.
Strategy 1 (strict gate): malformed or unsafe XML never reaches here because the pre-insert checker will skip insertion. Therefore we keep removal simple and conservative:
- Delegate removal to the base class (span-aware, policy bounds).
- Do not modify the XML declaration, DOCTYPE, or attempt single-line rejoin of the prolog seam.
- If a trailing policy-blank immediately follows the removed header and
the policy requested a spacer (
ensure_blank_after_header=True), remove at most one such spacer line to restore the pre-header adjacency.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lines
|
list[str]
|
Full file content split into lines (keepends=True). |
required |
span
|
tuple[int, int] | None
|
Optional (start, end) indices of the header block to remove; if |
None
|
newline_style
|
str
|
Newline style ( |
'\n'
|
ends_with_newline
|
bool | None
|
Whether the original file ended with a newline; not used by this simplified implementation but kept for signature compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
StripHeaderResult
|
Structured strip result containing the updated file lines, the |
StripHeaderResult
|
inclusive removed span when a header was removed, and the diagnostic |
StripHeaderResult
|
describing the outcome. |
Source code in src/topmark/processors/builtins/xml.py
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | |