Track ancestor headings for each org-mode entry in org-node parser

This commit is contained in:
Debanjum Singh Solanky
2023-11-16 00:13:39 -08:00
parent 208ddddc6a
commit 305c25ae1a
2 changed files with 116 additions and 3 deletions

View File

@@ -80,6 +80,7 @@ def makelist(file, filename):
} # populated from #+SEQ_TODO line
level = ""
heading = ""
ancestor_headings = [f"{filename}"]
bodytext = ""
introtext = ""
tags = list() # set of all tags in headline
@@ -98,7 +99,7 @@ def makelist(file, filename):
heading_search = re.search(r"^(\*+)\s(.*?)\s*$", line)
if heading_search: # we are processing a heading line
if heading: # if we have are on second heading, append first heading to headings list
thisNode = Orgnode(level, heading, bodytext, tags)
thisNode = Orgnode(level, heading, bodytext, tags, ancestor_headings)
if closed_date:
thisNode.closed = closed_date
closed_date = ""
@@ -114,6 +115,8 @@ def makelist(file, filename):
thisNode.properties = property_map
nodelist.append(thisNode)
property_map = {"LINE": f"file:{normalize_filename(filename)}::{ctr}"}
previous_level = level
previous_heading = heading
level = heading_search.group(1)
heading = heading_search.group(2)
bodytext = ""
@@ -126,6 +129,17 @@ def makelist(file, filename):
for parsedtag in parsedtags.split(":"):
if parsedtag != "":
tags.append(parsedtag)
# Add previous heading to ancestors if current heading is deeper than previous level
if len(level) > len(previous_level) and previous_heading:
ancestor_headings.append(previous_heading)
# Remove last ancestor(s) if current heading is shallower than previous level
elif len(level) < len(previous_level):
for _ in range(len(level), len(previous_level)):
if not ancestor_headings or len(ancestor_headings) == 0:
break
ancestor_headings.pop()
else: # we are processing a non-heading line
if line[:10] == "#+SEQ_TODO":
kwlist = re.findall(r"([A-Z]+)\(", line)
@@ -216,7 +230,7 @@ def makelist(file, filename):
nodelist = [thisNode] + nodelist
# write out last heading node
if heading:
thisNode = Orgnode(level, heading, bodytext, tags)
thisNode = Orgnode(level, heading, bodytext, tags, ancestor_headings)
thisNode.properties = property_map
if sched_date:
thisNode.scheduled = sched_date
@@ -261,7 +275,7 @@ class Orgnode(object):
with the headline.
"""
def __init__(self, level, headline, body, tags):
def __init__(self, level, headline, body, tags, ancestor_headings=[]):
"""
Create an Orgnode object given the parameters of level (as the
raw asterisks), headline text (including the TODO tag), and
@@ -279,9 +293,17 @@ class Orgnode(object):
self._closed = "" # Closed date
self._properties = dict()
self._logbook = list() # List of clock-in, clock-out tuples representing logbook entries
self._ancestor_headings = ancestor_headings.copy()
# Look for priority in headline and transfer to prty field
@property
def ancestors(self):
"""
Return the Heading text of the node without the TODO tag
"""
return self._ancestor_headings
@property
def heading(self):
"""