From 9d369ae4df4936b8aa3ecccc3e3be0cc31746413 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Sun, 11 Sep 2022 15:54:26 +0300 Subject: [PATCH] Fix OrgNode render of entries with property drawers and empty body - Issue - Indent regex was previously catching escape sequences like newlines - This was resulting in entries with only escape sequences in body to be prepended to property drawers etc during rendering - Fix - Update indent regex to only look for spaces in each line - Only render body when body contains non-escape characters - Create test to prevent this regression from silently resurfacing --- src/processor/org_mode/orgnode.py | 6 ++++-- tests/test_orgnode.py | 30 ++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/processor/org_mode/orgnode.py b/src/processor/org_mode/orgnode.py index bed27975..1853c84b 100644 --- a/src/processor/org_mode/orgnode.py +++ b/src/processor/org_mode/orgnode.py @@ -38,7 +38,7 @@ import datetime from pathlib import Path from os.path import relpath -indent_regex = re.compile(r'^\s*') +indent_regex = re.compile(r'^ *') def normalize_filename(filename): "Normalize and escape filename for rendering" @@ -455,6 +455,8 @@ class Orgnode(object): n = n + indent + f":{key}: {value}\n" n = n + indent + ":END:\n" - n = n + self._body + # Output Body + if self.hasBody: + n = n + self._body return n diff --git a/tests/test_orgnode.py b/tests/test_orgnode.py index 372969ab..d36cca79 100644 --- a/tests/test_orgnode.py +++ b/tests/test_orgnode.py @@ -1,7 +1,5 @@ # Standard Packages import datetime -from os.path import relpath -from pathlib import Path # Internal Packages from src.processor.org_mode import orgnode @@ -89,6 +87,34 @@ Body Line 2''' assert entries[0].logbook == [(datetime.datetime(1984,4,1,9,0,0), datetime.datetime(1984,4,1,12,0,0))] +# ---------------------------------------------------------------------------------------------------- +def test_render_entry_with_property_drawer_and_empty_body(tmp_path): + "Render heading entry with property drawer" + # Arrange + entry_to_render = f''' +*** [#A] Heading1 :tag1: + :PROPERTIES: + :ID: 111-111-111-1111-1111 + :END: +\t\r \n +''' + orgfile = create_file(tmp_path, entry_to_render) + + expected_entry = f'''*** [#A] Heading1 :tag1: +:PROPERTIES: +:LINE: file:{orgfile}::2 +:ID: id:111-111-111-1111-1111 +:SOURCE: [[file:{orgfile}::*Heading1]] +:END: +''' + + # Act + parsed_entries = orgnode.makelist(orgfile) + + # Assert + assert f'{parsed_entries[0]}' == expected_entry + + # ---------------------------------------------------------------------------------------------------- def test_all_links_to_entry_rendered(tmp_path): "Ensure all links to entry rendered in property drawer from entry"