diff --git a/scraibe/email_sender.py b/scraibe/email_sender.py index 2401f2e..0072ee8 100644 --- a/scraibe/email_sender.py +++ b/scraibe/email_sender.py @@ -496,13 +496,28 @@ def create_transcript_docx(text: str, filename: str): if current: content_lines.append(current) - # First visual line: include label if present + # First visual line: include label if present, ensuring total <= max_chars if content_lines: - first_line_text = (label_text + " " if label_text else "") + content_lines[0] + first_content = content_lines.pop(0) + if label_text: + prefix = label_text + " " + # If too long, trim first_content at word boundary + if len(prefix) + len(first_content) > max_chars: + allowed = max_chars - len(prefix) + if allowed < 1: + allowed = 1 + # Truncate at word boundary + candidate = first_content[:allowed] + last_space = candidate.rfind(" ") + if last_space > 0: + candidate = candidate[:last_space] + first_content = candidate + first_line_text = prefix + first_content + else: + first_line_text = first_content + line_number += 1 _add_transcript_paragraph(doc, first_line_text, line_number=line_number) - # Remove remaining content lines' leading content (already done) from first line - content_lines = content_lines[1:] # Subsequent visual lines: no label, just content for cl in content_lines: