music-kraken-core/music_kraken/objects/formatted_text.py

79 lines
1.9 KiB
Python
Raw Normal View History

2023-02-03 14:21:59 +00:00
import pandoc
"""
TODO
implement in setup.py a skript to install pandocs
https://pandoc.org/installing.html
2023-02-03 14:21:59 +00:00
!!!!!!!!!!!!!!!!!!IMPORTANT!!!!!!!!!!!!!!!!!!
"""
2023-02-03 14:21:59 +00:00
2023-02-03 14:21:59 +00:00
class FormattedText:
2023-03-13 13:28:28 +00:00
"""
the self.html value should be saved to the database
"""
2023-02-03 14:21:59 +00:00
doc = None
def __init__(
self,
plaintext: str = None,
markdown: str = None,
html: str = None
) -> None:
2023-02-03 14:21:59 +00:00
self.set_plaintext(plaintext)
self.set_markdown(markdown)
self.set_html(html)
def set_plaintext(self, plaintext: str):
if plaintext is None:
return
self.doc = pandoc.read(plaintext)
2023-02-03 14:21:59 +00:00
def set_markdown(self, markdown: str):
if markdown is None:
return
self.doc = pandoc.read(markdown, format="markdown")
2023-02-03 14:21:59 +00:00
def set_html(self, html: str):
if html is None:
return
self.doc = pandoc.read(html, format="html")
def get_markdown(self) -> str:
if self.doc is None:
return ""
2023-02-03 14:21:59 +00:00
return pandoc.write(self.doc, format="markdown").strip()
def get_html(self) -> str:
if self.doc is None:
return ""
2023-02-03 14:21:59 +00:00
return pandoc.write(self.doc, format="html").strip()
def get_plaintext(self) -> str:
if self.doc is None:
return ""
return pandoc.write(self.doc, format="plain").strip()
2023-03-13 13:28:28 +00:00
@property
def is_empty(self) -> bool:
return self.doc is None
def __eq__(self, other) -> False:
if type(other) != type(self):
return False
if self.is_empty and other.is_empty:
return True
return self.doc == other.doc
2023-03-28 06:57:50 +00:00
def __str__(self) -> str:
return self.plaintext
2023-02-06 08:16:28 +00:00
plaintext = property(fget=get_plaintext, fset=set_plaintext)
markdown = property(fget=get_markdown, fset=set_markdown)
html = property(fget=get_html, fset=set_html)