2024-11-03 22:49:35 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
from .data import Message
|
|
|
|
|
|
|
|
|
|
|
|
def normalize_line(line: str) -> str:
|
|
|
|
line = re.sub(r'(<[\w ]+>)', r'`\1`', line)
|
|
|
|
line = re.sub(r'(\$\$\$)', r'`\1`', line)
|
2024-11-03 22:57:15 +00:00
|
|
|
line = re.sub(r'\s\s', ' ', line)
|
|
|
|
line = re.sub(r'\s+([.:;!?])', r'\1', line)
|
2024-11-03 22:49:35 +00:00
|
|
|
return line.strip()
|
|
|
|
|
2024-11-03 22:58:40 +00:00
|
|
|
|
2024-11-03 22:49:35 +00:00
|
|
|
def format_message_as_citation(out: list[str], msg: Message) -> None:
|
|
|
|
out.append(f'{msg.sent_at.date()} {msg.sent_at.time()} [[{msg.sender}]]:')
|
|
|
|
out.append('\n')
|
|
|
|
for line in msg.text.strip().split('\n'):
|
2024-11-04 21:52:21 +00:00
|
|
|
normalized = normalize_line(line)
|
2024-11-04 21:49:26 +00:00
|
|
|
out.append('>')
|
2024-11-04 21:52:21 +00:00
|
|
|
if normalized:
|
2024-11-04 21:49:26 +00:00
|
|
|
out.append(' ')
|
2024-11-04 21:52:21 +00:00
|
|
|
out.append(normalized)
|
2024-11-04 21:49:26 +00:00
|
|
|
out.append('\n')
|
2024-11-03 22:49:35 +00:00
|
|
|
del line
|
|
|
|
out.append('\n')
|
|
|
|
|
|
|
|
|
|
|
|
def format_message_as_table(out: list[str], msg: Message) -> None:
|
|
|
|
out.append(f'| {msg.sent_at} | [[{msg.sender}]] | ')
|
|
|
|
for line in msg.text.split('\n'):
|
|
|
|
out.append(f'{line}')
|
|
|
|
del line
|
|
|
|
out.append('|\n')
|
|
|
|
|
|
|
|
|
|
|
|
def format_messages(messages: list[Message], title: str) -> str:
|
|
|
|
out = ['# ', title, '\n\n']
|
|
|
|
|
|
|
|
as_table = False
|
|
|
|
|
|
|
|
for msg_idx, msg in enumerate(messages):
|
|
|
|
if msg_idx == 0 or messages[msg_idx - 1].sent_at.date() != msg.sent_at.date():
|
|
|
|
out.append('---\n')
|
|
|
|
out.append(f'## [[{msg.sent_at.date()}]]\n\n')
|
|
|
|
if as_table:
|
|
|
|
out.append('| sent at | sender | text |\n')
|
|
|
|
out.append('| ------- | ------ | ---- |\n')
|
|
|
|
|
|
|
|
if as_table:
|
|
|
|
format_message_as_table(out, msg)
|
|
|
|
else:
|
|
|
|
format_message_as_citation(out, msg)
|
|
|
|
del msg
|
|
|
|
|
|
|
|
return ''.join(out)
|