From 976397bd82fde0852ed6509624312297ce053fb5 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Sat, 10 Sep 2022 15:22:26 +0300 Subject: [PATCH] Ignore empty #+TITLE, merge multiple #+TITLE for 0th level headings --- src/processor/org_mode/orgnode.py | 10 +++- tests/test_orgnode.py | 94 ++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 3 deletions(-) diff --git a/src/processor/org_mode/orgnode.py b/src/processor/org_mode/orgnode.py index 8d88bf18..f5c55e7f 100644 --- a/src/processor/org_mode/orgnode.py +++ b/src/processor/org_mode/orgnode.py @@ -118,8 +118,14 @@ def makelist(filename): for kw in kwlist: todos[kw] = "" # Set file title to TITLE property, if it exists - if line[:8] == "#+TITLE:": - file_title = line[8:].strip() + title_search = re.search(r'^#\+TITLE:\s*(.*)$', line) + if title_search and title_search.group(1).strip() != '': + title_text = title_search.group(1).strip() + if file_title == f'{filename}': + file_title = title_text + else: + file_title += f' {title_text}' + continue # Ignore Properties Drawers Completely if re.search(':PROPERTIES:', line): diff --git a/tests/test_orgnode.py b/tests/test_orgnode.py index 2db68a0b..eebf7f8a 100644 --- a/tests/test_orgnode.py +++ b/tests/test_orgnode.py @@ -8,6 +8,28 @@ from src.processor.org_mode import orgnode # Test +# ---------------------------------------------------------------------------------------------------- +def test_parse_entry_with_no_headings(tmp_path): + "Test parsing of entry with minimal fields" + # Arrange + entry = f'''Body Line 1''' + orgfile = create_file(tmp_path, entry) + + # Act + entries = orgnode.makelist(orgfile) + + # Assert + assert len(entries) == 1 + assert entries[0].Heading() == f'{orgfile}' + assert entries[0].Tags() == set() + assert entries[0].Body() == "Body Line 1" + assert entries[0].Priority() == "" + assert entries[0].Property("ID") == "" + assert entries[0].Closed() == "" + assert entries[0].Scheduled() == "" + assert entries[0].Deadline() == "" + + # ---------------------------------------------------------------------------------------------------- def test_parse_minimal_entry(tmp_path): "Test parsing of entry with minimal fields" @@ -166,10 +188,80 @@ Body 2 assert entry.Logbook() == [(datetime.datetime(1984,4,index+1,9,0,0), datetime.datetime(1984,4,index+1,12,0,0))] +# ---------------------------------------------------------------------------------------------------- +def test_parse_entry_with_empty_title(tmp_path): + "Test parsing of entry with minimal fields" + # Arrange + entry = f'''#+TITLE: +Body Line 1''' + orgfile = create_file(tmp_path, entry) + + # Act + entries = orgnode.makelist(orgfile) + + # Assert + assert len(entries) == 1 + assert entries[0].Heading() == f'{orgfile}' + assert entries[0].Tags() == set() + assert entries[0].Body() == "Body Line 1" + assert entries[0].Priority() == "" + assert entries[0].Property("ID") == "" + assert entries[0].Closed() == "" + assert entries[0].Scheduled() == "" + assert entries[0].Deadline() == "" + + +# ---------------------------------------------------------------------------------------------------- +def test_parse_entry_with_title_and_no_headings(tmp_path): + "Test parsing of entry with minimal fields" + # Arrange + entry = f'''#+TITLE: test +Body Line 1''' + orgfile = create_file(tmp_path, entry) + + # Act + entries = orgnode.makelist(orgfile) + + # Assert + assert len(entries) == 1 + assert entries[0].Heading() == 'test' + assert entries[0].Tags() == set() + assert entries[0].Body() == "Body Line 1" + assert entries[0].Priority() == "" + assert entries[0].Property("ID") == "" + assert entries[0].Closed() == "" + assert entries[0].Scheduled() == "" + assert entries[0].Deadline() == "" + + +# ---------------------------------------------------------------------------------------------------- +def test_parse_entry_with_multiple_titles_and_no_headings(tmp_path): + "Test parsing of entry with minimal fields" + # Arrange + entry = f'''#+TITLE: title1 +Body Line 1 +#+TITLE: title2 ''' + orgfile = create_file(tmp_path, entry) + + # Act + entries = orgnode.makelist(orgfile) + + # Assert + assert len(entries) == 1 + assert entries[0].Heading() == 'title1 title2' + assert entries[0].Tags() == set() + assert entries[0].Body() == "Body Line 1\n" + assert entries[0].Priority() == "" + assert entries[0].Property("ID") == "" + assert entries[0].Closed() == "" + assert entries[0].Scheduled() == "" + assert entries[0].Deadline() == "" + + # Helper Functions def create_file(tmp_path, entry, filename="test.org"): org_file = tmp_path / f"notes/{filename}" org_file.parent.mkdir() org_file.touch() org_file.write_text(entry) - return org_file \ No newline at end of file + return org_file