diff --git a/libpurple_to_markdown/__init__.py b/libpurple_to_markdown/__init__.py index b5ec4d8..5e1996a 100644 --- a/libpurple_to_markdown/__init__.py +++ b/libpurple_to_markdown/__init__.py @@ -43,7 +43,6 @@ purposes, but it shouldn't be too difficult to adjust the formatting code. import dataclasses import datetime import logging -import re from collections.abc import Iterable, Iterator from ._version import __version__ @@ -68,47 +67,6 @@ def datetime_sent( return naive -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.split('\n'): - line = re.sub(r'(<[\w ]+>)', r'`\1`', line) - line = re.sub(r'(\$\$\$)', r'`\1`', line) - out.append(f'> {line}\n') - 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) - - MSG_ADJACENTCY_DIST = datetime.timedelta(minutes=2) diff --git a/libpurple_to_markdown/__main__.py b/libpurple_to_markdown/__main__.py index eb2317d..12570a0 100644 --- a/libpurple_to_markdown/__main__.py +++ b/libpurple_to_markdown/__main__.py @@ -6,12 +6,12 @@ from pathlib import Path from . import ( filter_useless_messages, - format_messages, libpurple, merge_adjacent_messages, synctech_sms, ) from .data import MYSELF, Message +from .markdown import format_messages logger = logging.getLogger(__name__) diff --git a/libpurple_to_markdown/data.py b/libpurple_to_markdown/data.py index 8ba53ad..8663a7e 100644 --- a/libpurple_to_markdown/data.py +++ b/libpurple_to_markdown/data.py @@ -1,6 +1,8 @@ import dataclasses import datetime +__all__ = ['MYSELF', 'Message'] + MYSELF = 'MYSELF' diff --git a/libpurple_to_markdown/markdown.py b/libpurple_to_markdown/markdown.py new file mode 100644 index 0000000..2139e65 --- /dev/null +++ b/libpurple_to_markdown/markdown.py @@ -0,0 +1,48 @@ + +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) + return line.strip() + +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'): + out.append(f'> {normalize_line(line)}\n') + 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) diff --git a/test/test_markdown_format.py b/test/test_markdown_format.py new file mode 100644 index 0000000..0251262 --- /dev/null +++ b/test/test_markdown_format.py @@ -0,0 +1,29 @@ +import datetime + +from libpurple_to_markdown.markdown import format_messages +from libpurple_to_markdown.data import Message + +NOW = datetime.datetime.fromtimestamp(1_000_000_000, tz=datetime.UTC) + + +TEXT_1 = 'Hello \nWorld \n ' + +EXPECTED_1='''# Test + +--- +## [[2001-09-09]] + +2001-09-09 01:46:40 [[Test Sender]]: +> Hello +> World + +2001-09-09 01:46:40 [[Test Sender]]: +> Hello +> World + +''' + +def test_whitespace_at_end_of_line(): + msg = Message(NOW, 'Test Sender', TEXT_1, 'Test Chat') + result = format_messages([msg, msg], title='Test') + assert result == EXPECTED_1