|
12 | 12 | #
|
13 | 13 |
|
14 | 14 |
|
| 15 | +def escape_for_markdown(text: str) -> str: |
| 16 | + """ |
| 17 | + Modify a string to properly display in a markdown table cell. |
| 18 | + """ |
| 19 | + if not text: |
| 20 | + return text |
| 21 | + |
| 22 | + # Replace newlines with <br /> tags |
| 23 | + escaped = text.replace('\n', '<br />') |
| 24 | + |
| 25 | + # Escape backslashes. |
| 26 | + escaped = escaped.replace('\\', '\\\\') |
| 27 | + |
| 28 | + # Escape pipe characters that would break table structure |
| 29 | + escaped = escaped.replace('|', '\\|') |
| 30 | + |
| 31 | + return escaped |
| 32 | + |
| 33 | + |
15 | 34 | def generate_markdown(csv_path: str, exit_code: int = 0): # noqa (C901)
|
16 | 35 | # Print warning if exit code is non-zero
|
17 | 36 | if exit_code != 0:
|
@@ -46,7 +65,7 @@ def generate_markdown(csv_path: str, exit_code: int = 0): # noqa (C901)
|
46 | 65 |
|
47 | 66 | for row in data_rows:
|
48 | 67 | # Make a copy of the row to avoid modifying the original
|
49 |
| - processed_row = row.copy() |
| 68 | + processed_row = [escape_for_markdown(cell) for cell in row] |
50 | 69 |
|
51 | 70 | # Count results and collect failed tests
|
52 | 71 | if result_column_index is not None and result_column_index < len(row):
|
@@ -96,7 +115,8 @@ def generate_markdown(csv_path: str, exit_code: int = 0): # noqa (C901)
|
96 | 115 | # Generate Failed Tests section
|
97 | 116 | print("# Failed Tests\n")
|
98 | 117 | if failed_tests:
|
99 |
| - print("| " + " | ".join(header) + " |") |
| 118 | + escaped_header = [escape_for_markdown(col) for col in header] |
| 119 | + print("| " + " | ".join(escaped_header) + " |") |
100 | 120 | print("|" + "|".join(["---"] * len(header)) + "|")
|
101 | 121 | for row in failed_tests:
|
102 | 122 | print("| " + " | ".join(row) + " |")
|
|
0 commit comments