org-rw/org_dom/utils.py

36 lines
1 KiB
Python
Raw Normal View History

2020-10-25 20:23:08 +01:00
from .org_dom import Headline, Line, RawLine, Text, Bold, Code, Italic, Strike, Underlined, Verbatim
2020-06-27 19:20:34 +02:00
def get_hl_raw_contents(doc: Headline) -> str:
lines = []
for content in doc.contents:
lines.append(get_raw_contents(content))
2020-10-10 00:39:32 +02:00
raw = ''.join(lines)
return raw
2020-06-27 19:20:34 +02:00
def get_rawline_contents(doc: RawLine) -> str:
return doc.line
2020-10-10 00:39:32 +02:00
def get_span_contents(doc: Line) -> str:
return doc.get_raw()
2020-10-25 20:23:08 +01:00
def get_text_contents(doc: Text) -> str:
return doc.get_raw()
2020-10-10 00:39:32 +02:00
2020-06-27 19:20:34 +02: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-10 00:39:32 +02: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 20:23:08 +01:00
if isinstance(doc, (Text, Bold, Code, Italic, Strike, Underlined, Verbatim)):
return doc.get_raw()
print('Unhandled type: ' + str(doc))
2020-06-27 19:20:34 +02:00
raise NotImplementedError('Unhandled type: ' + str(doc))