From d4b0d0301fb334ebab6caa94b05e2272af13b716 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Sun, 1 Sep 2024 23:51:10 +0200 Subject: [PATCH] Test and implement `org-use-tag-inheritance`. --- org_rw/org_rw.py | 9 ++++++++- tests/test_org.py | 22 +++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/org_rw/org_rw.py b/org_rw/org_rw.py index c139fa3..b57676f 100644 --- a/org_rw/org_rw.py +++ b/org_rw/org_rw.py @@ -754,7 +754,14 @@ class Headline: @property def tags(self) -> list[str]: parent_tags = self.parent.tags - if self.doc.environment.get('org-tags-exclude-from-inheritance'): + if self.doc.environment.get('org-use-tag-inheritance'): + accepted_tags = [] + for tag in self.doc.environment.get('org-use-tag-inheritance'): + if tag in parent_tags: + accepted_tags.append(tag) + parent_tags = accepted_tags + + elif self.doc.environment.get('org-tags-exclude-from-inheritance'): for tag in self.doc.environment.get('org-tags-exclude-from-inheritance'): if tag in parent_tags: parent_tags.remove(tag) diff --git a/tests/test_org.py b/tests/test_org.py index 662a472..cf370b6 100644 --- a/tests/test_org.py +++ b/tests/test_org.py @@ -910,7 +910,7 @@ class TestSerde(unittest.TestCase): with open(os.path.join(DIR, "13-tags.org")) as f: orig = f.read() doc = loads(orig, { - 'org-tags-exclude-from-inheritance': ('h1tag', 'otherh2tag') + 'org-tags-exclude-from-inheritance': ('h1tag', 'otherh2tag'), }) self.assertEqual(doc.tags, ['filetag']) @@ -925,6 +925,26 @@ class TestSerde(unittest.TestCase): h1_2_h2 = h1_2.children[0] self.assertEqual(sorted(h1_2_h2.tags), ['filetag', 'otherh1tag', 'otherh2tag']) + def test_select_tags_to_inheritance_property_read_13(self): + with open(os.path.join(DIR, "13-tags.org")) as f: + orig = f.read() + doc = loads(orig, { + 'org-tags-exclude-from-inheritance': ('h1tag', 'otherh2tag'), + 'org-use-tag-inheritance': ('h1tag',), + }) + + self.assertEqual(doc.tags, ['filetag']) + + h1_1, h1_2 = doc.getTopHeadlines() + self.assertEqual(sorted(h1_1.tags), ['h1tag']) + self.assertEqual(sorted(h1_2.tags), ['otherh1tag']) + + h1_1_h2 = h1_1.children[0] + self.assertEqual(sorted(h1_1_h2.tags), ['h1tag', 'h2tag']) + + h1_2_h2 = h1_2.children[0] + self.assertEqual(sorted(h1_2_h2.tags), ['otherh2tag']) + def print_tree(tree, indentation=0, headline=None): for element in tree: print(" " * indentation * 2, "EL:", element)