Commits

Luke Plant committed 745af0a

Beginnings of algorithm to create section divs

  • Participants
  • Parent commits be2fc99

Comments (0)

Files changed (2)

File semanticeditor/tests.py

         self.assertEqual(html, format_html(html, {}))
 
     def test_no_styling(self):
-        html = "<h1>Hello</h1><p>P 1</p><h2>Heading 2</h2>"
-        outh = "<div><h1>Hello</h1><p>P 1</p><div><h2>Heading 2</h2></div></div>"
+        html = "<h1>Hello</h1><p>P 1</p><h2>Heading 2</h2><h1>Another</h1><p>So</p>"
+        outh = "<div><h1>Hello</h1><p>P 1</p><div><h2>Heading 2</h2></div></div><div><h1>Another</h1><p>So</p></div>"
         self.assertEqual(outh, format_html(html, {}))
 
     def test_existing_divs(self):

File semanticeditor/utils.py

     and values which are lists of CSS classes or special commands.
     Commands start with 'command:'
     """
+    # Ensure that the headings are well formed and the HTML is valid
+    headingnames = extract_headings(html)
+
     tree = parse(html)
+
     # Strip existing divs
     remove_tag(tree, 'div')
 
+    # Get the heading nodes, decorated with the level of the heading
+    headers = [(int(n.tag[1]), n) for n in tree.getiterator() if n.tag in headingdef]
+
+    # 'scope' of each section is from heading node to before the next
+    # heading with a level the same or higher
+
+    # First, we assume that all h1, h2 etc tags will be children of
+    # the root.  remove_tag should have ensured that.
+    for level, h in headers:
+        name = flatten(h)
+        # TODO: assert that the node is a child of root
+        nextnodes = [(l,n) for (l,n) in headers if l <= level]
+        if not nextnodes:
+            # scope extends to end
+            # TODO
+            pass
+        else:
+            # scope extends to before n
+            # TODO
+            pass
+        # TODO - insert div around scope
+        # TODO - apply styles
+        # TODO - store div for later processing
+
+    # TODO - apply commands to divs
+
     return ET.tostring(tree).replace('<html>','').replace('</html>','')