2024-04-09 10:21:03 +00:00
|
|
|
import mistune
|
2024-04-19 11:51:08 +00:00
|
|
|
from markdownify import markdownify as md
|
2023-02-03 14:21:59 +00:00
|
|
|
|
2024-04-19 10:17:08 +00:00
|
|
|
|
|
|
|
def plain_to_markdown(plain: str) -> str:
|
|
|
|
return plain.replace("\n", " \n")
|
|
|
|
|
|
|
|
|
2024-04-09 10:21:03 +00:00
|
|
|
class FormattedText:
|
|
|
|
html = ""
|
2023-02-03 14:21:59 +00:00
|
|
|
|
|
|
|
def __init__(
|
2023-02-06 22:47:13 +00:00
|
|
|
self,
|
|
|
|
markdown: str = None,
|
2024-04-19 10:17:08 +00:00
|
|
|
html: str = None,
|
|
|
|
plain: str = None,
|
2023-02-06 22:47:13 +00:00
|
|
|
) -> None:
|
2024-04-09 10:21:03 +00:00
|
|
|
if html is not None:
|
|
|
|
self.html = html
|
|
|
|
elif markdown is not None:
|
|
|
|
self.html = mistune.markdown(markdown)
|
2024-04-19 10:17:08 +00:00
|
|
|
elif plain is not None:
|
|
|
|
self.html = mistune.markdown(plain_to_markdown(plain))
|
2023-03-13 13:28:28 +00:00
|
|
|
|
2023-03-18 11:36:53 +00:00
|
|
|
@property
|
|
|
|
def is_empty(self) -> bool:
|
2024-04-10 16:18:52 +00:00
|
|
|
return self.html == ""
|
2023-03-18 11:36:53 +00:00
|
|
|
|
|
|
|
def __eq__(self, other) -> False:
|
|
|
|
if type(other) != type(self):
|
|
|
|
return False
|
|
|
|
if self.is_empty and other.is_empty:
|
|
|
|
return True
|
|
|
|
|
2024-05-08 19:06:40 +00:00
|
|
|
return self.html == other.html
|
2023-03-18 11:36:53 +00:00
|
|
|
|
2024-04-09 10:21:03 +00:00
|
|
|
@property
|
|
|
|
def markdown(self) -> str:
|
2024-04-19 12:05:05 +00:00
|
|
|
return md(self.html).strip()
|
2023-03-18 11:36:53 +00:00
|
|
|
|
2024-04-09 10:21:03 +00:00
|
|
|
def __str__(self) -> str:
|
|
|
|
return self.markdown
|
2023-02-03 15:33:22 +00:00
|
|
|
|
2024-04-09 10:21:03 +00:00
|
|
|
plaintext = markdown
|
|
|
|
|