Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(parser): preserve indentation inside fenced code blocks during normalization (#1315)

Ahmad Bilal5b818da92d18
diff --git a/semantica/normalize/text_normalizer.py b/semantica/normalize/text_normalizer.py
index 2f55193e..f5658e04 100644
--- a/semantica/normalize/text_normalizer.py
+++ b/semantica/normalize/text_normalizer.py
@@ -187,7 +187,7 @@ class TextNormalizer:
normalized = normalized.title()
self.progress_tracker.stop_tracking(tracking_id, status="completed")
- return normalized.strip()
+ return normalized
except Exception as e:
self.progress_tracker.stop_tracking(
@@ -406,20 +406,121 @@ class WhitespaceNormalizer:
self.logger.debug("Whitespace normalizer initialized")
+ @staticmethod
+ def _split_code_blocks(text: str) -> List[tuple]:
+ """
+ Split text into alternating non-code and code segments.
+
+ Fenced code blocks are delimited by lines starting with three or more
+ backticks (```) or tildes (~~~), optionally followed by a language tag.
+ The opening fence must appear at the start of a line (allowing leading
+ whitespace). A line that still contains the delimiter character after
+ the marker (e.g. `` ```foo``` ``) is inline code, not a fence, so it is
+ left in the surrounding prose instead of starting an unclosed block.
+ The block ends at the next closing fence line with the same fence
+ character, at least as many repetitions, only trailing whitespace, and
+ an indentation within 3 spaces of the opener (so an indented ``` inside
+ a Python docstring does not close the block early).
+
+ If a fence is opened but never closed, everything from the opening
+ fence to the end of the text is treated as code.
+
+ Returns:
+ List of (is_code, segment_text) tuples where is_code is True for
+ fenced code block content (including the fence lines themselves).
+ """
+ fence_re = re.compile(r"^[ \t]*([`]{3,}|[~]{3,})(.*)$", re.MULTILINE)
+
+ fence_starts: List[tuple] = []
+ for m in fence_re.finditer(text):
+ indent = len(m.group(0)) - len(m.group(1)) - len(m.group(2))
+ fence_starts.append((m.start(), m.group(1), m.group(2), m.end(), indent))
+
+ segments: List[tuple] = []
+ last_end = 0
+ in_fence = False
+ fence_char: Optional[str] = None
+ fence_len = 0
+ opener_indent = 0
+
+ for start, chars, rest, match_end, indent in fence_starts:
+ cur_char = chars[0]
+ cur_len = len(chars)
+
+ if not in_fence:
+ # Inline code on the same line as a fence marker (e.g. ```foo```)
+ # is not an opening fence; leave it in prose so it is never
+ # parsed as an unclosed block.
+ if cur_char in rest:
+ continue
+ # Opening fence
+ if start > last_end:
+ segments.append((False, text[last_end:start]))
+ in_fence = True
+ fence_char = cur_char
+ fence_len = cur_len
+ opener_indent = indent
+ # Include the opening fence line in the code segment
+ line_end = text.find("\n", match_end)
+ if line_end == -1:
+ line_end = len(text)
+ else:
+ line_end += 1 # include the newline
+ segments.append((True, text[start:line_end]))
+ last_end = line_end
+ else:
+ # Are we looking for a closing fence?
+ if (
+ cur_char == fence_char
+ and cur_len >= fence_len
+ and rest.strip() == ""
+ and abs(indent - opener_indent) <= 3
+ ):
+ # Closing fence found – emit code up to and including it
+ line_end = text.find("\n", match_end)
+ if line_end == -1:
+ line_end = len(text)
+ else:
+ line_end += 1
+ if start > last_end:
+ segments.append((True, text[last_end:start]))
+ segments.append((True, text[start:line_end]))
+ last_end = line_end
+ in_fence = False
+ fence_char = None
+ fence_len = 0
+
+ # Handle unclosed fence – everything from last_end is code
+ if in_fence:
+ if last_end < len(text):
+ segments.append((True, text[last_end:]))
+ last_end = len(text)
+ elif last_end < len(text):
+ segments.append((False, text[last_end:]))
+
+ return segments
+
def normalize_whitespace(
self, text: str, line_break_type: str = "unix", **options
) -> str:
"""
Normalize whitespace in text.
- This method normalizes whitespace by replacing tabs with spaces,
- normalizing line breaks, and collapsing multiple spaces.
+ This method normalizes whitespace by collapsing multiple spaces and
+ excess blank lines in prose. Content inside fenced code blocks
+ (delimited by ``` or ~~~) is preserved exactly as-is, including tabs,
+ so that indentation is not lost. Line endings are normalized to LF
+ internally for processing and converted to the requested line break
+ type at the end, so ``\\r\\n`` input is never mangled into
+ ``\\r\\r\\n\\n``. Trailing whitespace on prose lines (including
+ whitespace-only blank lines) is stripped before blank lines are
+ collapsed, so a line like ``\\n \\n`` collapses like an empty one.
Args:
text: Input text with potentially irregular whitespace
line_break_type: Line break type (default: "unix"):
- - "unix": Unix-style line breaks (\n)
- - "windows": Windows-style line breaks (\r\n)
+ - "unix": Unix-style line breaks (\\n)
+ - "windows": Windows-style line breaks (\\r\\n)
**options: Additional normalization options (unused)
Returns:
@@ -428,17 +529,47 @@ class WhitespaceNormalizer:
if not text:
return ""
- # Replace tabs with spaces
- text = text.replace("\t", " ")
-
- # Normalize line breaks
- text = self.handle_line_breaks(text, line_break_type)
-
- # Remove excessive whitespace
- text = re.sub(r" +", " ", text)
- text = re.sub(r"\n\s*\n", "\n\n", text) # Normalize multiple newlines
-
- return text.strip()
+ # Normalise to LF internally for all processing.
+ text = text.replace("\r\n", "\n").replace("\r", "\n")
+
+ # Split into code / non-code segments BEFORE touching tabs.
+ segments = self._split_code_blocks(text)
+
+ result_parts: List[str] = []
+ for idx, (is_code, segment) in enumerate(segments):
+ if is_code:
+ # Preserve code block content exactly (tabs included).
+ result_parts.append(segment)
+ else:
+ # Replace tabs with a single space (prose only).
+ segment = segment.replace("\t", " ")
+ # Collapse multiple spaces.
+ segment = re.sub(r" +", " ", segment)
+ # Strip trailing whitespace on prose lines so whitespace-only
+ # blank lines (e.g. "\n \n") behave like blank lines when
+ # collapsed with \n{3,}.
+ segment = re.sub(r"[ \t]+\n", "\n", segment)
+ if idx == 0:
+ # Strip the leading edge from the first prose chunk here;
+ # stripping the joined result would eat opening-fence
+ # indentation that follows a leading newline.
+ segment = segment.lstrip()
+ elif idx > 0:
+ # A prose chunk that follows a code block starts at a line
+ # boundary; the code block already contributes the first
+ # newline, so collapse any leading blank lines here to one.
+ segment = re.sub(r"^\n+", "\n", segment)
+ if idx == len(segments) - 1:
+ # Strip the trailing edge from the last prose chunk only.
+ segment = segment.rstrip()
+ # Collapse excess blank lines within this prose chunk only.
+ segment = re.sub(r"\n{3,}", "\n\n", segment)
+ result_parts.append(segment)
+
+ result = "".join(result_parts)
+
+ # Restore requested line endings.
+ return self.handle_line_breaks(result, line_break_type)
def handle_line_breaks(self, text: str, line_break_type: str = "unix") -> str:
"""
diff --git a/tests/normalize/test_text_normalizer.py b/tests/normalize/test_text_normalizer.py
index 84076975..436e86e4 100644
--- a/tests/normalize/test_text_normalizer.py
+++ b/tests/normalize/test_text_normalizer.py
@@ -313,7 +313,7 @@ class TestSpecialCharacterProcessor(unittest.TestCase):
def test_process_special_chars_integration(self):
"""Test the main processing method integration"""
- text = "“Crème”"
+ text = "\u201cCr\u00e8me\u201d"
result = self.processor.process_special_chars(
text, normalize_diacritics=True, remove_diacritics=True
@@ -321,5 +321,802 @@ class TestSpecialCharacterProcessor(unittest.TestCase):
self.assertEqual(result, '"Creme"')
+class TestFencedCodeBlockPreservation(unittest.TestCase):
+ """
+ Test suite ensuring whitespace normalization preserves indentation
+ inside fenced code blocks (``` and ~~~ delimiters).
+ """
+
+ def setUp(self):
+ self.normalizer = WhitespaceNormalizer()
+
+ def test_python_code_4_space_indentation_backtick_fence(self):
+ """4-space indented Python code inside ```python fences is preserved."""
+ text = (
+ "Some text before\n"
+ "\n"
+ "```python\n"
+ "def hello():\n"
+ " print('hi')\n"
+ " if True:\n"
+ " return 1\n"
+ "```\n"
+ "\n"
+ "Some text after"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" print('hi')", result)
+ self.assertIn(" return 1", result)
+ self.assertIn("def hello():", result)
+
+ def test_python_code_4_space_indentation_tilde_fence(self):
+ """4-space indented Python code inside ~~~python fences is preserved."""
+ text = (
+ "Before\n"
+ "\n"
+ "~~~python\n"
+ "class Foo:\n"
+ " def method(self):\n"
+ " pass\n"
+ "~~~\n"
+ "\n"
+ "After"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" def method(self):", result)
+ self.assertIn(" pass", result)
+
+ def test_nested_lists_outside_code_block(self):
+ """Nested list indentation outside code blocks is collapsed normally."""
+ text = (
+ "A list:\n"
+ "\n"
+ " - item one\n"
+ " - item two\n"
+ " - nested item\n"
+ " - item three"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ # The leading spaces on list items get collapsed
+ self.assertNotIn(" - item", result)
+ self.assertIn("- item", result)
+
+ def test_mixed_code_and_normal_text(self):
+ """Code block is preserved while surrounding text is normalized."""
+ text = (
+ " Leading spaces in prose\n"
+ "\n"
+ "```js\n"
+ "const x = 1;\n"
+ "```\n"
+ "\n"
+ " Trailing spaces in prose"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ # Prose multiple spaces collapsed
+ self.assertIn("Leading spaces in prose", result)
+ self.assertIn("Trailing spaces in prose", result)
+ # Code block whitespace preserved
+ self.assertIn("const x = 1;", result)
+
+ def test_multiple_code_blocks(self):
+ """Multiple code blocks are each preserved."""
+ text = (
+ "First\n"
+ "```\n"
+ " a b\n"
+ "```\n"
+ "Middle\n"
+ "```\n"
+ " c d\n"
+ "```\n"
+ "Last"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" a b", result)
+ self.assertIn(" c d", result)
+ self.assertIn("First", result)
+ self.assertIn("Middle", result)
+ self.assertIn("Last", result)
+
+ def test_unclosed_fence_preserves_content(self):
+ """An unclosed fence still preserves content as code."""
+ text = (
+ "Before\n"
+ "```\n"
+ " indented code\n"
+ " still code"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" indented code", result)
+ self.assertIn(" still code", result)
+
+ def test_fence_with_language_tag(self):
+ """Fence lines with language tags are handled correctly."""
+ text = (
+ "Text\n"
+ "```python\n"
+ " x = 1\n"
+ "```\n"
+ "More text"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" x = 1", result)
+
+ def test_tab_to_space_conversion_only_in_prose(self):
+ """Tabs are replaced with spaces in prose but preserved in code blocks."""
+ text = (
+ "Before\tcode\n"
+ "```\n"
+ "\tdef foo():\n"
+ "\t\tpass\n"
+ "```\n"
+ "\tAfter code"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ # Prose tabs converted to single spaces
+ self.assertIn("Before code", result)
+ self.assertIn("After code", result)
+ # Code-block tabs preserved exactly
+ self.assertIn("\tdef foo():", result)
+ self.assertIn("\t\tpass", result)
+ self.assertNotIn(" def foo():", result)
+
+ def test_empty_code_block(self):
+ """Empty code blocks are handled."""
+ text = "Before\n```\n```\nAfter"
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn("Before", result)
+ self.assertIn("After", result)
+
+ def test_code_block_only(self):
+ """Text that is entirely a code block."""
+ text = "```\n only code\n```"
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" only code", result)
+
+ def test_empty_input(self):
+ """Empty string returns empty string."""
+ self.assertEqual(self.normalizer.normalize_whitespace(""), "")
+
+ def test_indented_fence_at_start_preserves_leading_whitespace(self):
+ """Leading whitespace on an opening fence at the start of input is preserved."""
+ text = " ```python\n code\n ```"
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertTrue(result.startswith(" ```python"),
+ f"Leading indentation stripped; got: {result!r}")
+
+ def test_indented_fence_at_end_preserves_trailing_whitespace(self):
+ """Trailing whitespace on a closing fence at the end of input is preserved."""
+ text = "```python\n code\n ``` "
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertTrue(result.endswith("``` "),
+ f"Trailing indentation stripped; got: {result!r}")
+
+ def test_leading_newline_before_indented_fence_preserves_indent(self):
+ """A leading newline before an indented fence must not eat fence indent."""
+ text = "\n ```python\n code\n ```"
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertTrue(result.startswith(" ```python"),
+ f"Opening-fence indentation stripped; got: {result!r}")
+
+ def test_inline_backtick_code_not_parsed_as_fence(self):
+ """Inline code like ```foo``` must not be parsed as an unclosed block."""
+ text = "Before\n```foo```\n\n\n After more\n"
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn("```foo```", result)
+ self.assertIn("After more", result)
+
+ def test_inline_tilde_code_not_parsed_as_fence(self):
+ """Inline code like ~~~foo~~~ must not be parsed as an unclosed block."""
+ text = "Before\n~~~foo~~~\n\n\n After more\n"
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn("~~~foo~~~", result)
+ self.assertIn("After more", result)
+
+ def test_indented_fence_in_docstring_does_not_close_block(self):
+ """An indented ``` inside a docstring must not close the code block."""
+ text = (
+ "```python\n"
+ "def foo():\n"
+ ' """\n'
+ " Example:\n"
+ " ```\n"
+ " contents\n"
+ " ```\n"
+ ' """\n'
+ "```\n"
+ "end"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ # The docstring content stays inside the code block.
+ self.assertIn(
+ "Example:\n ```\n contents\n ```\n \"\"\"",
+ result,
+ )
+ # The block is still closed by the unindented closing fence and
+ # the trailing prose is normalized.
+ self.assertIn("```\nend", result)
+
+ def test_docstring_fence_within_small_indent_delta_closes(self):
+ """A closer indented within 3 spaces of the opener is accepted."""
+ text = " ```python\ncode\n ```\nend"
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn("code", result)
+ self.assertIn("```\nend", result)
+
+ def test_real_world_python_example(self):
+ """Full realistic example with prose, code, and nested lists."""
+ text = (
+ "Here is an example:\n"
+ "\n"
+ "```python\n"
+ "def factorial(n):\n"
+ " if n <= 1:\n"
+ " return 1\n"
+ " return n * factorial(n - 1)\n"
+ "```\n"
+ "\n"
+ "And some notes:\n"
+ "\n"
+ " - First point\n"
+ " - Second point\n"
+ " - Sub-point"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ # Code block fully preserved
+ self.assertIn(" if n <= 1:", result)
+ self.assertIn(" return 1", result)
+ self.assertIn(" return n * factorial(n - 1)", result)
+ # Prose normalized
+ self.assertIn("Here is an example", result)
+ self.assertIn("And some notes", result)
+
+ def test_split_code_blocks_helper(self):
+ """Test the _split_code_blocks helper directly."""
+ text = "abc\n```\ncode\n```\ndef"
+ segments = self.normalizer._split_code_blocks(text)
+ # Before (non-code) + code block (code) + after (non-code)
+ non_code = [s for s in segments if not s[0]]
+ code = [s for s in segments if s[0]]
+ self.assertEqual(len(non_code), 2)
+ self.assertIn("abc", non_code[0][1])
+ self.assertIn("def", non_code[1][1])
+ self.assertTrue(len(code) >= 1)
+ # All code segments combined contain the full code block
+ code_text = "".join(seg for _, seg in code)
+ self.assertIn("```", code_text)
+ self.assertIn("code", code_text)
+
+ def test_very_long_fenced_block(self):
+ """Deeply indented code (8+ spaces) inside a code block is preserved."""
+ text = (
+ "text\n"
+ "```\n"
+ " eight spaces\n"
+ " sixteen spaces\n"
+ "```\n"
+ "more text"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" eight spaces", result)
+ self.assertIn(" sixteen spaces", result)
+
+ def test_pure_backtick_fence(self):
+ """A pure run of 3+ backticks opens and closes a code block."""
+ text = (
+ "Before\n"
+ "```\n"
+ " code here\n"
+ "```\n"
+ "After"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" code here", result)
+ self.assertIn("Before", result)
+ self.assertIn("After", result)
+
+ def test_pure_tilde_fence(self):
+ """A pure run of 3+ tildes opens and closes a code block."""
+ text = (
+ "Before\n"
+ "~~~\n"
+ " code here\n"
+ "~~~\n"
+ "After"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" code here", result)
+ self.assertIn("Before", result)
+ self.assertIn("After", result)
+
+ def test_mixed_fence_not_recognized(self):
+ """A line starting with mixed backtick/tilde characters is NOT a fence."""
+ text = (
+ "Before\n"
+ "`~~not-a-fence\n"
+ " indented text\n"
+ "~``also-not\n"
+ "After"
+ )
+ segments = self.normalizer._split_code_blocks(text)
+ # No code segments should exist – everything is prose
+ code_segments = [s for s in segments if s[0]]
+ self.assertEqual(len(code_segments), 0)
+ # The indented text should be in prose and therefore have leading spaces collapsed
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertNotIn(" indented text", result)
+
+ def test_mixed_fence_backtick_tilde_backtick(self):
+ """`~` is not a valid fence delimiter."""
+ segments = self.normalizer._split_code_blocks("`~`code\n`~`")
+ code_segments = [s for s in segments if s[0]]
+ self.assertEqual(len(code_segments), 0)
+
+ def test_mixed_fence_tilde_backtick_tilde(self):
+ """~`~ is not a valid fence delimiter."""
+ segments = self.normalizer._split_code_blocks("~`~code\n~`~")
+ code_segments = [s for s in segments if s[0]]
+ self.assertEqual(len(code_segments), 0)
+
+ def test_longer_mixed_fence_not_recognized(self):
+ """``~~ or ~~`` are not valid fence delimiters."""
+ text = (
+ "``~~\n"
+ "code\n"
+ "``~~"
+ )
+ segments = self.normalizer._split_code_blocks(text)
+ code_segments = [s for s in segments if s[0]]
+ self.assertEqual(len(code_segments), 0)
+
+ def test_false_closer_backtick_with_text(self):
+ """``` not-a-closer should NOT close a code block."""
+ text = (
+ "Before\n"
+ "```\n"
+ " code\n"
+ "``` not-a-closer\n"
+ " still code\n"
+ "```\n"
+ "After"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ # Both code lines should be preserved
+ self.assertIn(" code", result)
+ self.assertIn(" still code", result)
+ self.assertIn("After", result)
+
+ def test_false_closer_tilde_with_text(self):
+ """~~~ not-a-closer should NOT close a code block."""
+ text = (
+ "Before\n"
+ "~~~\n"
+ " code\n"
+ "~~~ not-a-closer\n"
+ " still code\n"
+ "~~~\n"
+ "After"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" code", result)
+ self.assertIn(" still code", result)
+
+ def test_false_closer_with_language_like_text(self):
+ """```python-note should NOT act as a closer for ```python."""
+ text = (
+ "```python\n"
+ " x = 1\n"
+ "```python-note\n"
+ " y = 2\n"
+ "```\n"
+ "end"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" x = 1", result)
+ self.assertIn(" y = 2", result)
+
+ def test_closer_with_trailing_spaces_is_valid(self):
+ """A closing fence followed only by spaces is valid."""
+ text = (
+ "Before\n"
+ "```\n"
+ " code\n"
+ "``` \n"
+ "After"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" code", result)
+ self.assertIn("After", result)
+
+ def test_closer_with_trailing_tabs_is_valid(self):
+ """A closing fence followed only by tabs is valid."""
+ text = (
+ "Before\n"
+ "```\n"
+ " code\n"
+ "```\t\t\n"
+ "After"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" code", result)
+ self.assertIn("After", result)
+
+ def test_false_closer_longer_than_opener(self):
+ """A false closer with more backticks + text still should not close."""
+ text = (
+ "```\n"
+ " code\n"
+ "````language\n"
+ "```\n"
+ "end"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" code", result)
+ self.assertIn("end", result)
+
+ def test_blank_lines_collapse_after_code_block(self):
+ """Excess blank lines between a closing fence and prose are collapsed."""
+ text = (
+ "Before\n"
+ "```\n"
+ " code\n"
+ "```\n"
+ "\n"
+ "\n"
+ "\n"
+ "After"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ # Should have exactly one blank line (two newlines) between code and prose
+ self.assertIn("```\n\nAfter", result)
+
+ def test_blank_lines_collapse_before_code_block(self):
+ """Excess blank lines between prose and an opening fence are collapsed."""
+ text = (
+ "Before\n"
+ "\n"
+ "\n"
+ "\n"
+ "```\n"
+ " code\n"
+ "```\n"
+ "After"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ # Should have exactly one blank line before the fence
+ self.assertIn("Before\n\n```", result)
+
+ def test_blank_lines_collapse_between_two_code_blocks(self):
+ """Excess blank lines between two code blocks are collapsed."""
+ text = (
+ "```\n"
+ " a\n"
+ "```\n"
+ "\n"
+ "\n"
+ "\n"
+ "```\n"
+ " b\n"
+ "```\n"
+ "end"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" a", result)
+ self.assertIn(" b", result)
+ # Only one blank line between code blocks
+ self.assertNotIn("\n\n\n", result)
+
+ def test_blank_lines_with_whitespace_only_between(self):
+ """Blank lines containing only spaces/tabs are collapsed."""
+ text = (
+ "```\n"
+ " code\n"
+ "```\n"
+ " \n"
+ " \t \n"
+ "After"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" code", result)
+ self.assertIn("After", result)
+ self.assertNotIn("\n\n\n", result)
+ # Whitespace-only blank lines collapse to a single blank line after
+ # the closing fence.
+ self.assertEqual("\n```\n\nAfter", result[result.index("code") + len("code"):])
+
+ def test_whitespace_only_blank_lines_inside_prose_collapse(self):
+ """Whitespace-only blank lines in prose collapse like empty lines."""
+ text = "line1\n \n \t \nline2"
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertEqual("line1\n\nline2", result)
+
+ def test_four_backtick_fence(self):
+ """Four-backtick fences work correctly."""
+ text = (
+ "Before\n"
+ "````\n"
+ " code\n"
+ "````\n"
+ "After"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" code", result)
+ self.assertIn("After", result)
+
+ def test_five_tilde_fence(self):
+ """Five-tilde fences work correctly."""
+ text = (
+ "Before\n"
+ "~~~~~\n"
+ " code\n"
+ "~~~~~\n"
+ "After"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" code", result)
+ self.assertIn("After", result)
+
+ def test_closing_fence_exact_length_required(self):
+ """A closing fence with fewer delimiters than the opener is NOT a closer."""
+ text = (
+ "````\n"
+ " code\n"
+ "```\n"
+ " more code\n"
+ "````\n"
+ "After"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn(" code", result)
+ self.assertIn(" more code", result)
+ self.assertIn("After", result)
+
+ def test_false_closer_embedded_in_code_block(self):
+ """A false closer line is preserved as code content inside the block."""
+ text = (
+ "```\n"
+ "line1\n"
+ "``` this is not a closing fence\n"
+ "line2\n"
+ "```\n"
+ "end"
+ )
+ segments = self.normalizer._split_code_blocks(text)
+ code_segments = [s for s in segments if s[0]]
+ # The false closer line should appear in a code segment
+ code_text = "".join(seg for _, seg in code_segments)
+ self.assertIn("``` this is not a closing fence", code_text)
+ self.assertIn("line1", code_text)
+ self.assertIn("line2", code_text)
+
+ def test_code_block_two_blank_lines_between_functions_preserved(self):
+ """Two blank lines between functions in a code block are preserved (PEP 8)."""
+ text = (
+ "text\n"
+ "```python\n"
+ "def first():\n"
+ " pass\n"
+ "\n"
+ "\n"
+ "def second():\n"
+ " pass\n"
+ "```\n"
+ "more text"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ # Both blank lines remain inside the code block.
+ self.assertIn(" pass\n\n\ndef second():", result)
+ self.assertNotEqual(result.count("\n\n\n"), 0)
+
+ def test_code_block_tabs_preserved(self):
+ """Tab characters inside code blocks (e.g. Makefiles) are preserved."""
+ text = (
+ "intro\n"
+ "```makefile\n"
+ "\tbuild:\n"
+ "\t\t$(CC) $(CFLAGS) -o app main.c\n"
+ "```\n"
+ "outro"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn("\tbuild:", result)
+ self.assertIn("\t\t$(CC) $(CFLAGS) -o app main.c", result)
+ self.assertNotIn(" $(CC)", result)
+
+ def test_tab_indented_python_code_block_preserved(self):
+ """Tab-indented Python code inside a code block keeps its tabs."""
+ text = (
+ "Before\n"
+ "```python\n"
+ "if True:\n"
+ "\tresult = 1\n"
+ "\tif result:\n"
+ "\t\tpass\n"
+ "```\n"
+ "After"
+ )
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertIn("\tresult = 1", result)
+ self.assertIn("\tif result:", result)
+ self.assertIn("\t\tpass", result)
+
+ def test_document_starting_with_indented_code_block(self):
+ """A document starting with an indented code block keeps leading indentation."""
+ text = " ```python\n def foo():\n return 1\n ```"
+ result = self.normalizer.normalize_whitespace(text)
+ self.assertTrue(result.startswith(" ```python"), repr(result))
+ self.assertIn(" def foo():", result)
+ self.assertIn(" return 1", result)
+
+ def test_crlf_line_endings_not_mangled(self):
+ """CRLF input is not mangled into \\r\\r\\n\\n during processing."""
+ text = (
+ "Before\r\n"
+ "\r\n"
+ "```\r\n"
+ " code\r\n"
+ "```\r\n"
+ "After"
+ )
+ result = self.normalizer.normalize_whitespace(text, line_break_type="windows")
+ self.assertNotIn("\r\r\n\n", result)
+ self.assertNotIn("\r\r", result)
+ self.assertIn("Before\r\n\r\n```", result)
+ self.assertIn(" code", result)
+
+ def test_crlf_two_blank_lines_in_code_block_preserved(self):
+ """Two blank lines inside a CRLF code block are preserved after restoration."""
+ text = (
+ "intro\r\n"
+ "```python\r\n"
+ "def first():\r\n"
+ " pass\r\n"
+ "\r\n"
+ "\r\n"
+ "def second():\r\n"
+ " pass\r\n"
+ "```\r\n"
+ "outro"
+ )
+ result = self.normalizer.normalize_whitespace(text, line_break_type="windows")
+ self.assertIn("pass\r\n\r\n\r\ndef second", result)
+ self.assertNotIn("\r\r\n\n", result)
+
+
+class TestTextNormalizerFencedCodeIntegration(unittest.TestCase):
+ """
+ Integration tests for TextNormalizer.normalize_text with fenced code blocks.
+ """
+
+ def setUp(self):
+ self.logger_patcher = patch("semantica.normalize.text_normalizer.get_logger")
+ self.tracker_patcher = patch(
+ "semantica.normalize.text_normalizer.get_progress_tracker"
+ )
+ self.cleaner_patcher = patch("semantica.normalize.text_normalizer.TextCleaner")
+
+ self.mock_logger = self.logger_patcher.start()
+ self.mock_tracker = self.tracker_patcher.start()
+ self.mock_cleaner_cls = self.cleaner_patcher.start()
+
+ self.mock_tracker_instance = MagicMock()
+ self.mock_tracker_instance.enabled = True
+ self.mock_tracker.return_value = self.mock_tracker_instance
+
+ self.mock_cleaner_instance = MagicMock()
+ self.mock_cleaner_cls.return_value = self.mock_cleaner_instance
+
+ self.normalizer = TextNormalizer()
+
+ def tearDown(self):
+ self.logger_patcher.stop()
+ self.tracker_patcher.stop()
+ self.cleaner_patcher.stop()
+
+ def test_normalize_text_preserves_code_block_indentation(self):
+ """Full normalize_text pipeline preserves code block indentation."""
+ text = (
+ " Some text \n"
+ "```python\n"
+ "def greet(name):\n"
+ " print(f'Hello, {name}')\n"
+ "```\n"
+ " More text "
+ )
+ result = self.normalizer.normalize_text(text)
+ self.assertIn(" print(f'Hello, {name}')", result)
+ self.assertIn("Some text", result)
+ self.assertIn("More text", result)
+
+ def test_normalize_text_with_tilde_fence(self):
+ """normalize_text preserves indentation inside ~~~ fences."""
+ text = (
+ "Intro\n"
+ "~~~\n"
+ " indented\n"
+ "~~~\n"
+ "Outro"
+ )
+ result = self.normalizer.normalize_text(text)
+ self.assertIn(" indented", result)
+
+ def test_normalize_text_with_smart_quotes_and_code(self):
+ """Smart quotes are replaced outside code blocks, code is preserved."""
+ text = (
+ "\u201cquoted\u201d text\n"
+ "```\n"
+ " code here\n"
+ "```\n"
+ "\u201cmore\u201d"
+ )
+ result = self.normalizer.normalize_text(text)
+ self.assertIn('"quoted" text', result)
+ self.assertIn(" code here", result)
+ self.assertIn('"more"', result)
+
+ def test_normalize_text_document_starting_with_indented_code_block(self):
+ """A document starting with an indented code block retains leading indentation."""
+ text = (
+ " ```python\n"
+ " def foo():\n"
+ " return 1\n"
+ " ```"
+ )
+ result = self.normalizer.normalize_text(text)
+ self.assertTrue(result.startswith(" ```python"), repr(result))
+ self.assertIn(" def foo():", result)
+ self.assertIn(" return 1", result)
+
+ def test_normalize_text_preserves_two_blank_lines_in_code_block(self):
+ """normalize_text does not collapse blank lines inside fenced code blocks."""
+ text = (
+ "Intro\n"
+ "```python\n"
+ "def first():\n"
+ " pass\n"
+ "\n"
+ "\n"
+ "def second():\n"
+ " pass\n"
+ "```"
+ )
+ result = self.normalizer.normalize_text(text)
+ self.assertIn(" pass\n\n\ndef second():", result)
+
+ def test_normalize_text_preserves_tabs_in_code_block(self):
+ """normalize_text keeps tabs inside fenced code blocks intact."""
+ text = (
+ "Intro\n"
+ "```makefile\n"
+ "\tbuild:\n"
+ "\t\tcommand\n"
+ "```"
+ )
+ result = self.normalizer.normalize_text(text)
+ self.assertIn("\tbuild:", result)
+ self.assertIn("\t\tcommand", result)
+
+ def test_normalize_text_crlf_not_mangled(self):
+ """Full pipeline does not mangle \\r\\n into \\r\\r\\n\\n."""
+ text = (
+ "Intro\r\n"
+ "\r\n"
+ "```python\r\n"
+ "def first():\r\n"
+ " pass\r\n"
+ "\r\n"
+ "\r\n"
+ "def second():\r\n"
+ " pass\r\n"
+ "```\r\n"
+ "Outro"
+ )
+ result = self.normalizer.normalize_text(text, line_break_type="windows")
+ self.assertNotIn("\r\r\n\n", result)
+ self.assertIn("pass\r\n\r\n\r\ndef second", result)
+ self.assertIn("Intro\r\n\r\n```python", result)
+
+
if __name__ == "__main__":
unittest.main()