Skip to content

Commit e6453b1

Browse files
committed
Fix ANSI escape sequence corruption in CI tests - Replace temporary console rendering with direct Rich Text objects in list rendering - Fix double-escaping issue that occurred when TERM environment variable was unset - Update temporary console configurations to use consistent terminal settings - Update snapshots to reflect corrected ANSI escape sequence output - Tests now pass consistently in both local and CI environments
1 parent 1be35d5 commit e6453b1

File tree

3 files changed

+90
-41
lines changed

3 files changed

+90
-41
lines changed

md2term.py

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,13 @@ def _render_blockquote(self, token: Dict[str, Any]) -> None:
132132
# Render children into a string buffer
133133
old_console = self.console
134134
buffer = StringIO()
135-
temp_console = Console(file=buffer, width=self.console.size.width - 4)
135+
temp_console = Console(
136+
file=buffer,
137+
width=self.console.size.width - 4,
138+
force_terminal=True,
139+
color_system="256",
140+
legacy_windows=False
141+
)
136142
self.console = temp_console
137143

138144
for child in token['children']:
@@ -242,7 +248,13 @@ def _render_callout(self, content: str, callout_info: Dict[str, str]) -> None:
242248
# Parse and render the content with markdown
243249
old_console = self.console
244250
content_buffer = StringIO()
245-
temp_console = Console(file=content_buffer, width=self.console.size.width - 6)
251+
temp_console = Console(
252+
file=content_buffer,
253+
width=self.console.size.width - 6,
254+
force_terminal=True,
255+
color_system="256",
256+
legacy_windows=False
257+
)
246258
self.console = temp_console
247259

248260
# Parse the content as markdown
@@ -290,20 +302,45 @@ def _render_list(self, token: Dict[str, Any]) -> None:
290302
marker = "•"
291303
marker_style = "bold yellow"
292304

293-
# Render list item content
294-
old_console = self.console
295-
buffer = StringIO()
296-
temp_console = Console(file=buffer, width=self.console.size.width - 4)
297-
self.console = temp_console
305+
# Create the marker text
306+
marker_text = Text(marker, style=marker_style)
298307

308+
# Render list item content as Rich Text objects
309+
content_text = Text()
299310
for child in item['children']:
300-
self._render_token(child)
301-
302-
self.console = old_console
303-
content = buffer.getvalue().rstrip()
304-
305-
# Print with proper indentation
306-
self.console.print(f"[{marker_style}]{marker}[/] {content}")
311+
if child['type'] == 'paragraph':
312+
# For paragraphs, render inline tokens directly
313+
for inline_child in child['children']:
314+
if inline_child['type'] == 'text':
315+
content_text.append(inline_child['raw'])
316+
else:
317+
# Render other inline tokens
318+
inline_text = self._render_inline_tokens([inline_child])
319+
content_text.append_text(inline_text)
320+
else:
321+
# For other types, fall back to the old method
322+
old_console = self.console
323+
buffer = StringIO()
324+
temp_console = Console(
325+
file=buffer,
326+
width=self.console.size.width - 4,
327+
force_terminal=True,
328+
color_system="256",
329+
legacy_windows=False
330+
)
331+
self.console = temp_console
332+
self._render_token(child)
333+
self.console = old_console
334+
content_text.append(buffer.getvalue().rstrip())
335+
336+
# Combine marker and content
337+
line_text = Text()
338+
line_text.append_text(marker_text)
339+
line_text.append(" ")
340+
line_text.append_text(content_text)
341+
342+
# Print the complete line
343+
self.console.print(line_text)
307344

308345
def _render_thematic_break(self) -> None:
309346
"""Render a horizontal rule."""

tests/__snapshots__/test_md2term.ambr

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,10 @@
128128
# name: TestMarkdownFeatures.test_blockquotes
129129
'''
130130
│ This is a blockquote with some important information. It can span multiple 
131-
│ lines and contain bold and italic text.
131+
│ lines and contain [1mbold[0m and [3mitalic[0m text.
132132
│
133-
│ It can even contain inline code and links (https://example.com).
133+
│ It can even contain [1;31;40minline code[0m and [1;4;34mlinks[0m[2;34m 
134+
(https://example.com)[0m.
134135

135136
'''
136137
# ---
@@ -250,24 +251,25 @@
250251
Unordered List
251252

252253
• First item
253-
• Second item with bold text
254-
• Third item with italic text
255-
• Fourth item with inline code
254+
• Second item with bold text
255+
• Third item with italic text
256+
• Fourth item with inline code
256257

257258
Ordered List
258259

259-
1. First numbered item
260-
2. Second numbered item
261-
3. Third numbered item with a link (https://example.com)
262-
4. Fourth numbered item
260+
1. First numbered item
261+
2. Second numbered item
262+
3. Third numbered item with a link (https://example.com)
263+
4. Fourth numbered item
263264

264265
Blockquotes
265266
────────────────────────────────────────────────────────────────────────────────
266267

267268
│ This is a blockquote with some important information. It can span multiple 
268-
│ lines and contain bold and italic text.
269+
│ lines and contain [1mbold[0m and [3mitalic[0m text.
269270
│
270-
│ It can even contain inline code and links (https://example.com).
271+
│ It can even contain [1;31;40minline code[0m and [1;4;34mlinks[0m[2;34m 
272+
(https://example.com)[0m.
271273

272274
│ This is another blockquote to show multiple quotes.
273275

@@ -351,18 +353,21 @@
351353
╭────────────────────────────────────────────────────────────────────────────╮
352354
│ ⚠️ Complex Warning │
353355
│ │
354-
│ This callout contains bold text, italic text, and inline code. │
356+
│ This callout contains bold text, italic text, and │
357+
│ inline code. │
355358
│ │
356359
│ It can also contain: │
357360
│ │
358-
│ • Bullet points • Multiple paragraphs • Even links (https://example.com) │
359-
│ ╭───────────────────────────────────────────────────────────────────────── │
360-
│ ─╮ │ # And code blocks! │
361-
│ │ │ def example(): │
362-
│ │ │ return "Hello from a callout!" │
363-
│ │ │
364-
│ ╰───────────────────────────────────────────────────────────────────────── │
365-
│ ─╯ │
361+
│ • Bullet points • Multiple paragraphs • Even │
362+
│ links (https://example.com) │
363+
│ ╭────────────────────────────────────────────────────────────────────── │
364+
│ ────╮ │ # And code blocks! │
365+
│ │ │ def example(): │
366+
│ │ │  return "Hello │
367+
│ from a callout!" │
368+
│ │ │
369+
│ ╰────────────────────────────────────────────────────────────────────── │
370+
│ ────╯ │
366371
╰────────────────────────────────────────────────────────────────────────────╯
367372

368373
That's all folks!
@@ -419,10 +424,10 @@
419424
# ---
420425
# name: TestMarkdownFeatures.test_ordered_lists
421426
'''
422-
1. First numbered item
423-
2. Second numbered item
424-
3. Third numbered item with a link (https://example.com)
425-
4. Fourth numbered item
427+
1. First numbered item
428+
2. Second numbered item
429+
3. Third numbered item with a link (https://example.com)
430+
4. Fourth numbered item
426431

427432
'''
428433
# ---
@@ -436,9 +441,9 @@
436441
# name: TestMarkdownFeatures.test_unordered_lists
437442
'''
438443
• First item
439-
• Second item with bold text
440-
• Third item with italic text
441-
• Fourth item with inline code
444+
• Second item with bold text
445+
• Third item with italic text
446+
• Fourth item with inline code
442447

443448
'''
444449
# ---

tests/test_md2term.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@
1515

1616
def create_test_console(output, width=80):
1717
"""Create a console with consistent settings for testing."""
18+
import os
19+
# Force consistent environment for testing
20+
os.environ['FORCE_COLOR'] = '1'
21+
os.environ['TERM'] = 'xterm-256color'
22+
os.environ['COLORTERM'] = 'truecolor'
23+
os.environ['NO_COLOR'] = ''
24+
1825
return Console(
1926
file=output,
2027
width=width,

0 commit comments

Comments
 (0)