edgimar avatar edgimar committed 944a9e0

- handle initial text in an org-file which isn't part of a headline

Initial text in a file that isn't a headline (e.g. preceded with a "* ") will
be treated as the body (notes) text for an empty headline (i.e. a line
containing only "* "). In other words, the text will be parsed as if there
were an empty headline inserted before the non-headline lines.

- added test for this initial non-headline condition

Comments (0)

Files changed (2)

 from oauth2client.file import Storage
 from oauth2client.client import OAuth2WebServerFlow
 from oauth2client.tools import run
-from xdg.BaseDirectory import save_config_path, save_data_path
+from xdg.BaseDirectory import save_data_path #, save_config_path 
 import os.path
 import sys
 import re
     headline_regex = re.compile("^(\*+ )( *)(DONE )?")
     tasks_tree = TasksTree()
     
+    indent_level = 0
     last_indent_level = 0
     seen_first_task = False
+    task_notes = None
     for n, line in enumerate(f):
         matches = headline_regex.findall(line)
         line = line.rstrip("\n")
                         task_notes=task_notes,
                         task_status=task_status)
             else:
-                # this is the first task, so skip adding a task to the
+                if task_notes is not None:
+                    # this means there was some text at the beginning of the
+                    # file before any headline was encountered.  We create a
+                    # dummy headline to contain this text.
+                    tasks_tree.last(0).add_subtask(
+                            title="",
+                            task_notes=task_notes,
+                            task_status=None)
+                # this is the first task, so skip adding a last-task to the
                 # tree, and record that we've encountered our first task
                 seen_first_task = True
             
 Suite of unit-tests for testing Michel
 """
 import unittest
+import textwrap
 import michel.michel as m
 
 class TestMichel(unittest.TestCase):
     
     def test_text_to_tasktree(self):
         # text should have trailing "\n" character, like most textfiles
-        org_text = """\
-* Headline 1
-Body 1a
-Body 1b
-* DONE Headline 2
-** Headline 2.1
-"""
+        org_text = textwrap.dedent("""\
+            * Headline 1
+            Body 1a
+                Body 1b
+            * DONE    Headline 2
+            ** Headline 2.1
+            """)
+
+        tasktree = m.parse_text(org_text)
+        self.assertEqual(str(tasktree), org_text)
+
+    def test_initial_non_headline_text(self):
+        """
+        Test the case where the first lines of an org-mode file are not
+        org-mode headlines.
         
+        """
+        # text should have trailing "\n" character, like most textfiles
+        org_text = textwrap.dedent("""\
+
+            Some non-headline text...
+            Another line of it.
+            * Headline 1
+            Body 1a
+                Body 1b
+            * DONE    Headline 2
+            ** Headline 2.1
+            """)
+
         tasktree = m.parse_text(org_text)
-        self.assertEqual(org_text, str(tasktree))
-    
-    def test_tree_to_text(self):
-        pass
-
+        # a dummy headline will be added to contain the initial text
+        self.assertEqual(str(tasktree), "* \n" + org_text)
 
 if __name__ == '__main__':
     unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.