From bd57dff091cd44296d01cc64a3d651d28e0c1c46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Sat, 14 Aug 2021 17:40:08 +0200 Subject: [PATCH] Close PROPERTY drawers when there's no empty lines between headlines. --- .gitignore | 3 +++ org_rw/org_rw.py | 14 ++++++++++++++ tests/08-property-creation.org | 6 ++++++ tests/test_org.py | 29 +++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 tests/08-property-creation.org diff --git a/.gitignore b/.gitignore index a81c8ee..5c8ee49 100644 --- a/.gitignore +++ b/.gitignore @@ -82,6 +82,9 @@ target/ profile_default/ ipython_config.py +# Idea IDE +.idea + # pyenv # For a library or package, you might want to ignore these files since the code is # intended to run in multiple environments; otherwise, check them in: diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index 2d155a2..0f06f9d 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -1472,6 +1472,20 @@ class OrgDoc: last_type = ltype structured_lines.append(content) + if last_type == PROPERTIES_T: + # No structural closing + + indentation = 0 + if len(lines) > 0: + last_line = lines[i - 1][1][1] + indentation = last_line.index(":") + structured_lines.append(" " * indentation + ":END:\n") + logging.warning( + "Added structural:{}: {}".format( + line[1][0], structured_lines[-1].strip() + ) + ) + if len(structured_lines) > 0: content = "".join(structured_lines) diff --git a/tests/08-property-creation.org b/tests/08-property-creation.org new file mode 100644 index 0000000..774d0e0 --- /dev/null +++ b/tests/08-property-creation.org @@ -0,0 +1,6 @@ +#+TITLE: 08-Property-creation + +* Top headline +** Second headline + +*** Third headline diff --git a/tests/test_org.py b/tests/test_org.py index 15d68cd..fc27430 100644 --- a/tests/test_org.py +++ b/tests/test_org.py @@ -507,3 +507,32 @@ class TestSerde(unittest.TestCase): hl = doc.getTopHeadlines()[0] self.assertEqual(hl.get_property("ID"), "419f4651-21c8-4166-b8d5-692c34be9f93") self.assertEqual(len(hl.children), 1) + + def test_org_property_creation_08(self): + with open(os.path.join(DIR, "08-property-creation.org")) as f: + orig = f.read() + doc = loads(orig) + + headline = doc.getTopHeadlines()[0] + headline.id = "first" + + second = headline.children[0] + second.id = "second" + + self.assertEqual( + dumps(doc).strip(), + """ + #+TITLE: 08-Property-creation + +* Top headline +:PROPERTIES: +:ID: first +:END: +** Second headline +:PROPERTIES: +:ID: second +:END: + +*** Third headline + """.strip(), + )