Merge pull request #13 from Ede123/options
Add some options to optimize pretty-printing
This commit is contained in:
commit
27d5cd3881
1 changed files with 23 additions and 5 deletions
|
|
@ -2763,8 +2763,12 @@ def serializeXML(element, options, ind = 0, preserveWhitespace = False):
|
|||
|
||||
indent = ind
|
||||
I=''
|
||||
newline = ''
|
||||
if options.newlines:
|
||||
if options.indent_type == 'tab': I='\t'
|
||||
elif options.indent_type == 'space': I=' '
|
||||
I *= options.indent_depth
|
||||
newline = '\n'
|
||||
|
||||
outParts.extend([(I * ind), '<', element.nodeName])
|
||||
|
||||
|
|
@ -2823,7 +2827,7 @@ def serializeXML(element, options, ind = 0, preserveWhitespace = False):
|
|||
if preserveWhitespace:
|
||||
outParts.append(serializeXML(child, options, 0, preserveWhitespace))
|
||||
else:
|
||||
outParts.extend(['\n', serializeXML(child, options, indent + 1, preserveWhitespace)])
|
||||
outParts.extend([newline, serializeXML(child, options, indent + 1, preserveWhitespace)])
|
||||
onNewLine = True
|
||||
# text node
|
||||
elif child.nodeType == 3:
|
||||
|
|
@ -2845,10 +2849,10 @@ def serializeXML(element, options, ind = 0, preserveWhitespace = False):
|
|||
|
||||
if onNewLine: outParts.append(I * ind)
|
||||
outParts.extend(['</', element.nodeName, '>'])
|
||||
if indent > 0: outParts.append('\n')
|
||||
if indent > 0: outParts.append(newline)
|
||||
else:
|
||||
outParts.append('/>')
|
||||
if indent > 0: outParts.append('\n')
|
||||
if indent > 0: outParts.append(newline)
|
||||
|
||||
return "".join(outParts)
|
||||
|
||||
|
|
@ -2918,6 +2922,9 @@ def scourString(in_string, options=None):
|
|||
if options.strip_comments:
|
||||
numCommentsRemoved = removeComments(doc)
|
||||
|
||||
if options.strip_xml_space_attribute and doc.documentElement.hasAttribute('xml:space'):
|
||||
doc.documentElement.removeAttribute('xml:space')
|
||||
|
||||
# repair style (remove unnecessary style properties and change them into XML attributes)
|
||||
numStylePropsFixed = repairStyle(doc.documentElement, options)
|
||||
|
||||
|
|
@ -3164,6 +3171,15 @@ _options_parser.add_option("-q", "--quiet",
|
|||
_options_parser.add_option("--indent",
|
||||
action="store", type="string", dest="indent_type", default="space",
|
||||
help="indentation of the output: none, space, tab (default: %default)")
|
||||
_options_parser.add_option("--nindent",
|
||||
action="store", type=int, dest="indent_depth", default=1,
|
||||
help="depth of the indentation, i.e. number of spaces/tabs: (default: %default)")
|
||||
_options_parser.add_option("--no-line-breaks",
|
||||
action="store_false", dest="newlines", default=True,
|
||||
help="do not create line breaks in output (also disables indentation; might be overriden by xml:space=\"preserve\")")
|
||||
_options_parser.add_option("--strip-xml-space",
|
||||
action="store_true", dest="strip_xml_space_attribute", default=False,
|
||||
help="strip the xml:space=\"preserve\" attribute from the root SVG element")
|
||||
_options_parser.add_option("--protect-ids-noninkscape",
|
||||
action="store_true", dest="protect_ids_noninkscape", default=False,
|
||||
help="Don't change IDs not ending with a digit")
|
||||
|
|
@ -3193,6 +3209,8 @@ def parse_args(args=None, ignore_additional_args=False):
|
|||
_options_parser.error("Can't have negative significant digits, see --help")
|
||||
if not options.indent_type in ["tab", "space", "none"]:
|
||||
_options_parser.error("Invalid value for --indent, see --help")
|
||||
if options.indent_depth < 0:
|
||||
_options_parser.error("Value for --nindent should be positive (or zero), see --help")
|
||||
if options.infilename and options.outfilename and options.infilename == options.outfilename:
|
||||
_options_parser.error("Input filename is the same as output filename")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue