html-to-markdown 1.3.3__py3-none-any.whl → 1.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of html-to-markdown might be problematic. Click here for more details.

@@ -4,6 +4,8 @@ from typing import TYPE_CHECKING
4
4
 
5
5
  if TYPE_CHECKING:
6
6
  from collections.abc import Iterable
7
+ import base64
8
+ import re
7
9
  from functools import partial
8
10
  from inspect import getfullargspec
9
11
  from textwrap import fill
@@ -21,41 +23,101 @@ from html_to_markdown.utils import chomp, indent, underline
21
23
 
22
24
  SupportedElements = Literal[
23
25
  "a",
26
+ "abbr",
27
+ "article",
28
+ "aside",
29
+ "audio",
24
30
  "b",
31
+ "bdi",
32
+ "bdo",
25
33
  "blockquote",
26
34
  "br",
35
+ "button",
36
+ "caption",
37
+ "cite",
27
38
  "code",
39
+ "col",
40
+ "colgroup",
41
+ "data",
42
+ "datalist",
43
+ "dd",
28
44
  "del",
45
+ "details",
46
+ "dfn",
47
+ "dialog",
48
+ "dl",
49
+ "dt",
29
50
  "em",
51
+ "fieldset",
52
+ "figcaption",
53
+ "figure",
54
+ "footer",
55
+ "form",
30
56
  "h1",
31
57
  "h2",
32
58
  "h3",
33
59
  "h4",
34
60
  "h5",
35
61
  "h6",
62
+ "header",
63
+ "hgroup",
36
64
  "hr",
37
65
  "i",
66
+ "iframe",
38
67
  "img",
68
+ "input",
69
+ "ins",
70
+ "kbd",
71
+ "label",
72
+ "legend",
39
73
  "list",
40
- "ul",
74
+ "main",
75
+ "mark",
76
+ "math",
77
+ "menu",
78
+ "meter",
79
+ "nav",
41
80
  "ol",
42
81
  "li",
82
+ "optgroup",
83
+ "option",
84
+ "output",
43
85
  "p",
86
+ "picture",
44
87
  "pre",
45
- "script",
46
- "style",
88
+ "progress",
89
+ "q",
90
+ "rb",
91
+ "rp",
92
+ "rt",
93
+ "rtc",
94
+ "ruby",
47
95
  "s",
48
- "strong",
49
96
  "samp",
97
+ "script",
98
+ "section",
99
+ "select",
100
+ "small",
101
+ "strong",
102
+ "style",
50
103
  "sub",
104
+ "summary",
51
105
  "sup",
106
+ "svg",
52
107
  "table",
53
- "caption",
54
- "figcaption",
108
+ "tbody",
55
109
  "td",
110
+ "textarea",
111
+ "tfoot",
56
112
  "th",
113
+ "thead",
114
+ "time",
57
115
  "tr",
58
- "kbd",
116
+ "u",
117
+ "ul",
118
+ "var",
119
+ "video",
120
+ "wbr",
59
121
  ]
60
122
 
61
123
  Converter = Callable[[str, Tag], str]
@@ -119,15 +181,30 @@ def _convert_a(*, tag: Tag, text: str, autolinks: bool, default_title: bool) ->
119
181
  return f"{prefix}[{text}]({href}{title_part}){suffix}" if href else text
120
182
 
121
183
 
122
- def _convert_blockquote(*, text: str, convert_as_inline: bool) -> str:
184
+ def _convert_blockquote(*, text: str, tag: Tag, convert_as_inline: bool) -> str:
123
185
  if convert_as_inline:
124
186
  return text
125
- return f"\n{line_beginning_re.sub('> ', text.strip())}\n\n" if text else ""
126
187
 
127
-
128
- def _convert_br(*, convert_as_inline: bool, newline_style: str) -> str:
129
- if convert_as_inline:
188
+ if not text:
130
189
  return ""
190
+
191
+ # Handle cite attribute
192
+ cite_url = tag.get("cite")
193
+ quote_text = f"\n{line_beginning_re.sub('> ', text.strip())}\n\n"
194
+
195
+ if cite_url:
196
+ quote_text += f"— <{cite_url}>\n\n"
197
+
198
+ return quote_text
199
+
200
+
201
+ def _convert_br(*, convert_as_inline: bool, newline_style: str, tag: Tag) -> str:
202
+ # Convert br to line break, but handle headings specially
203
+ if tag.find_parent(["h1", "h2", "h3", "h4", "h5", "h6"]):
204
+ return " " # Convert to space in headings
205
+
206
+ # Always convert br to line break in other contexts
207
+ _ = convert_as_inline # Unused but kept for API consistency
131
208
  return "\\\n" if newline_style.lower() == BACKSLASH else " \n"
132
209
 
133
210
 
@@ -154,13 +231,24 @@ def _convert_hn(
154
231
 
155
232
  def _convert_img(*, tag: Tag, convert_as_inline: bool, keep_inline_images_in: Iterable[str] | None) -> str:
156
233
  alt = tag.attrs.get("alt", "")
234
+ alt = alt if isinstance(alt, str) else ""
157
235
  src = tag.attrs.get("src", "")
236
+ src = src if isinstance(src, str) else ""
158
237
  title = tag.attrs.get("title", "")
238
+ title = title if isinstance(title, str) else ""
239
+ width = tag.attrs.get("width", "")
240
+ width = width if isinstance(width, str) else ""
241
+ height = tag.attrs.get("height", "")
242
+ height = height if isinstance(height, str) else ""
159
243
  title_part = ' "{}"'.format(title.replace('"', r"\"")) if title else ""
160
244
  parent_name = tag.parent.name if tag.parent else ""
161
- if convert_as_inline and parent_name not in (keep_inline_images_in or []):
245
+ # Always preserve images in table cells (td, th) by default
246
+ default_preserve_in = ["td", "th"]
247
+ preserve_in = set(keep_inline_images_in or []) | set(default_preserve_in)
248
+ if convert_as_inline and parent_name not in preserve_in:
162
249
  return alt
163
-
250
+ if width or height:
251
+ return f"<img src='{src}' alt='{alt}' title='{title}' width='{width}' height='{height}' />"
164
252
  return f"![{alt}]({src}{title_part})"
165
253
 
166
254
 
@@ -188,6 +276,17 @@ def _convert_list(*, tag: Tag, text: str) -> str:
188
276
 
189
277
 
190
278
  def _convert_li(*, tag: Tag, text: str, bullets: str) -> str:
279
+ # Check for task list (checkbox input)
280
+ checkbox = tag.find("input", {"type": "checkbox"})
281
+ if checkbox and isinstance(checkbox, Tag):
282
+ checked = checkbox.get("checked") is not None
283
+ checkbox_symbol = "[x]" if checked else "[ ]"
284
+ # Remove the checkbox from the text content
285
+ checkbox_text = text
286
+ if checkbox.string:
287
+ checkbox_text = text.replace(str(checkbox.string), "").strip()
288
+ return f"- {checkbox_symbol} {checkbox_text.strip()}\n"
289
+
191
290
  parent = tag.parent
192
291
  if parent is not None and parent.name == "ol":
193
292
  start = (
@@ -225,6 +324,29 @@ def _convert_p(*, wrap: bool, text: str, convert_as_inline: bool, wrap_width: in
225
324
  return f"{text}\n\n" if text else ""
226
325
 
227
326
 
327
+ def _convert_mark(*, text: str, convert_as_inline: bool, highlight_style: str) -> str:
328
+ """Convert HTML mark element to Markdown highlighting.
329
+
330
+ Args:
331
+ text: The text content of the mark element.
332
+ convert_as_inline: Whether to convert as inline content.
333
+ highlight_style: The style to use for highlighting ("double-equal", "html", "bold").
334
+
335
+ Returns:
336
+ The converted markdown text.
337
+ """
338
+ if convert_as_inline:
339
+ return text
340
+
341
+ if highlight_style == "double-equal":
342
+ return f"=={text}=="
343
+ if highlight_style == "bold":
344
+ return f"**{text}**"
345
+ if highlight_style == "html":
346
+ return f"<mark>{text}</mark>"
347
+ return text
348
+
349
+
228
350
  def _convert_pre(
229
351
  *,
230
352
  tag: Tag,
@@ -253,10 +375,10 @@ def _convert_th(*, tag: Tag, text: str) -> str:
253
375
 
254
376
  def _convert_tr(*, tag: Tag, text: str) -> str:
255
377
  cells = tag.find_all(["td", "th"])
256
- parent_name = tag.parent.name if tag.parent else ""
378
+ parent_name = tag.parent.name if tag.parent and hasattr(tag.parent, "name") else ""
257
379
  tag_grand_parent = tag.parent.parent if tag.parent else None
258
380
  is_headrow = (
259
- all(cell.name == "th" for cell in cells)
381
+ all(hasattr(cell, "name") and cell.name == "th" for cell in cells)
260
382
  or (not tag.previous_sibling and parent_name != "tbody")
261
383
  or (
262
384
  not tag.previous_sibling
@@ -269,8 +391,12 @@ def _convert_tr(*, tag: Tag, text: str) -> str:
269
391
  if is_headrow and not tag.previous_sibling:
270
392
  full_colspan = 0
271
393
  for cell in cells:
272
- if "colspan" in cell.attrs and cell["colspan"].isdigit():
273
- full_colspan += int(cell["colspan"])
394
+ if hasattr(cell, "attrs") and "colspan" in cell.attrs:
395
+ colspan_value = cell.attrs["colspan"]
396
+ if isinstance(colspan_value, str) and colspan_value.isdigit():
397
+ full_colspan += int(colspan_value)
398
+ else:
399
+ full_colspan += 1
274
400
  else:
275
401
  full_colspan += 1
276
402
  underline += "| " + " | ".join(["---"] * full_colspan) + " |" + "\n"
@@ -282,100 +408,1515 @@ def _convert_tr(*, tag: Tag, text: str) -> str:
282
408
  return overline + "|" + text + "\n" + underline
283
409
 
284
410
 
285
- def create_converters_map(
286
- autolinks: bool,
287
- bullets: str,
288
- code_language: str,
289
- code_language_callback: Callable[[Tag], str] | None,
290
- default_title: bool,
291
- heading_style: Literal["atx", "atx_closed", "underlined"],
292
- keep_inline_images_in: Iterable[str] | None,
293
- newline_style: str,
294
- strong_em_symbol: str,
295
- sub_symbol: str,
296
- sup_symbol: str,
297
- wrap: bool,
298
- wrap_width: int,
299
- ) -> ConvertersMap:
300
- """Create a mapping of HTML elements to their corresponding conversion functions.
411
+ def _convert_caption(*, text: str, convert_as_inline: bool) -> str:
412
+ """Convert HTML caption element to emphasized text.
301
413
 
302
414
  Args:
303
- autolinks: Whether to convert URLs into links.
304
- bullets: The bullet characters to use for unordered lists.
305
- code_language: The default code language to use.
306
- code_language_callback: A callback to get the code language.
307
- default_title: Whether to use the URL as the title for links.
308
- heading_style: The style of headings.
309
- keep_inline_images_in: The tags to keep inline images in.
310
- newline_style: The style of newlines.
311
- strong_em_symbol: The symbol to use for strong and emphasis text.
312
- sub_symbol: The symbol to use for subscript text.
313
- sup_symbol: The symbol to use for superscript text.
314
- wrap: Whether to wrap text.
315
- wrap_width: The width to wrap text at.
415
+ text: The text content of the caption element.
416
+ convert_as_inline: Whether to convert as inline content.
316
417
 
317
418
  Returns:
318
- A mapping of HTML elements to their corresponding conversion functions
419
+ The converted markdown text with caption formatting.
319
420
  """
421
+ if convert_as_inline:
422
+ return text
320
423
 
321
- def _wrapper(func: Callable[..., T]) -> Callable[[str, Tag], T]:
322
- spec = getfullargspec(func)
424
+ if not text.strip():
425
+ return ""
323
426
 
324
- def _inner(*, text: str, tag: Tag, convert_as_inline: bool) -> T:
325
- if spec.kwonlyargs:
326
- kwargs: dict[str, Any] = {}
327
- if "tag" in spec.kwonlyargs:
328
- kwargs["tag"] = tag
329
- if "text" in spec.kwonlyargs:
330
- kwargs["text"] = text
331
- if "convert_as_inline" in spec.kwonlyargs:
332
- kwargs["convert_as_inline"] = convert_as_inline
333
- return func(**kwargs)
334
- return func(text)
427
+ return f"*{text.strip()}*\n\n"
335
428
 
336
- return cast("Callable[[str, Tag], T]", _inner)
337
429
 
338
- return {
339
- "a": _wrapper(partial(_convert_a, autolinks=autolinks, default_title=default_title)),
340
- "b": _wrapper(partial(_create_inline_converter(2 * strong_em_symbol))),
341
- "blockquote": _wrapper(partial(_convert_blockquote)),
342
- "br": _wrapper(partial(_convert_br, newline_style=newline_style)),
343
- "code": _wrapper(_create_inline_converter("`")),
344
- "del": _wrapper(_create_inline_converter("~~")),
345
- "em": _wrapper(_create_inline_converter(strong_em_symbol)),
346
- "h1": _wrapper(partial(_convert_hn, n=1, heading_style=heading_style)),
347
- "h2": _wrapper(partial(_convert_hn, n=2, heading_style=heading_style)),
348
- "h3": _wrapper(partial(_convert_hn, n=3, heading_style=heading_style)),
349
- "h4": _wrapper(partial(_convert_hn, n=4, heading_style=heading_style)),
350
- "h5": _wrapper(partial(_convert_hn, n=5, heading_style=heading_style)),
351
- "h6": _wrapper(partial(_convert_hn, n=6, heading_style=heading_style)),
352
- "hr": _wrapper(lambda _: "\n\n---\n\n"),
353
- "i": _wrapper(partial(_create_inline_converter(strong_em_symbol))),
354
- "img": _wrapper(partial(_convert_img, keep_inline_images_in=keep_inline_images_in)),
355
- "list": _wrapper(_convert_list),
356
- "ul": _wrapper(_convert_list),
357
- "ol": _wrapper(_convert_list),
358
- "li": _wrapper(partial(_convert_li, bullets=bullets)),
359
- "p": _wrapper(partial(_convert_p, wrap=wrap, wrap_width=wrap_width)),
360
- "pre": _wrapper(
361
- partial(
362
- _convert_pre,
363
- code_language=code_language,
364
- code_language_callback=code_language_callback,
365
- )
366
- ),
367
- "script": _wrapper(lambda _: ""),
368
- "style": _wrapper(lambda _: ""),
369
- "s": _wrapper(_create_inline_converter("~~")),
370
- "strong": _wrapper(_create_inline_converter(strong_em_symbol * 2)),
371
- "samp": _wrapper(_create_inline_converter("`")),
372
- "sub": _wrapper(_create_inline_converter(sub_symbol)),
373
- "sup": _wrapper(_create_inline_converter(sup_symbol)),
374
- "table": _wrapper(lambda text: f"\n\n{text}\n"),
375
- "caption": _wrapper(lambda text: f"{text}\n"),
376
- "figcaption": _wrapper(lambda text: f"\n\n{text}\n\n"),
377
- "td": _wrapper(_convert_td),
378
- "th": _wrapper(_convert_th),
379
- "tr": _wrapper(_convert_tr),
380
- "kbd": _wrapper(_create_inline_converter("`")),
430
+ def _convert_thead(*, text: str, convert_as_inline: bool) -> str:
431
+ """Convert HTML thead element preserving table structure.
432
+
433
+ Args:
434
+ text: The text content of the thead element.
435
+ convert_as_inline: Whether to convert as inline content.
436
+
437
+ Returns:
438
+ The converted markdown text preserving table structure.
439
+ """
440
+ if convert_as_inline:
441
+ return text
442
+
443
+ return text
444
+
445
+
446
+ def _convert_tbody(*, text: str, convert_as_inline: bool) -> str:
447
+ """Convert HTML tbody element preserving table structure.
448
+
449
+ Args:
450
+ text: The text content of the tbody element.
451
+ convert_as_inline: Whether to convert as inline content.
452
+
453
+ Returns:
454
+ The converted markdown text preserving table structure.
455
+ """
456
+ if convert_as_inline:
457
+ return text
458
+
459
+ return text
460
+
461
+
462
+ def _convert_tfoot(*, text: str, convert_as_inline: bool) -> str:
463
+ """Convert HTML tfoot element preserving table structure.
464
+
465
+ Args:
466
+ text: The text content of the tfoot element.
467
+ convert_as_inline: Whether to convert as inline content.
468
+
469
+ Returns:
470
+ The converted markdown text preserving table structure.
471
+ """
472
+ if convert_as_inline:
473
+ return text
474
+
475
+ return text
476
+
477
+
478
+ def _convert_colgroup(*, tag: Tag, text: str, convert_as_inline: bool) -> str:
479
+ """Convert HTML colgroup element preserving column structure for documentation.
480
+
481
+ Args:
482
+ tag: The colgroup tag element.
483
+ text: The text content of the colgroup element.
484
+ convert_as_inline: Whether to convert as inline content.
485
+
486
+ Returns:
487
+ The converted markdown text preserving colgroup structure.
488
+ """
489
+ if convert_as_inline:
490
+ return text
491
+
492
+ if not text.strip():
493
+ return ""
494
+
495
+ span = tag.get("span", "")
496
+ attrs = []
497
+ if span and isinstance(span, str) and span.strip():
498
+ attrs.append(f'span="{span}"')
499
+
500
+ attrs_str = " ".join(attrs)
501
+ if attrs_str:
502
+ return f"<colgroup {attrs_str}>\n{text.strip()}\n</colgroup>\n\n"
503
+ return f"<colgroup>\n{text.strip()}\n</colgroup>\n\n"
504
+
505
+
506
+ def _convert_col(*, tag: Tag, convert_as_inline: bool) -> str:
507
+ """Convert HTML col element preserving column attributes for documentation.
508
+
509
+ Args:
510
+ tag: The col tag element.
511
+ convert_as_inline: Whether to convert as inline content.
512
+
513
+ Returns:
514
+ The converted markdown text preserving col structure.
515
+ """
516
+ if convert_as_inline:
517
+ return ""
518
+
519
+ span = tag.get("span", "")
520
+ width = tag.get("width", "")
521
+ style = tag.get("style", "")
522
+
523
+ attrs = []
524
+ if width and isinstance(width, str) and width.strip():
525
+ attrs.append(f'width="{width}"')
526
+ if style and isinstance(style, str) and style.strip():
527
+ attrs.append(f'style="{style}"')
528
+ if span and isinstance(span, str) and span.strip():
529
+ attrs.append(f'span="{span}"')
530
+
531
+ attrs_str = " ".join(attrs)
532
+ if attrs_str:
533
+ return f"<col {attrs_str} />\n"
534
+ return "<col />\n"
535
+
536
+
537
+ def _convert_semantic_block(*, text: str, convert_as_inline: bool) -> str:
538
+ """Convert HTML5 semantic elements to block-level Markdown.
539
+
540
+ Args:
541
+ text: The text content of the semantic element.
542
+ convert_as_inline: Whether to convert as inline content.
543
+
544
+ Returns:
545
+ The converted markdown text with proper block spacing.
546
+ """
547
+ if convert_as_inline:
548
+ return text
549
+
550
+ return f"{text}\n\n" if text.strip() else ""
551
+
552
+
553
+ def _convert_details(*, text: str, convert_as_inline: bool) -> str:
554
+ """Convert HTML details element preserving HTML structure.
555
+
556
+ Args:
557
+ text: The text content of the details element.
558
+ convert_as_inline: Whether to convert as inline content.
559
+
560
+ Returns:
561
+ The converted markdown text preserving HTML structure.
562
+ """
563
+ if convert_as_inline:
564
+ return text
565
+
566
+ return f"<details>\n{text.strip()}\n</details>\n\n" if text.strip() else ""
567
+
568
+
569
+ def _convert_summary(*, text: str, convert_as_inline: bool) -> str:
570
+ """Convert HTML summary element preserving HTML structure.
571
+
572
+ Args:
573
+ text: The text content of the summary element.
574
+ convert_as_inline: Whether to convert as inline content.
575
+
576
+ Returns:
577
+ The converted markdown text preserving HTML structure.
578
+ """
579
+ if convert_as_inline:
580
+ return text
581
+
582
+ return f"<summary>{text.strip()}</summary>\n\n" if text.strip() else ""
583
+
584
+
585
+ def _convert_dl(*, text: str, convert_as_inline: bool) -> str:
586
+ """Convert HTML definition list element.
587
+
588
+ Args:
589
+ text: The text content of the definition list.
590
+ convert_as_inline: Whether to convert as inline content.
591
+
592
+ Returns:
593
+ The converted markdown text with proper spacing.
594
+ """
595
+ if convert_as_inline:
596
+ return text
597
+
598
+ return f"{text}\n" if text.strip() else ""
599
+
600
+
601
+ def _convert_dt(*, text: str, convert_as_inline: bool) -> str:
602
+ """Convert HTML definition term element.
603
+
604
+ Args:
605
+ text: The text content of the definition term.
606
+ convert_as_inline: Whether to convert as inline content.
607
+
608
+ Returns:
609
+ The converted markdown text as a definition term.
610
+ """
611
+ if convert_as_inline:
612
+ return text
613
+
614
+ if not text.strip():
615
+ return ""
616
+
617
+ return f"{text.strip()}\n"
618
+
619
+
620
+ def _convert_dd(*, text: str, convert_as_inline: bool) -> str:
621
+ """Convert HTML definition description element.
622
+
623
+ Args:
624
+ text: The text content of the definition description.
625
+ convert_as_inline: Whether to convert as inline content.
626
+
627
+ Returns:
628
+ The converted markdown text as a definition description.
629
+ """
630
+ if convert_as_inline:
631
+ return text
632
+
633
+ if not text.strip():
634
+ return ""
635
+
636
+ return f": {text.strip()}\n\n"
637
+
638
+
639
+ def _convert_cite(*, text: str, convert_as_inline: bool) -> str:
640
+ """Convert HTML cite element to italic text.
641
+
642
+ Args:
643
+ text: The text content of the cite element.
644
+ convert_as_inline: Whether to convert as inline content.
645
+
646
+ Returns:
647
+ The converted markdown text in italic format.
648
+ """
649
+ if convert_as_inline:
650
+ return text
651
+
652
+ if not text.strip():
653
+ return ""
654
+
655
+ return f"*{text.strip()}*"
656
+
657
+
658
+ def _convert_q(*, text: str, convert_as_inline: bool) -> str:
659
+ """Convert HTML q element to quoted text.
660
+
661
+ Args:
662
+ text: The text content of the q element.
663
+ convert_as_inline: Whether to convert as inline content.
664
+
665
+ Returns:
666
+ The converted markdown text with quotes.
667
+ """
668
+ if convert_as_inline:
669
+ return text
670
+
671
+ if not text.strip():
672
+ return ""
673
+
674
+ # Escape any existing quotes in the text
675
+ escaped_text = text.strip().replace('"', '\\"')
676
+ return f'"{escaped_text}"'
677
+
678
+
679
+ def _convert_audio(*, tag: Tag, text: str, convert_as_inline: bool) -> str: # noqa: C901
680
+ """Convert HTML audio element preserving structure with fallback.
681
+
682
+ Args:
683
+ tag: The audio tag element.
684
+ text: The text content of the audio element (fallback content).
685
+ convert_as_inline: Whether to convert as inline content.
686
+
687
+ Returns:
688
+ The converted markdown text preserving audio element.
689
+ """
690
+ _ = convert_as_inline # Unused but kept for API consistency
691
+ src = tag.get("src", "")
692
+
693
+ # Check for source elements if no src attribute
694
+ if not src:
695
+ source_tag = tag.find("source")
696
+ if source_tag and isinstance(source_tag, Tag):
697
+ src = source_tag.get("src", "")
698
+
699
+ # Get other attributes
700
+ controls = "controls" if tag.get("controls") is not None else ""
701
+ autoplay = "autoplay" if tag.get("autoplay") is not None else ""
702
+ loop = "loop" if tag.get("loop") is not None else ""
703
+ muted = "muted" if tag.get("muted") is not None else ""
704
+ preload = tag.get("preload", "")
705
+
706
+ # Build attributes string
707
+ attrs = []
708
+ if src and isinstance(src, str) and src.strip():
709
+ attrs.append(f'src="{src}"')
710
+ if controls:
711
+ attrs.append(controls)
712
+ if autoplay:
713
+ attrs.append(autoplay)
714
+ if loop:
715
+ attrs.append(loop)
716
+ if muted:
717
+ attrs.append(muted)
718
+ if preload and isinstance(preload, str) and preload.strip():
719
+ attrs.append(f'preload="{preload}"')
720
+
721
+ attrs_str = " ".join(attrs)
722
+
723
+ # If there's fallback content, preserve it
724
+ if text.strip():
725
+ if attrs_str:
726
+ return f"<audio {attrs_str}>\n{text.strip()}\n</audio>\n\n"
727
+ return f"<audio>\n{text.strip()}\n</audio>\n\n"
728
+
729
+ # Self-closing for no fallback content
730
+ if attrs_str:
731
+ return f"<audio {attrs_str} />\n\n"
732
+ return "<audio />\n\n"
733
+
734
+
735
+ def _convert_video(*, tag: Tag, text: str, convert_as_inline: bool) -> str: # noqa: C901, PLR0912
736
+ """Convert HTML video element preserving structure with fallback.
737
+
738
+ Args:
739
+ tag: The video tag element.
740
+ text: The text content of the video element (fallback content).
741
+ convert_as_inline: Whether to convert as inline content.
742
+
743
+ Returns:
744
+ The converted markdown text preserving video element.
745
+ """
746
+ _ = convert_as_inline # Unused but kept for API consistency
747
+ src = tag.get("src", "")
748
+
749
+ # Check for source elements if no src attribute
750
+ if not src:
751
+ source_tag = tag.find("source")
752
+ if source_tag and isinstance(source_tag, Tag):
753
+ src = source_tag.get("src", "")
754
+
755
+ # Get other attributes
756
+ width = tag.get("width", "")
757
+ height = tag.get("height", "")
758
+ poster = tag.get("poster", "")
759
+ controls = "controls" if tag.get("controls") is not None else ""
760
+ autoplay = "autoplay" if tag.get("autoplay") is not None else ""
761
+ loop = "loop" if tag.get("loop") is not None else ""
762
+ muted = "muted" if tag.get("muted") is not None else ""
763
+ preload = tag.get("preload", "")
764
+
765
+ # Build attributes string
766
+ attrs = []
767
+ if src and isinstance(src, str) and src.strip():
768
+ attrs.append(f'src="{src}"')
769
+ if width and isinstance(width, str) and width.strip():
770
+ attrs.append(f'width="{width}"')
771
+ if height and isinstance(height, str) and height.strip():
772
+ attrs.append(f'height="{height}"')
773
+ if poster and isinstance(poster, str) and poster.strip():
774
+ attrs.append(f'poster="{poster}"')
775
+ if controls:
776
+ attrs.append(controls)
777
+ if autoplay:
778
+ attrs.append(autoplay)
779
+ if loop:
780
+ attrs.append(loop)
781
+ if muted:
782
+ attrs.append(muted)
783
+ if preload and isinstance(preload, str) and preload.strip():
784
+ attrs.append(f'preload="{preload}"')
785
+
786
+ attrs_str = " ".join(attrs)
787
+
788
+ # If there's fallback content, preserve it
789
+ if text.strip():
790
+ if attrs_str:
791
+ return f"<video {attrs_str}>\n{text.strip()}\n</video>\n\n"
792
+ return f"<video>\n{text.strip()}\n</video>\n\n"
793
+
794
+ # Self-closing for no fallback content
795
+ if attrs_str:
796
+ return f"<video {attrs_str} />\n\n"
797
+ return "<video />\n\n"
798
+
799
+
800
+ def _convert_iframe(*, tag: Tag, text: str, convert_as_inline: bool) -> str: # noqa: C901, PLR0912
801
+ """Convert HTML iframe element preserving structure.
802
+
803
+ Args:
804
+ tag: The iframe tag element.
805
+ text: The text content of the iframe element (usually empty).
806
+ convert_as_inline: Whether to convert as inline content.
807
+
808
+ Returns:
809
+ The converted markdown text preserving iframe element.
810
+ """
811
+ _ = text # Unused but kept for API consistency
812
+ _ = convert_as_inline # Unused but kept for API consistency
813
+ src = tag.get("src", "")
814
+ width = tag.get("width", "")
815
+ height = tag.get("height", "")
816
+ title = tag.get("title", "")
817
+ allow = tag.get("allow", "")
818
+ sandbox = tag.get("sandbox") # Don't provide default
819
+ loading = tag.get("loading", "")
820
+
821
+ # Build attributes string
822
+ attrs = []
823
+ if src and isinstance(src, str) and src.strip():
824
+ attrs.append(f'src="{src}"')
825
+ if width and isinstance(width, str) and width.strip():
826
+ attrs.append(f'width="{width}"')
827
+ if height and isinstance(height, str) and height.strip():
828
+ attrs.append(f'height="{height}"')
829
+ if title and isinstance(title, str) and title.strip():
830
+ attrs.append(f'title="{title}"')
831
+ if allow and isinstance(allow, str) and allow.strip():
832
+ attrs.append(f'allow="{allow}"')
833
+ if sandbox is not None:
834
+ if isinstance(sandbox, list):
835
+ # BeautifulSoup returns AttributeValueList for space-separated values
836
+ if sandbox:
837
+ attrs.append(f'sandbox="{" ".join(sandbox)}"')
838
+ else:
839
+ # Empty list means boolean attribute
840
+ attrs.append("sandbox")
841
+ elif isinstance(sandbox, str) and sandbox:
842
+ attrs.append(f'sandbox="{sandbox}"')
843
+ else:
844
+ attrs.append("sandbox")
845
+ if loading and isinstance(loading, str) and loading.strip():
846
+ attrs.append(f'loading="{loading}"')
847
+
848
+ attrs_str = " ".join(attrs)
849
+
850
+ # iframes are typically self-closing in usage
851
+ if attrs_str:
852
+ return f"<iframe {attrs_str}></iframe>\n\n"
853
+ return "<iframe></iframe>\n\n"
854
+
855
+
856
+ def _convert_abbr(*, tag: Tag, text: str, convert_as_inline: bool) -> str:
857
+ """Convert HTML abbr element to text with optional title.
858
+
859
+ Args:
860
+ tag: The abbr tag element.
861
+ text: The text content of the abbr element.
862
+ convert_as_inline: Whether to convert as inline content.
863
+
864
+ Returns:
865
+ The converted markdown text with optional title annotation.
866
+ """
867
+ _ = convert_as_inline # Unused but kept for API consistency
868
+ if not text.strip():
869
+ return ""
870
+
871
+ title = tag.get("title")
872
+ if title and isinstance(title, str) and title.strip():
873
+ # Show abbreviation with title in parentheses
874
+ return f"{text.strip()} ({title.strip()})"
875
+
876
+ return text.strip()
877
+
878
+
879
+ def _convert_time(*, tag: Tag, text: str, convert_as_inline: bool) -> str:
880
+ """Convert HTML time element preserving datetime attribute.
881
+
882
+ Args:
883
+ tag: The time tag element.
884
+ text: The text content of the time element.
885
+ convert_as_inline: Whether to convert as inline content.
886
+
887
+ Returns:
888
+ The converted markdown text preserving time information.
889
+ """
890
+ _ = convert_as_inline # Unused but kept for API consistency
891
+ if not text.strip():
892
+ return ""
893
+
894
+ datetime_attr = tag.get("datetime")
895
+ if datetime_attr and isinstance(datetime_attr, str) and datetime_attr.strip():
896
+ # Preserve machine-readable datetime in HTML
897
+ return f'<time datetime="{datetime_attr.strip()}">{text.strip()}</time>'
898
+
899
+ return text.strip()
900
+
901
+
902
+ def _convert_data(*, tag: Tag, text: str, convert_as_inline: bool) -> str:
903
+ """Convert HTML data element preserving value attribute.
904
+
905
+ Args:
906
+ tag: The data tag element.
907
+ text: The text content of the data element.
908
+ convert_as_inline: Whether to convert as inline content.
909
+
910
+ Returns:
911
+ The converted markdown text preserving machine-readable data.
912
+ """
913
+ _ = convert_as_inline # Unused but kept for API consistency
914
+ if not text.strip():
915
+ return ""
916
+
917
+ value_attr = tag.get("value")
918
+ if value_attr and isinstance(value_attr, str) and value_attr.strip():
919
+ # Preserve machine-readable value in HTML
920
+ return f'<data value="{value_attr.strip()}">{text.strip()}</data>'
921
+
922
+ return text.strip()
923
+
924
+
925
+ def _convert_wbr(*, convert_as_inline: bool) -> str:
926
+ """Convert HTML wbr (word break opportunity) element.
927
+
928
+ Args:
929
+ convert_as_inline: Whether to convert as inline content.
930
+
931
+ Returns:
932
+ Empty string as wbr is just a break opportunity.
933
+ """
934
+ _ = convert_as_inline # Unused but kept for API consistency
935
+ return "" # Word break opportunity doesn't produce visible output
936
+
937
+
938
+ def _convert_form(*, tag: Tag, text: str, convert_as_inline: bool) -> str:
939
+ """Convert HTML form element preserving structure for documentation.
940
+
941
+ Args:
942
+ tag: The form tag element.
943
+ text: The text content of the form element.
944
+ convert_as_inline: Whether to convert as inline content.
945
+
946
+ Returns:
947
+ The converted markdown text preserving form structure.
948
+ """
949
+ if convert_as_inline:
950
+ return text
951
+
952
+ if not text.strip():
953
+ return ""
954
+
955
+ action = tag.get("action", "")
956
+ method = tag.get("method", "")
957
+ attrs = []
958
+
959
+ if action and isinstance(action, str) and action.strip():
960
+ attrs.append(f'action="{action.strip()}"')
961
+ if method and isinstance(method, str) and method.strip():
962
+ attrs.append(f'method="{method.strip()}"')
963
+
964
+ attrs_str = " ".join(attrs)
965
+ if attrs_str:
966
+ return f"<form {attrs_str}>\n{text.strip()}\n</form>\n\n"
967
+ return f"<form>\n{text.strip()}\n</form>\n\n"
968
+
969
+
970
+ def _convert_fieldset(*, text: str, convert_as_inline: bool) -> str:
971
+ """Convert HTML fieldset element preserving structure.
972
+
973
+ Args:
974
+ text: The text content of the fieldset element.
975
+ convert_as_inline: Whether to convert as inline content.
976
+
977
+ Returns:
978
+ The converted markdown text preserving fieldset structure.
979
+ """
980
+ if convert_as_inline:
981
+ return text
982
+
983
+ if not text.strip():
984
+ return ""
985
+
986
+ return f"<fieldset>\n{text.strip()}\n</fieldset>\n\n"
987
+
988
+
989
+ def _convert_legend(*, text: str, convert_as_inline: bool) -> str:
990
+ """Convert HTML legend element to emphasized text.
991
+
992
+ Args:
993
+ text: The text content of the legend element.
994
+ convert_as_inline: Whether to convert as inline content.
995
+
996
+ Returns:
997
+ The converted markdown text as emphasized legend.
998
+ """
999
+ if convert_as_inline:
1000
+ return text
1001
+
1002
+ if not text.strip():
1003
+ return ""
1004
+
1005
+ return f"<legend>{text.strip()}</legend>\n\n"
1006
+
1007
+
1008
+ def _convert_label(*, tag: Tag, text: str, convert_as_inline: bool) -> str:
1009
+ """Convert HTML label element preserving for attribute.
1010
+
1011
+ Args:
1012
+ tag: The label tag element.
1013
+ text: The text content of the label element.
1014
+ convert_as_inline: Whether to convert as inline content.
1015
+
1016
+ Returns:
1017
+ The converted markdown text preserving label structure.
1018
+ """
1019
+ if convert_as_inline:
1020
+ return text
1021
+
1022
+ if not text.strip():
1023
+ return ""
1024
+
1025
+ for_attr = tag.get("for")
1026
+ if for_attr and isinstance(for_attr, str) and for_attr.strip():
1027
+ return f'<label for="{for_attr.strip()}">{text.strip()}</label>\n\n'
1028
+
1029
+ return f"<label>{text.strip()}</label>\n\n"
1030
+
1031
+
1032
+ def _convert_input_enhanced(*, tag: Tag, convert_as_inline: bool) -> str: # noqa: C901
1033
+ """Convert HTML input element preserving all relevant attributes.
1034
+
1035
+ Args:
1036
+ tag: The input tag element.
1037
+ convert_as_inline: Whether to convert as inline content.
1038
+
1039
+ Returns:
1040
+ The converted markdown text preserving input structure.
1041
+ """
1042
+ input_type = tag.get("type", "text")
1043
+
1044
+ # Special handling for inputs in list items - let _convert_li handle checkboxes
1045
+ # and ignore other input types in list items (legacy behavior)
1046
+ if tag.find_parent("li"):
1047
+ return ""
1048
+
1049
+ id_attr = tag.get("id", "")
1050
+ name = tag.get("name", "")
1051
+ value = tag.get("value", "")
1052
+ placeholder = tag.get("placeholder", "")
1053
+ required = tag.get("required") is not None
1054
+ disabled = tag.get("disabled") is not None
1055
+ readonly = tag.get("readonly") is not None
1056
+ checked = tag.get("checked") is not None
1057
+ accept = tag.get("accept", "")
1058
+
1059
+ attrs = []
1060
+ if input_type and isinstance(input_type, str):
1061
+ attrs.append(f'type="{input_type}"')
1062
+ if id_attr and isinstance(id_attr, str) and id_attr.strip():
1063
+ attrs.append(f'id="{id_attr}"')
1064
+ if name and isinstance(name, str) and name.strip():
1065
+ attrs.append(f'name="{name}"')
1066
+ if value and isinstance(value, str) and value.strip():
1067
+ attrs.append(f'value="{value}"')
1068
+ if placeholder and isinstance(placeholder, str) and placeholder.strip():
1069
+ attrs.append(f'placeholder="{placeholder}"')
1070
+ if accept and isinstance(accept, str) and accept.strip():
1071
+ attrs.append(f'accept="{accept}"')
1072
+ if required:
1073
+ attrs.append("required")
1074
+ if disabled:
1075
+ attrs.append("disabled")
1076
+ if readonly:
1077
+ attrs.append("readonly")
1078
+ if checked:
1079
+ attrs.append("checked")
1080
+
1081
+ attrs_str = " ".join(attrs)
1082
+ result = f"<input {attrs_str} />" if attrs_str else "<input />"
1083
+
1084
+ return result if convert_as_inline else f"{result}\n\n"
1085
+
1086
+
1087
+ def _convert_textarea(*, tag: Tag, text: str, convert_as_inline: bool) -> str:
1088
+ """Convert HTML textarea element preserving attributes.
1089
+
1090
+ Args:
1091
+ tag: The textarea tag element.
1092
+ text: The text content of the textarea element.
1093
+ convert_as_inline: Whether to convert as inline content.
1094
+
1095
+ Returns:
1096
+ The converted markdown text preserving textarea structure.
1097
+ """
1098
+ if convert_as_inline:
1099
+ return text
1100
+
1101
+ if not text.strip():
1102
+ return ""
1103
+
1104
+ name = tag.get("name", "")
1105
+ placeholder = tag.get("placeholder", "")
1106
+ rows = tag.get("rows", "")
1107
+ cols = tag.get("cols", "")
1108
+ required = tag.get("required") is not None
1109
+
1110
+ attrs = []
1111
+ if name and isinstance(name, str) and name.strip():
1112
+ attrs.append(f'name="{name}"')
1113
+ if placeholder and isinstance(placeholder, str) and placeholder.strip():
1114
+ attrs.append(f'placeholder="{placeholder}"')
1115
+ if rows and isinstance(rows, str) and rows.strip():
1116
+ attrs.append(f'rows="{rows}"')
1117
+ if cols and isinstance(cols, str) and cols.strip():
1118
+ attrs.append(f'cols="{cols}"')
1119
+ if required:
1120
+ attrs.append("required")
1121
+
1122
+ attrs_str = " ".join(attrs)
1123
+ content = text.strip()
1124
+
1125
+ if attrs_str:
1126
+ return f"<textarea {attrs_str}>{content}</textarea>\n\n"
1127
+ return f"<textarea>{content}</textarea>\n\n"
1128
+
1129
+
1130
+ def _convert_select(*, tag: Tag, text: str, convert_as_inline: bool) -> str:
1131
+ """Convert HTML select element preserving structure.
1132
+
1133
+ Args:
1134
+ tag: The select tag element.
1135
+ text: The text content of the select element.
1136
+ convert_as_inline: Whether to convert as inline content.
1137
+
1138
+ Returns:
1139
+ The converted markdown text preserving select structure.
1140
+ """
1141
+ if convert_as_inline:
1142
+ return text
1143
+
1144
+ if not text.strip():
1145
+ return ""
1146
+
1147
+ id_attr = tag.get("id", "")
1148
+ name = tag.get("name", "")
1149
+ multiple = tag.get("multiple") is not None
1150
+ required = tag.get("required") is not None
1151
+
1152
+ attrs = []
1153
+ if id_attr and isinstance(id_attr, str) and id_attr.strip():
1154
+ attrs.append(f'id="{id_attr}"')
1155
+ if name and isinstance(name, str) and name.strip():
1156
+ attrs.append(f'name="{name}"')
1157
+ if multiple:
1158
+ attrs.append("multiple")
1159
+ if required:
1160
+ attrs.append("required")
1161
+
1162
+ attrs_str = " ".join(attrs)
1163
+ content = text.strip()
1164
+
1165
+ if attrs_str:
1166
+ return f"<select {attrs_str}>\n{content}\n</select>\n\n"
1167
+ return f"<select>\n{content}\n</select>\n\n"
1168
+
1169
+
1170
+ def _convert_option(*, tag: Tag, text: str, convert_as_inline: bool) -> str:
1171
+ """Convert HTML option element preserving value and selected state.
1172
+
1173
+ Args:
1174
+ tag: The option tag element.
1175
+ text: The text content of the option element.
1176
+ convert_as_inline: Whether to convert as inline content.
1177
+
1178
+ Returns:
1179
+ The converted markdown text preserving option structure.
1180
+ """
1181
+ if convert_as_inline:
1182
+ return text
1183
+
1184
+ if not text.strip():
1185
+ return ""
1186
+
1187
+ value = tag.get("value", "")
1188
+ selected = tag.get("selected") is not None
1189
+
1190
+ attrs = []
1191
+ if value and isinstance(value, str) and value.strip():
1192
+ attrs.append(f'value="{value}"')
1193
+ if selected:
1194
+ attrs.append("selected")
1195
+
1196
+ attrs_str = " ".join(attrs)
1197
+ content = text.strip()
1198
+
1199
+ if attrs_str:
1200
+ return f"<option {attrs_str}>{content}</option>\n"
1201
+ return f"<option>{content}</option>\n"
1202
+
1203
+
1204
+ def _convert_optgroup(*, tag: Tag, text: str, convert_as_inline: bool) -> str:
1205
+ """Convert HTML optgroup element preserving label.
1206
+
1207
+ Args:
1208
+ tag: The optgroup tag element.
1209
+ text: The text content of the optgroup element.
1210
+ convert_as_inline: Whether to convert as inline content.
1211
+
1212
+ Returns:
1213
+ The converted markdown text preserving optgroup structure.
1214
+ """
1215
+ if convert_as_inline:
1216
+ return text
1217
+
1218
+ if not text.strip():
1219
+ return ""
1220
+
1221
+ label = tag.get("label", "")
1222
+
1223
+ attrs = []
1224
+ if label and isinstance(label, str) and label.strip():
1225
+ attrs.append(f'label="{label}"')
1226
+
1227
+ attrs_str = " ".join(attrs)
1228
+ content = text.strip()
1229
+
1230
+ if attrs_str:
1231
+ return f"<optgroup {attrs_str}>\n{content}\n</optgroup>\n"
1232
+ return f"<optgroup>\n{content}\n</optgroup>\n"
1233
+
1234
+
1235
+ def _convert_button(*, tag: Tag, text: str, convert_as_inline: bool) -> str:
1236
+ """Convert HTML button element preserving type and attributes.
1237
+
1238
+ Args:
1239
+ tag: The button tag element.
1240
+ text: The text content of the button element.
1241
+ convert_as_inline: Whether to convert as inline content.
1242
+
1243
+ Returns:
1244
+ The converted markdown text preserving button structure.
1245
+ """
1246
+ if convert_as_inline:
1247
+ return text
1248
+
1249
+ if not text.strip():
1250
+ return ""
1251
+
1252
+ button_type = tag.get("type", "")
1253
+ name = tag.get("name", "")
1254
+ value = tag.get("value", "")
1255
+ disabled = tag.get("disabled") is not None
1256
+
1257
+ attrs = []
1258
+ if button_type and isinstance(button_type, str) and button_type.strip():
1259
+ attrs.append(f'type="{button_type}"')
1260
+ if name and isinstance(name, str) and name.strip():
1261
+ attrs.append(f'name="{name}"')
1262
+ if value and isinstance(value, str) and value.strip():
1263
+ attrs.append(f'value="{value}"')
1264
+ if disabled:
1265
+ attrs.append("disabled")
1266
+
1267
+ attrs_str = " ".join(attrs)
1268
+
1269
+ if attrs_str:
1270
+ return f"<button {attrs_str}>{text.strip()}</button>\n\n"
1271
+ return f"<button>{text.strip()}</button>\n\n"
1272
+
1273
+
1274
+ def _convert_progress(*, tag: Tag, text: str, convert_as_inline: bool) -> str:
1275
+ """Convert HTML progress element preserving value and max.
1276
+
1277
+ Args:
1278
+ tag: The progress tag element.
1279
+ text: The text content of the progress element.
1280
+ convert_as_inline: Whether to convert as inline content.
1281
+
1282
+ Returns:
1283
+ The converted markdown text preserving progress structure.
1284
+ """
1285
+ if convert_as_inline:
1286
+ return text
1287
+
1288
+ if not text.strip():
1289
+ return ""
1290
+
1291
+ value = tag.get("value", "")
1292
+ max_val = tag.get("max", "")
1293
+
1294
+ attrs = []
1295
+ if value and isinstance(value, str) and value.strip():
1296
+ attrs.append(f'value="{value}"')
1297
+ if max_val and isinstance(max_val, str) and max_val.strip():
1298
+ attrs.append(f'max="{max_val}"')
1299
+
1300
+ attrs_str = " ".join(attrs)
1301
+ content = text.strip()
1302
+
1303
+ if attrs_str:
1304
+ return f"<progress {attrs_str}>{content}</progress>\n\n"
1305
+ return f"<progress>{content}</progress>\n\n"
1306
+
1307
+
1308
+ def _convert_meter(*, tag: Tag, text: str, convert_as_inline: bool) -> str:
1309
+ """Convert HTML meter element preserving value and range attributes.
1310
+
1311
+ Args:
1312
+ tag: The meter tag element.
1313
+ text: The text content of the meter element.
1314
+ convert_as_inline: Whether to convert as inline content.
1315
+
1316
+ Returns:
1317
+ The converted markdown text preserving meter structure.
1318
+ """
1319
+ if convert_as_inline:
1320
+ return text
1321
+
1322
+ if not text.strip():
1323
+ return ""
1324
+
1325
+ value = tag.get("value", "")
1326
+ min_val = tag.get("min", "")
1327
+ max_val = tag.get("max", "")
1328
+ low = tag.get("low", "")
1329
+ high = tag.get("high", "")
1330
+ optimum = tag.get("optimum", "")
1331
+
1332
+ attrs = []
1333
+ if value and isinstance(value, str) and value.strip():
1334
+ attrs.append(f'value="{value}"')
1335
+ if min_val and isinstance(min_val, str) and min_val.strip():
1336
+ attrs.append(f'min="{min_val}"')
1337
+ if max_val and isinstance(max_val, str) and max_val.strip():
1338
+ attrs.append(f'max="{max_val}"')
1339
+ if low and isinstance(low, str) and low.strip():
1340
+ attrs.append(f'low="{low}"')
1341
+ if high and isinstance(high, str) and high.strip():
1342
+ attrs.append(f'high="{high}"')
1343
+ if optimum and isinstance(optimum, str) and optimum.strip():
1344
+ attrs.append(f'optimum="{optimum}"')
1345
+
1346
+ attrs_str = " ".join(attrs)
1347
+ content = text.strip()
1348
+
1349
+ if attrs_str:
1350
+ return f"<meter {attrs_str}>{content}</meter>\n\n"
1351
+ return f"<meter>{content}</meter>\n\n"
1352
+
1353
+
1354
+ def _convert_output(*, tag: Tag, text: str, convert_as_inline: bool) -> str:
1355
+ """Convert HTML output element preserving for and name attributes.
1356
+
1357
+ Args:
1358
+ tag: The output tag element.
1359
+ text: The text content of the output element.
1360
+ convert_as_inline: Whether to convert as inline content.
1361
+
1362
+ Returns:
1363
+ The converted markdown text preserving output structure.
1364
+ """
1365
+ if convert_as_inline:
1366
+ return text
1367
+
1368
+ if not text.strip():
1369
+ return ""
1370
+
1371
+ for_attr = tag.get("for", "")
1372
+ name = tag.get("name", "")
1373
+
1374
+ attrs = []
1375
+ if for_attr:
1376
+ # BeautifulSoup returns space-separated attributes as lists
1377
+ for_value = " ".join(for_attr) if isinstance(for_attr, list) else str(for_attr)
1378
+ if for_value.strip():
1379
+ attrs.append(f'for="{for_value}"')
1380
+ if name and isinstance(name, str) and name.strip():
1381
+ attrs.append(f'name="{name}"')
1382
+
1383
+ attrs_str = " ".join(attrs)
1384
+
1385
+ if attrs_str:
1386
+ return f"<output {attrs_str}>{text.strip()}</output>\n\n"
1387
+ return f"<output>{text.strip()}</output>\n\n"
1388
+
1389
+
1390
+ def _convert_datalist(*, tag: Tag, text: str, convert_as_inline: bool) -> str:
1391
+ """Convert HTML datalist element preserving structure.
1392
+
1393
+ Args:
1394
+ tag: The datalist tag element.
1395
+ text: The text content of the datalist element.
1396
+ convert_as_inline: Whether to convert as inline content.
1397
+
1398
+ Returns:
1399
+ The converted markdown text preserving datalist structure.
1400
+ """
1401
+ if convert_as_inline:
1402
+ return text
1403
+
1404
+ if not text.strip():
1405
+ return ""
1406
+
1407
+ id_attr = tag.get("id", "")
1408
+
1409
+ attrs = []
1410
+ if id_attr and isinstance(id_attr, str) and id_attr.strip():
1411
+ attrs.append(f'id="{id_attr}"')
1412
+
1413
+ attrs_str = " ".join(attrs)
1414
+ content = text.strip()
1415
+
1416
+ if attrs_str:
1417
+ return f"<datalist {attrs_str}>\n{content}\n</datalist>\n\n"
1418
+ return f"<datalist>\n{content}\n</datalist>\n\n"
1419
+
1420
+
1421
+ def _convert_ruby(*, text: str, convert_as_inline: bool) -> str: # noqa: ARG001
1422
+ """Convert HTML ruby element providing pronunciation annotation.
1423
+
1424
+ Args:
1425
+ text: The text content of the ruby element.
1426
+ convert_as_inline: Whether to convert as inline content.
1427
+
1428
+ Returns:
1429
+ The converted markdown text with ruby annotation as fallback text.
1430
+ """
1431
+ if not text.strip():
1432
+ return ""
1433
+
1434
+ # Ruby elements are always inline by nature
1435
+ return text.strip()
1436
+
1437
+
1438
+ def _convert_rb(*, text: str, convert_as_inline: bool) -> str: # noqa: ARG001
1439
+ """Convert HTML rb (ruby base) element.
1440
+
1441
+ Args:
1442
+ text: The text content of the rb element.
1443
+ convert_as_inline: Whether to convert as inline content.
1444
+
1445
+ Returns:
1446
+ The converted markdown text (ruby base text).
1447
+ """
1448
+ if not text.strip():
1449
+ return ""
1450
+
1451
+ # Ruby base is the main text, pass through as-is
1452
+ return text.strip()
1453
+
1454
+
1455
+ def _convert_rt(*, text: str, convert_as_inline: bool, tag: Tag) -> str: # noqa: ARG001
1456
+ """Convert HTML rt (ruby text) element for pronunciation.
1457
+
1458
+ Args:
1459
+ text: The text content of the rt element.
1460
+ convert_as_inline: Whether to convert as inline content.
1461
+ tag: The rt tag element.
1462
+
1463
+ Returns:
1464
+ The converted markdown text with pronunciation in parentheses.
1465
+ """
1466
+ # Handle empty rt elements - still need parentheses
1467
+ content = text.strip()
1468
+
1469
+ # Check if this rt is surrounded by rp elements (fallback parentheses)
1470
+ prev_sibling = tag.previous_sibling
1471
+ next_sibling = tag.next_sibling
1472
+
1473
+ # If surrounded by rp elements, don't add extra parentheses
1474
+ has_rp_before = prev_sibling and getattr(prev_sibling, "name", None) == "rp"
1475
+ has_rp_after = next_sibling and getattr(next_sibling, "name", None) == "rp"
1476
+
1477
+ if has_rp_before and has_rp_after:
1478
+ # Already has rp parentheses, just return the text
1479
+ return content
1480
+ # Ruby text (pronunciation) shown in parentheses as fallback
1481
+ return f"({content})"
1482
+
1483
+
1484
+ def _convert_rp(*, text: str, convert_as_inline: bool) -> str: # noqa: ARG001
1485
+ """Convert HTML rp (ruby parentheses) element for fallback.
1486
+
1487
+ Args:
1488
+ text: The text content of the rp element.
1489
+ convert_as_inline: Whether to convert as inline content.
1490
+
1491
+ Returns:
1492
+ The converted markdown text (parentheses for ruby fallback).
1493
+ """
1494
+ if not text.strip():
1495
+ return ""
1496
+
1497
+ # Ruby parentheses preserved for fallback compatibility
1498
+ return text.strip()
1499
+
1500
+
1501
+ def _convert_rtc(*, text: str, convert_as_inline: bool) -> str: # noqa: ARG001
1502
+ """Convert HTML rtc (ruby text container) element.
1503
+
1504
+ Args:
1505
+ text: The text content of the rtc element.
1506
+ convert_as_inline: Whether to convert as inline content.
1507
+
1508
+ Returns:
1509
+ The converted markdown text (ruby text container).
1510
+ """
1511
+ if not text.strip():
1512
+ return ""
1513
+
1514
+ # Ruby text container, pass through content
1515
+ return text.strip()
1516
+
1517
+
1518
+ def _convert_dialog(*, text: str, convert_as_inline: bool, tag: Tag) -> str:
1519
+ """Convert HTML dialog element preserving structure with attributes.
1520
+
1521
+ Args:
1522
+ text: The text content of the dialog element.
1523
+ convert_as_inline: Whether to convert as inline content.
1524
+ tag: The dialog tag element.
1525
+
1526
+ Returns:
1527
+ The converted markdown text preserving dialog structure.
1528
+ """
1529
+ if convert_as_inline:
1530
+ return text
1531
+
1532
+ if not text.strip():
1533
+ return ""
1534
+
1535
+ # Get dialog attributes for preservation
1536
+ attrs = []
1537
+ if tag.get("open") is not None:
1538
+ attrs.append("open")
1539
+ if tag.get("id"):
1540
+ attrs.append(f'id="{tag.get("id")}"')
1541
+
1542
+ attrs_str = " " + " ".join(attrs) if attrs else ""
1543
+
1544
+ return f"<dialog{attrs_str}>\n{text.strip()}\n</dialog>\n\n"
1545
+
1546
+
1547
+ def _convert_menu(*, text: str, convert_as_inline: bool, tag: Tag) -> str:
1548
+ """Convert HTML menu element preserving structure with attributes.
1549
+
1550
+ Args:
1551
+ text: The text content of the menu element.
1552
+ convert_as_inline: Whether to convert as inline content.
1553
+ tag: The menu tag element.
1554
+
1555
+ Returns:
1556
+ The converted markdown text preserving menu structure.
1557
+ """
1558
+ if convert_as_inline:
1559
+ return text
1560
+
1561
+ if not text.strip():
1562
+ return ""
1563
+
1564
+ # Get menu attributes for preservation
1565
+ attrs = []
1566
+ if tag.get("type") and tag.get("type") != "list":
1567
+ attrs.append(f'type="{tag.get("type")}"')
1568
+ if tag.get("label"):
1569
+ attrs.append(f'label="{tag.get("label")}"')
1570
+ if tag.get("id"):
1571
+ attrs.append(f'id="{tag.get("id")}"')
1572
+
1573
+ attrs_str = " " + " ".join(attrs) if attrs else ""
1574
+
1575
+ return f"<menu{attrs_str}>\n{text.strip()}\n</menu>\n\n"
1576
+
1577
+
1578
+ def _convert_figure(*, text: str, convert_as_inline: bool, tag: Tag) -> str:
1579
+ """Convert HTML figure element preserving semantic structure.
1580
+
1581
+ Args:
1582
+ text: The text content of the figure element.
1583
+ convert_as_inline: Whether to convert as inline content.
1584
+ tag: The figure tag element.
1585
+
1586
+ Returns:
1587
+ The converted markdown text preserving figure structure.
1588
+ """
1589
+ if not text.strip():
1590
+ return ""
1591
+
1592
+ if convert_as_inline:
1593
+ return text
1594
+
1595
+ # Get figure attributes for preservation
1596
+ attrs = []
1597
+ if tag.get("id"):
1598
+ attrs.append(f'id="{tag.get("id")}"')
1599
+ if tag.get("class"):
1600
+ # Handle class attribute which might be a list
1601
+ class_val = tag.get("class")
1602
+ if isinstance(class_val, list):
1603
+ class_val = " ".join(class_val)
1604
+ attrs.append(f'class="{class_val}"')
1605
+
1606
+ attrs_str = " " + " ".join(attrs) if attrs else ""
1607
+
1608
+ # Check if the figure contains only an image (common case)
1609
+ # In that case, we might want to preserve the figure wrapper
1610
+ content = text.strip()
1611
+
1612
+ # If content already has proper spacing, don't add extra newlines
1613
+ if content.endswith("\n\n"):
1614
+ return f"<figure{attrs_str}>\n{content}</figure>\n\n"
1615
+
1616
+ return f"<figure{attrs_str}>\n{content}\n</figure>\n\n"
1617
+
1618
+
1619
+ def _convert_hgroup(*, text: str, convert_as_inline: bool) -> str:
1620
+ """Convert HTML hgroup element preserving heading group semantics.
1621
+
1622
+ Args:
1623
+ text: The text content of the hgroup element.
1624
+ convert_as_inline: Whether to convert as inline content.
1625
+
1626
+ Returns:
1627
+ The converted markdown text preserving heading group structure.
1628
+ """
1629
+ if convert_as_inline:
1630
+ return text
1631
+
1632
+ if not text.strip():
1633
+ return ""
1634
+
1635
+ # Preserve the semantic grouping of headings
1636
+ # Add a marker to indicate this is a grouped heading
1637
+ content = text.strip()
1638
+
1639
+ # Remove excessive newlines between headings in the group
1640
+ # Headings in hgroup should be visually closer together
1641
+ content = re.sub(r"\n{3,}", "\n\n", content)
1642
+
1643
+ return f"<!-- heading group -->\n{content}\n<!-- end heading group -->\n\n"
1644
+
1645
+
1646
+ def _convert_picture(*, text: str, convert_as_inline: bool, tag: Tag) -> str:
1647
+ """Convert HTML picture element with responsive image sources.
1648
+
1649
+ Args:
1650
+ text: The text content of the picture element.
1651
+ convert_as_inline: Whether to convert as inline content.
1652
+ tag: The picture tag element.
1653
+
1654
+ Returns:
1655
+ The converted markdown text with picture information preserved.
1656
+ """
1657
+ if not text.strip():
1658
+ return ""
1659
+
1660
+ # Find all source elements
1661
+ sources = tag.find_all("source")
1662
+ img = tag.find("img")
1663
+
1664
+ if not img:
1665
+ # No img fallback, just return the text content
1666
+ return text.strip()
1667
+
1668
+ # Get the primary image markdown (already converted)
1669
+ img_markdown = text.strip()
1670
+
1671
+ # If there are no sources, just return the image
1672
+ if not sources:
1673
+ return img_markdown
1674
+
1675
+ # Build a comment with source information for responsive images
1676
+ source_info = []
1677
+ for source in sources:
1678
+ srcset = source.get("srcset")
1679
+ media = source.get("media")
1680
+ mime_type = source.get("type")
1681
+
1682
+ if srcset:
1683
+ info = f'srcset="{srcset}"'
1684
+ if media:
1685
+ info += f' media="{media}"'
1686
+ if mime_type:
1687
+ info += f' type="{mime_type}"'
1688
+ source_info.append(info)
1689
+
1690
+ if source_info and not convert_as_inline:
1691
+ # Add picture source information as a comment
1692
+ sources_comment = "<!-- picture sources:\n"
1693
+ for info in source_info:
1694
+ sources_comment += f" {info}\n"
1695
+ sources_comment += "-->\n"
1696
+ return f"{sources_comment}{img_markdown}"
1697
+
1698
+ # In inline mode or no sources, just return the image
1699
+ return img_markdown
1700
+
1701
+
1702
+ def _convert_svg(*, text: str, convert_as_inline: bool, tag: Tag) -> str:
1703
+ """Convert SVG element to Markdown image reference.
1704
+
1705
+ Args:
1706
+ text: The text content of the SVG element.
1707
+ convert_as_inline: Whether to convert as inline content.
1708
+ tag: The SVG tag element.
1709
+
1710
+ Returns:
1711
+ The converted markdown text as an image reference.
1712
+ """
1713
+ if convert_as_inline:
1714
+ # In inline mode, just return any text content
1715
+ return text.strip()
1716
+
1717
+ # Get SVG attributes
1718
+ title = tag.find("title")
1719
+ title_text = title.get_text().strip() if title else ""
1720
+
1721
+ # For inline SVG, we'll convert to a data URI
1722
+ # First, we need to get the full SVG markup
1723
+ svg_markup = str(tag)
1724
+
1725
+ # Create a data URI
1726
+ svg_bytes = svg_markup.encode("utf-8")
1727
+ svg_base64 = base64.b64encode(svg_bytes).decode("utf-8")
1728
+ data_uri = f"data:image/svg+xml;base64,{svg_base64}"
1729
+
1730
+ # Use title as alt text, or "SVG Image" if no title
1731
+ alt_text = title_text or "SVG Image"
1732
+
1733
+ return f"![{alt_text}]({data_uri})"
1734
+
1735
+
1736
+ def _convert_math(*, text: str, convert_as_inline: bool, tag: Tag) -> str:
1737
+ """Convert MathML math element preserving mathematical notation.
1738
+
1739
+ Args:
1740
+ text: The text content of the math element.
1741
+ convert_as_inline: Whether to convert as inline content.
1742
+ tag: The math tag element.
1743
+
1744
+ Returns:
1745
+ The converted markdown text preserving math structure.
1746
+ """
1747
+ if not text.strip():
1748
+ return ""
1749
+
1750
+ # Check if it's display math vs inline math
1751
+ display = tag.get("display") == "block"
1752
+
1753
+ # For now, preserve the MathML as a comment with the text representation
1754
+ # This allows systems that understand MathML to process it
1755
+ math_comment = f"<!-- MathML: {tag!s} -->"
1756
+
1757
+ if convert_as_inline or not display:
1758
+ # Inline math - just the text with comment
1759
+ return f"{math_comment}{text.strip()}"
1760
+ # Display math - on its own line
1761
+ return f"\n\n{math_comment}\n{text.strip()}\n\n"
1762
+
1763
+
1764
+ def create_converters_map(
1765
+ autolinks: bool,
1766
+ bullets: str,
1767
+ code_language: str,
1768
+ code_language_callback: Callable[[Tag], str] | None,
1769
+ default_title: bool,
1770
+ heading_style: Literal["atx", "atx_closed", "underlined"],
1771
+ highlight_style: Literal["double-equal", "html", "bold"],
1772
+ keep_inline_images_in: Iterable[str] | None,
1773
+ newline_style: str,
1774
+ strong_em_symbol: str,
1775
+ sub_symbol: str,
1776
+ sup_symbol: str,
1777
+ wrap: bool,
1778
+ wrap_width: int,
1779
+ ) -> ConvertersMap:
1780
+ """Create a mapping of HTML elements to their corresponding conversion functions.
1781
+
1782
+ Args:
1783
+ autolinks: Whether to convert URLs into links.
1784
+ bullets: The bullet characters to use for unordered lists.
1785
+ code_language: The default code language to use.
1786
+ code_language_callback: A callback to get the code language.
1787
+ default_title: Whether to use the URL as the title for links.
1788
+ heading_style: The style of headings.
1789
+ highlight_style: The style to use for highlighted text (mark elements).
1790
+ keep_inline_images_in: The tags to keep inline images in.
1791
+ newline_style: The style of newlines.
1792
+ strong_em_symbol: The symbol to use for strong and emphasis text.
1793
+ sub_symbol: The symbol to use for subscript text.
1794
+ sup_symbol: The symbol to use for superscript text.
1795
+ wrap: Whether to wrap text.
1796
+ wrap_width: The width to wrap text at.
1797
+
1798
+ Returns:
1799
+ A mapping of HTML elements to their corresponding conversion functions
1800
+ """
1801
+
1802
+ def _wrapper(func: Callable[..., T]) -> Callable[[str, Tag], T]:
1803
+ spec = getfullargspec(func)
1804
+
1805
+ def _inner(*, text: str, tag: Tag, convert_as_inline: bool) -> T:
1806
+ if spec.kwonlyargs:
1807
+ kwargs: dict[str, Any] = {}
1808
+ if "tag" in spec.kwonlyargs:
1809
+ kwargs["tag"] = tag
1810
+ if "text" in spec.kwonlyargs:
1811
+ kwargs["text"] = text
1812
+ if "convert_as_inline" in spec.kwonlyargs:
1813
+ kwargs["convert_as_inline"] = convert_as_inline
1814
+ return func(**kwargs)
1815
+ return func(text)
1816
+
1817
+ return cast("Callable[[str, Tag], T]", _inner)
1818
+
1819
+ return {
1820
+ "a": _wrapper(partial(_convert_a, autolinks=autolinks, default_title=default_title)),
1821
+ "abbr": _wrapper(_convert_abbr),
1822
+ "article": _wrapper(_convert_semantic_block),
1823
+ "aside": _wrapper(_convert_semantic_block),
1824
+ "audio": _wrapper(_convert_audio),
1825
+ "b": _wrapper(partial(_create_inline_converter(2 * strong_em_symbol))),
1826
+ "bdi": _wrapper(_create_inline_converter("")), # Bidirectional isolation - pass through
1827
+ "bdo": _wrapper(_create_inline_converter("")), # Bidirectional override - pass through
1828
+ "blockquote": _wrapper(partial(_convert_blockquote)),
1829
+ "br": _wrapper(partial(_convert_br, newline_style=newline_style)),
1830
+ "button": _wrapper(_convert_button),
1831
+ "caption": _wrapper(_convert_caption),
1832
+ "cite": _wrapper(_convert_cite),
1833
+ "code": _wrapper(_create_inline_converter("`")),
1834
+ "col": _wrapper(_convert_col),
1835
+ "colgroup": _wrapper(_convert_colgroup),
1836
+ "data": _wrapper(_convert_data),
1837
+ "datalist": _wrapper(_convert_datalist),
1838
+ "dd": _wrapper(_convert_dd),
1839
+ "del": _wrapper(_create_inline_converter("~~")),
1840
+ "details": _wrapper(_convert_details),
1841
+ "dfn": _wrapper(_create_inline_converter("*")), # Definition term - italic
1842
+ "dialog": _wrapper(_convert_dialog),
1843
+ "dl": _wrapper(_convert_dl),
1844
+ "dt": _wrapper(_convert_dt),
1845
+ "em": _wrapper(_create_inline_converter(strong_em_symbol)),
1846
+ "fieldset": _wrapper(_convert_fieldset),
1847
+ "figcaption": _wrapper(lambda text: f"\n\n{text}\n\n"),
1848
+ "figure": _wrapper(_convert_figure),
1849
+ "footer": _wrapper(_convert_semantic_block),
1850
+ "form": _wrapper(_convert_form),
1851
+ "h1": _wrapper(partial(_convert_hn, n=1, heading_style=heading_style)),
1852
+ "h2": _wrapper(partial(_convert_hn, n=2, heading_style=heading_style)),
1853
+ "h3": _wrapper(partial(_convert_hn, n=3, heading_style=heading_style)),
1854
+ "h4": _wrapper(partial(_convert_hn, n=4, heading_style=heading_style)),
1855
+ "h5": _wrapper(partial(_convert_hn, n=5, heading_style=heading_style)),
1856
+ "h6": _wrapper(partial(_convert_hn, n=6, heading_style=heading_style)),
1857
+ "header": _wrapper(_convert_semantic_block),
1858
+ "hgroup": _wrapper(_convert_hgroup),
1859
+ "hr": _wrapper(lambda _: "\n\n---\n\n"),
1860
+ "i": _wrapper(partial(_create_inline_converter(strong_em_symbol))),
1861
+ "iframe": _wrapper(_convert_iframe),
1862
+ "img": _wrapper(partial(_convert_img, keep_inline_images_in=keep_inline_images_in)),
1863
+ "input": _wrapper(_convert_input_enhanced),
1864
+ "ins": _wrapper(_create_inline_converter("==")), # Inserted text - highlight style
1865
+ "kbd": _wrapper(_create_inline_converter("`")),
1866
+ "label": _wrapper(_convert_label),
1867
+ "legend": _wrapper(_convert_legend),
1868
+ "li": _wrapper(partial(_convert_li, bullets=bullets)),
1869
+ "list": _wrapper(_convert_list),
1870
+ "main": _wrapper(_convert_semantic_block),
1871
+ "mark": _wrapper(partial(_convert_mark, highlight_style=highlight_style)),
1872
+ "math": _wrapper(_convert_math),
1873
+ "menu": _wrapper(_convert_menu),
1874
+ "meter": _wrapper(_convert_meter),
1875
+ "nav": _wrapper(_convert_semantic_block),
1876
+ "ol": _wrapper(_convert_list),
1877
+ "optgroup": _wrapper(_convert_optgroup),
1878
+ "option": _wrapper(_convert_option),
1879
+ "output": _wrapper(_convert_output),
1880
+ "p": _wrapper(partial(_convert_p, wrap=wrap, wrap_width=wrap_width)),
1881
+ "picture": _wrapper(_convert_picture),
1882
+ "pre": _wrapper(
1883
+ partial(
1884
+ _convert_pre,
1885
+ code_language=code_language,
1886
+ code_language_callback=code_language_callback,
1887
+ )
1888
+ ),
1889
+ "progress": _wrapper(_convert_progress),
1890
+ "q": _wrapper(_convert_q),
1891
+ "rb": _wrapper(_convert_rb),
1892
+ "rp": _wrapper(_convert_rp),
1893
+ "rt": _wrapper(_convert_rt),
1894
+ "rtc": _wrapper(_convert_rtc),
1895
+ "ruby": _wrapper(_convert_ruby),
1896
+ "s": _wrapper(_create_inline_converter("~~")),
1897
+ "samp": _wrapper(_create_inline_converter("`")),
1898
+ "script": _wrapper(lambda _: ""),
1899
+ "section": _wrapper(_convert_semantic_block),
1900
+ "select": _wrapper(_convert_select),
1901
+ "small": _wrapper(_create_inline_converter("")), # Small text - pass through
1902
+ "strong": _wrapper(_create_inline_converter(strong_em_symbol * 2)),
1903
+ "style": _wrapper(lambda _: ""),
1904
+ "sub": _wrapper(_create_inline_converter(sub_symbol)),
1905
+ "summary": _wrapper(_convert_summary),
1906
+ "sup": _wrapper(_create_inline_converter(sup_symbol)),
1907
+ "svg": _wrapper(_convert_svg),
1908
+ "table": _wrapper(lambda text: f"\n\n{text}\n"),
1909
+ "tbody": _wrapper(_convert_tbody),
1910
+ "td": _wrapper(_convert_td),
1911
+ "textarea": _wrapper(_convert_textarea),
1912
+ "tfoot": _wrapper(_convert_tfoot),
1913
+ "th": _wrapper(_convert_th),
1914
+ "thead": _wrapper(_convert_thead),
1915
+ "time": _wrapper(_convert_time),
1916
+ "tr": _wrapper(_convert_tr),
1917
+ "u": _wrapper(_create_inline_converter("")), # Underlined text - pass through (no Markdown equivalent)
1918
+ "ul": _wrapper(_convert_list),
1919
+ "var": _wrapper(_create_inline_converter("*")), # Variable - italic
1920
+ "video": _wrapper(_convert_video),
1921
+ "wbr": _wrapper(_convert_wbr),
381
1922
  }