Commits

Geoffrey Sneddon committed 16b714f

Make newline/indent characters customizable.

  • Participants
  • Parent commits dcbc622

Comments (0)

Files changed (2)

 #!/usr/bin/env python
-"""usage: %prog [options] input output
+"""usage: spec-gen [options] input output
 
 Post-process a document, adding cross-references, table of contents, etc.
 """
 	parser.add_option("", "--lxml.html", action="store_true",
 		default=False, dest="lxml_html", help="Use lxml's HTML parser/serializer.")
 	
+	parser.add_option("", "--newline-char", action="store", type="string",
+		default="\n", dest="newline_char", help="Set the newline character/string used when creating new newlines. This should match the rest of the newlines in the document.")
+	
+	parser.add_option("", "--indent-char", action="store", type="string",
+		default="\t", dest="indent_char", help="Set the character/string used when creating indenting new blocks of (X)HTML. This should match the rest of the indentation in the document.")
+	
+	parser.add_option("", "--force-html4-id", action="store_true",
+		default=False, dest="force_html4_id", help="Force the ID generation algorithm to create HTML 4 compliant IDs regardless of the DOCTYPE.")
+	
 	parser.add_option("", "--min-depth", action="store", type="int",
 		default=2, dest="min_depth", help="Highest ranking header to number/insert into TOC.")
 	
 	parser.add_option("", "--allow-duplicate-terms", action="store_true",
 		default=False, dest="allow_duplicate_terms", help="Allow multiple definitions of terms when cross-referencing (the last instance of the term is used when referencing it).")
 	
-	parser.add_option("", "--force-html4-id", action="store_true",
-		default=False, dest="force_html4_id", help="Force the ID generation algorithm to create HTML 4 compliant IDs regardless of the DOCTYPE.")
-	
 	parser.add_option("", "--w3c-compat", action="store_true",
 		default=False, dest="w3c_compat", help="Behave in a (mostly) compatible way to the W3C CSS WG's Postprocessor (this implies all of the other --w3c-compat options with the exception of --w3c-compat-crazy-substitution, as that is too crazy).")
 	

File 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)
+								self.indentNode(toc_section[-1][-1], (i + 1) * 2, **kwargs)
 								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)
+							self.indentNode(toc_section[0], (i + 1) * 2 - 1, **kwargs)
 							toc_section[0].append(etree.Element("ol"))
-							self.indentNode(toc_section[0][0], (i + 1) * 2)
+							self.indentNode(toc_section[0][0], (i + 1) * 2, **kwargs)
 							if w3c_compat or w3c_compat_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
 					# Add the current item to the TOC
 					item = etree.Element("li")
 					toc_section.append(item)
-					self.indentNode(item, (i + 1) * 2 - 1)
+					self.indentNode(item, (i + 1) * 2 - 1, **kwargs)
 					
 				# 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)
+					self.indentNode(node.getnext(), 0, **kwargs)
 				elif node.text.strip(utils.spaceCharacters) == "toc":
 					node.addprevious(etree.Comment("begin-toc"))
-					self.indentNode(node.getprevious(), 0)
+					self.indentNode(node.getprevious(), 0, **kwargs)
 					node.addprevious(deepcopy(self.toc))
-					self.indentNode(node.getprevious(), 0)
+					self.indentNode(node.getprevious(), 0, **kwargs)
 					node.addprevious(etree.Comment("end-toc"))
-					self.indentNode(node.getprevious(), 0)
+					self.indentNode(node.getprevious(), 0, **kwargs)
 					to_remove.add(node)
 		for node in to_remove:
 			node.getparent().remove(node)
 	
-	def indentNode(self, node, indent=0, **kwargs):
-		whitespace = "\n" + "\t" * indent
+	def indentNode(self, node, indent=0, newline_char="\n", indent_char="\t", **kwargs):
+		whitespace = newline_char + indent_char * indent
 		if node.getprevious() is not None:
 			if node.getprevious().tail is None:
 				node.getprevious().tail = whitespace