org-rw/org_rw/utils.py

45 lines
1.1 KiB
Python
Raw Normal View History

2021-04-02 22:59:00 +00:00
import uuid
2020-12-20 11:39:47 +00:00
from .org_rw import (Bold, Code, Headline, Italic, Line, RawLine, Strike, Text,
Underlined, Verbatim)
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()
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())