Commits

Luke Plant committed e0319ee

Softened the requirement that headings must start at H1

Comments (0)

Files changed (2)

semanticeditor/tests.py

     def test_rejects_bad_html(self):
         self.assertRaises(InvalidHtml, extract_headings, "<h1>Foo")
 
-    def test_rejects_headings_not_start_at_1(self):
-        self.assertRaises(IncorrectHeadings, extract_headings, "<h2>Hello</h2>")
+    def test_rejects_higher_headings_later(self):
+        """
+        Ensures that if the first heading is e.g. h2, no h1 headings
+        are allowed
+        """
+        self.assertRaises(IncorrectHeadings, extract_headings, "<h2>Hello</h2><h1>Hi</h1>")
 
     def test_rejects_improper_headings(self):
         self.assertRaises(IncorrectHeadings, extract_headings, "<h1>Hello</h1><h3>Bad heading</h3>")

semanticeditor/utils/presentation.py

     heading_names = set()
     cur_level = 1
     last_heading_num = 0
+    first_heading_level = 0
     for n in root.getiterator():
         if n.tag in blockdef:
             text = flatten(n)
                 level = int(n.tag[1])
                 cur_level = level
                 if assert_structure:
-                    if len(heading_names) == 0 and level > 1:
-                        raise IncorrectHeadings("First heading must be H1.")
+                    if len(heading_names) == 0:
+                        first_heading_level = level
+                    else:
+                        if level < first_heading_level:
+                            raise IncorrectHeadings("No heading can be higher than the first heading, which was H%d." % first_heading_level)
 
                     if name in heading_names:
                         raise IncorrectHeadings('There are duplicate headings with the name'
                                                 ' "%s".' % name)
 
                     # Headings should decrease or monotonically increase
-                    if level > last_heading_num + 1:
+                    if len(heading_names) > 0 and level > last_heading_num + 1:
                         raise IncorrectHeadings('Heading "%(name)s" is level H%(foundnum)d,'
                                                 ' but it should be level H%(rightnum)d or less' %
                                                 dict(name=name,foundnum=level,rightnum=last_heading_num + 1))