|
| 1 | +"""Trailing spaces that encode a hard break must survive normalization. |
| 2 | +
|
| 3 | +``MarkItDown._convert`` strips trailing whitespace from every converter's |
| 4 | +Markdown. Two or more trailing spaces are a CommonMark hard line break, so |
| 5 | +those are kept. A single trailing space, a trailing tab, and a line that is |
| 6 | +only whitespace are still removed. Runs of blank lines are still collapsed. |
| 7 | +""" |
| 8 | + |
| 9 | +import io |
| 10 | +from typing import Any, BinaryIO |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from markitdown import ( |
| 15 | + DocumentConverter, |
| 16 | + DocumentConverterResult, |
| 17 | + MarkItDown, |
| 18 | + StreamInfo, |
| 19 | +) |
| 20 | +from markitdown._markitdown import _rstrip_preserving_hard_break |
| 21 | + |
| 22 | + |
| 23 | +class _PassthroughConverter(DocumentConverter): |
| 24 | + def accepts( |
| 25 | + self, |
| 26 | + file_stream: BinaryIO, |
| 27 | + stream_info: StreamInfo, |
| 28 | + **kwargs: Any, |
| 29 | + ) -> bool: |
| 30 | + return (stream_info.extension or "").lower() == ".fixture" |
| 31 | + |
| 32 | + def convert( |
| 33 | + self, |
| 34 | + file_stream: BinaryIO, |
| 35 | + stream_info: StreamInfo, |
| 36 | + **kwargs: Any, |
| 37 | + ) -> DocumentConverterResult: |
| 38 | + return DocumentConverterResult(markdown=file_stream.read().decode("utf-8")) |
| 39 | + |
| 40 | + |
| 41 | +def _convert(source: str) -> str: |
| 42 | + markitdown = MarkItDown(enable_builtins=False, enable_plugins=False) |
| 43 | + markitdown.register_converter(_PassthroughConverter(), priority=-1.0) |
| 44 | + return markitdown.convert_stream( |
| 45 | + io.BytesIO(source.encode("utf-8")), |
| 46 | + stream_info=StreamInfo(extension=".fixture", charset="utf-8"), |
| 47 | + ).markdown |
| 48 | + |
| 49 | + |
| 50 | +def _convert_builtin(source: str, *, extension: str, mimetype: str) -> str: |
| 51 | + return ( |
| 52 | + MarkItDown(enable_plugins=False) |
| 53 | + .convert_stream( |
| 54 | + io.BytesIO(source.encode("utf-8")), |
| 55 | + stream_info=StreamInfo( |
| 56 | + extension=extension, mimetype=mimetype, charset="utf-8" |
| 57 | + ), |
| 58 | + ) |
| 59 | + .markdown |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +@pytest.mark.parametrize( |
| 64 | + ("line", "expected"), |
| 65 | + [ |
| 66 | + ("First line ", "First line "), |
| 67 | + ("First line ", "First line "), |
| 68 | + ("First line ", "First line"), |
| 69 | + ("First line\t", "First line"), |
| 70 | + ("First line \t", "First line"), |
| 71 | + ("First line \t", "First line"), |
| 72 | + ("First line\t ", "First line\t "), |
| 73 | + (" ", ""), |
| 74 | + ("", ""), |
| 75 | + ("First line\u00a0\u00a0", "First line"), |
| 76 | + ], |
| 77 | +) |
| 78 | +def test_rstrip_keeps_only_a_hard_break(line: str, expected: str) -> None: |
| 79 | + assert _rstrip_preserving_hard_break(line) == expected |
| 80 | + |
| 81 | + |
| 82 | +def test_hard_line_breaks_survive_normalization() -> None: |
| 83 | + source = "First line \nSecond line\n" |
| 84 | + |
| 85 | + assert _convert(source) == source |
| 86 | + |
| 87 | + |
| 88 | +def test_single_trailing_space_is_still_trimmed() -> None: |
| 89 | + source = "First line \nSecond line\n" |
| 90 | + |
| 91 | + assert _convert(source) == "First line\nSecond line\n" |
| 92 | + |
| 93 | + |
| 94 | +def test_hard_line_breaks_with_more_than_two_spaces_are_preserved() -> None: |
| 95 | + source = "First line \nSecond line\n" |
| 96 | + |
| 97 | + assert _convert(source) == source |
| 98 | + |
| 99 | + |
| 100 | +def test_several_hard_breaks_in_one_document_are_preserved() -> None: |
| 101 | + source = "One \nTwo \nThree\n" |
| 102 | + |
| 103 | + assert _convert(source) == source |
| 104 | + |
| 105 | + |
| 106 | +def test_crlf_hard_break_becomes_lf_and_keeps_the_spaces() -> None: |
| 107 | + source = "First line \r\nSecond line\r\n" |
| 108 | + |
| 109 | + assert _convert(source) == "First line \nSecond line\n" |
| 110 | + |
| 111 | + |
| 112 | +def test_backslash_hard_break_is_unchanged() -> None: |
| 113 | + source = "First line\\\nSecond line\n" |
| 114 | + |
| 115 | + assert _convert(source) == source |
| 116 | + |
| 117 | + |
| 118 | +def test_trailing_tab_is_still_trimmed() -> None: |
| 119 | + source = "First line\t\nSecond line\n" |
| 120 | + |
| 121 | + assert _convert(source) == "First line\nSecond line\n" |
| 122 | + |
| 123 | + |
| 124 | +def test_spaces_followed_by_a_tab_are_not_a_hard_break() -> None: |
| 125 | + source = "First line \t\nSecond line\n" |
| 126 | + |
| 127 | + assert _convert(source) == "First line\nSecond line\n" |
| 128 | + |
| 129 | + |
| 130 | +def test_whitespace_only_line_is_still_trimmed() -> None: |
| 131 | + source = "First line\n \nSecond line\n" |
| 132 | + |
| 133 | + assert _convert(source) == "First line\n\nSecond line\n" |
| 134 | + |
| 135 | + |
| 136 | +def test_blank_line_runs_are_still_collapsed() -> None: |
| 137 | + source = "First line\n\n\n\nSecond line\n" |
| 138 | + |
| 139 | + assert _convert(source) == "First line\n\nSecond line\n" |
| 140 | + |
| 141 | + |
| 142 | +def test_hard_break_survives_beside_a_collapsed_blank_run() -> None: |
| 143 | + source = "First line \n\n\n\nSecond line \nThird\n" |
| 144 | + |
| 145 | + assert _convert(source) == "First line \n\nSecond line \nThird\n" |
| 146 | + |
| 147 | + |
| 148 | +def test_two_trailing_spaces_inside_a_fence_are_kept() -> None: |
| 149 | + source = "```\ncode \n```\n" |
| 150 | + |
| 151 | + assert _convert(source) == source |
| 152 | + |
| 153 | + |
| 154 | +def test_one_trailing_space_inside_a_fence_is_still_trimmed() -> None: |
| 155 | + source = "```\ncode \n```\n" |
| 156 | + |
| 157 | + assert _convert(source) == "```\ncode\n```\n" |
| 158 | + |
| 159 | + |
| 160 | +def test_empty_output_stays_empty() -> None: |
| 161 | + assert _convert("") == "" |
| 162 | + |
| 163 | + |
| 164 | +def test_hard_break_without_a_final_newline_is_kept() -> None: |
| 165 | + source = "First line " |
| 166 | + |
| 167 | + assert _convert(source) == source |
| 168 | + |
| 169 | + |
| 170 | +def test_markdown_files_keep_hard_line_breaks() -> None: |
| 171 | + source = "First line \nSecond line\n" |
| 172 | + |
| 173 | + assert _convert_builtin(source, extension=".md", mimetype="text/markdown") == source |
| 174 | + |
| 175 | + |
| 176 | +def test_markdown_crlf_file_keeps_the_hard_break() -> None: |
| 177 | + source = "- item one \r\n continuation\r\n" |
| 178 | + |
| 179 | + assert ( |
| 180 | + _convert_builtin(source, extension=".markdown", mimetype="text/markdown") |
| 181 | + == "- item one \n continuation\n" |
| 182 | + ) |
| 183 | + |
| 184 | + |
| 185 | +def test_html_br_keeps_a_hard_line_break() -> None: |
| 186 | + html = "<p>First<br>Second</p><p>Third<br/>Fourth</p>" |
| 187 | + |
| 188 | + assert ( |
| 189 | + _convert_builtin(html, extension=".html", mimetype="text/html") |
| 190 | + == "First \nSecond\n\nThird \nFourth" |
| 191 | + ) |
| 192 | + |
| 193 | + |
| 194 | +def test_html_paragraphs_do_not_gain_trailing_spaces() -> None: |
| 195 | + html = "<p>First </p><p>Second</p>" |
| 196 | + |
| 197 | + assert ( |
| 198 | + _convert_builtin(html, extension=".html", mimetype="text/html") |
| 199 | + == "First\n\nSecond" |
| 200 | + ) |
0 commit comments