Skip to content

Commit 91cdec1

Browse files
committed
feat: add get_file_property and get_only_file_property
1 parent 9184c9a commit 91cdec1

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

orgparse/node.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ def parse_comment(line):
152152
>>> parse_comment('# not a special comment') # None
153153
154154
"""
155-
if line.startswith('#+'):
156-
comment = line.lstrip('#+').split(':', 1)
155+
if re.match(r'\s*#\+', line):
156+
comment = re.split(r':\s+', re.split(r'\s*#\+',line)[1])
157157
if len(comment) == 2:
158158
return (comment[0], comment[1].strip())
159159

@@ -717,6 +717,27 @@ def __unicode__(self):
717717
def __str__(self):
718718
return unicode(self).encode('utf-8')
719719

720+
def get_file_property(self, property):
721+
"""
722+
Return a list of the selected property
723+
"""
724+
print(self._special_comments)
725+
if property in self._special_comments:
726+
return self._special_comments[property]
727+
else:
728+
return None
729+
730+
def get_only_file_property(self, property):
731+
"""
732+
Return a single element of the selected property
733+
"""
734+
elements = self.get_file_property(property)
735+
if elements:
736+
return elements[0]
737+
else:
738+
return None
739+
740+
720741

721742
class OrgRootNode(OrgBaseNode):
722743

orgparse/tests/test_misc.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,38 @@ def test_add_custom_todo_keys():
8282
root = loads(content, filename, env)
8383
assert root.env.all_todo_keys == ['CUSTOM_TODO', 'COMMENT_TODO',
8484
'CUSTOM_DONE', 'COMMENT_DONE']
85+
86+
def test_get_custom_property():
87+
filename = '<string>' # default for loads
88+
content = """#+TITLE: Test title
89+
* Node 1
90+
test 1
91+
* Node 2
92+
test 2
93+
"""
94+
95+
env = OrgEnv(filename=filename)
96+
97+
# after parsing, all keys are set
98+
root = loads(content, filename, env)
99+
assert root.get_file_property('TITLE') == ['Test title']
100+
assert root.get_only_file_property('TITLE') == 'Test title'
101+
102+
def test_get_custom_property_multivalued():
103+
filename = '<string>' # default for loads
104+
content = """#+TITLE: Test
105+
#+OTHER: Test title
106+
#+TITLE: alternate title
107+
108+
* Node 1
109+
test 1
110+
* Node 2
111+
test 2
112+
"""
113+
114+
env = OrgEnv(filename=filename)
115+
116+
# after parsing, all keys are set
117+
root = loads(content, filename, env)
118+
assert root.get_file_property('TITLE') == ['Test', 'alternate title']
119+
assert root.get_only_file_property('TITLE') == 'Test'

0 commit comments

Comments
 (0)