Commits

Geoffrey Sneddon committed dcbc622

Indenting code is hard. Make the TOC indented.

Comments (0)

Files changed (1)

specGen/processes/toc.py

 							# If the final li has no children, or the last children isn't an ol element
 							if len(toc_section[-1]) == 0 or toc_section[-1][-1].tag != "ol":
 								toc_section[-1].append(etree.Element("ol"))
+								self.indentNode(toc_section[-1][-1], (i + 1) * 2)
 								if w3c_compat or w3c_compat_class_toc:
 									toc_section[-1][-1].set("class", "toc")
 						except IndexError:
 							# If the current ol has no li in it
 							toc_section.append(etree.Element("li"))
+							self.indentNode(toc_section[0], (i + 1) * 2 - 1)
 							toc_section[0].append(etree.Element("ol"))
+							self.indentNode(toc_section[0][0], (i + 1) * 2)
 							if w3c_compat or w3c_compat_class_toc:
-								toc_section[0][-1].set("class", "toc")
+								toc_section[0][0].set("class", "toc")
 						# TOC Section is now the final child (ol) of the final item (li) in the previous section
 						assert toc_section[-1].tag == "li"
 						assert toc_section[-1][-1].tag == "ol"
 					# Add the current item to the TOC
 					item = etree.Element("li")
 					toc_section.append(item)
+					self.indentNode(item, (i + 1) * 2 - 1)
 					
 				# If we have a header
 				if section.header is not None:
 					in_toc = True
 					node.tail = None
 					node.addnext(deepcopy(self.toc))
+					self.indentNode(node.getnext(), 0)
 				elif node.text.strip(utils.spaceCharacters) == "toc":
 					node.addprevious(etree.Comment("begin-toc"))
+					self.indentNode(node.getprevious(), 0)
 					node.addprevious(deepcopy(self.toc))
+					self.indentNode(node.getprevious(), 0)
 					node.addprevious(etree.Comment("end-toc"))
+					self.indentNode(node.getprevious(), 0)
 					to_remove.add(node)
 		for node in to_remove:
 			node.getparent().remove(node)
+	
+	def indentNode(self, node, indent=0, **kwargs):
+		whitespace = "\n" + "\t" * indent
+		if node.getprevious() is not None:
+			if node.getprevious().tail is None:
+				node.getprevious().tail = whitespace
+			else:
+				node.getprevious().tail += whitespace
+		else:
+			if node.getparent().text is None:
+				node.getparent().text = whitespace
+			else:
+				node.getparent().text += whitespace
 
 class DifferentParentException(utils.SpecGenException):
 	"""begin-toc and end-toc do not have the same parent."""