org-rw/org_rw/utils.py

59 lines
1.2 KiB
Python
Raw Normal View History

2021-04-02 22:59:00 +00:00
import uuid
2024-08-18 20:44:00 +00:00
from .org_rw import (
Bold,
Code,
Headline,
Italic,
Line,
ListItem,
2024-08-18 20:49:33 +00:00
RawLine,
2024-08-18 20:44:00 +00:00
Strike,
Text,
Underlined,
Verbatim,
2024-08-18 20:49:33 +00:00
dump_contents,
2024-08-18 20:44:00 +00:00
)
2020-06-27 17:20:34 +00:00
def get_hl_raw_contents(doc: Headline) -> str:
lines = []
for content in doc.contents:
lines.append(get_raw_contents(content))
2020-11-26 22:44:56 +00:00
raw = "".join(lines)
2020-10-09 22:39:32 +00:00
return raw
2020-06-27 17:20:34 +00:00
def get_rawline_contents(doc: RawLine) -> str:
return doc.line
2020-10-09 22:39:32 +00:00
def get_span_contents(doc: Line) -> str:
return doc.get_raw()
2020-11-26 22:44:56 +00:00
2020-10-25 19:23:08 +00:00
def get_text_contents(doc: Text) -> str:
return doc.get_raw()
2020-10-09 22:39:32 +00:00
2020-11-26 22:44:56 +00:00
2020-06-27 17:20:34 +00:00
def get_raw_contents(doc) -> str:
if isinstance(doc, Headline):
return get_hl_raw_contents(doc)
if isinstance(doc, RawLine):
return get_rawline_contents(doc)
2020-10-09 22:39:32 +00:00
if isinstance(doc, Line):
return get_span_contents(doc)
if isinstance(doc, list):
2020-11-26 22:44:56 +00:00
return "".join([get_raw_contents(chunk) for chunk in doc])
2020-10-25 19:23:08 +00:00
if isinstance(doc, (Text, Bold, Code, Italic, Strike, Underlined, Verbatim)):
return doc.get_raw()
if isinstance(doc, ListItem):
return dump_contents(doc)[1]
2020-11-26 22:44:56 +00:00
print("Unhandled type: " + str(doc))
raise NotImplementedError("Unhandled type: " + str(doc))
2021-04-02 22:59:00 +00:00
def random_id() -> str:
return str(uuid.uuid4())