org-rw/org_dom/utils.py

36 lines
1.0 KiB
Python
Raw Normal View History

2020-10-25 19:23:08 +00:00
from .org_dom import Headline, Line, RawLine, Text, Bold, Code, Italic, Strike, 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-10-09 22:39:32 +00:00
raw = ''.join(lines)
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-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-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):
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()
print('Unhandled type: ' + str(doc))
2020-06-27 17:20:34 +00:00
raise NotImplementedError('Unhandled type: ' + str(doc))