Tests of markdown functionality
This commit is contained in:
parent
0050570cfb
commit
352caa0fdd
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
@ -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__)
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import dataclasses
|
||||
import datetime
|
||||
|
||||
__all__ = ['MYSELF', 'Message']
|
||||
|
||||
MYSELF = 'MYSELF'
|
||||
|
||||
|
||||
|
|
48
libpurple_to_markdown/markdown.py
Normal file
48
libpurple_to_markdown/markdown.py
Normal file
|
@ -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)
|
29
test/test_markdown_format.py
Normal file
29
test/test_markdown_format.py
Normal file
|
@ -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
|
Loading…
Reference in New Issue
Block a user