def unindent(self, text): if text: match = first_char_exp.search(text) if match: first_char = match.group(1) if first_char == '<': formatter = XmlIndentFormatter('', True) return formatter.indent(text) else: formatter = JsonFormatter(JsonReader(text), {'remove_comments': True, 'spacer': '', 'newline': '', 'indent_character': ''}) return formatter.format()
def format(self, text, indent_char = '\t'): if text: match = first_char_exp.search(text) if match: first_char = match.group(1) if first_char == '<': formatter = XmlIndentFormatter(indent_char) return formatter.indent(text) else: formatter = JsonFormatter(JsonReader(text), {'indent_character': indent_char}) return formatter.format()
def format(self, text, indent_char='\t'): if text: match = first_char_exp.search(text) if match: first_char = match.group(1) if first_char == '<': formatter = XmlIndentFormatter(indent_char) return formatter.indent(text) else: formatter = JsonFormatter( JsonReader(text), {'indent_character': indent_char}) return formatter.format()
def unindent(self, text): if text: match = first_char_exp.search(text) if match: first_char = match.group(1) if first_char == '<': formatter = XmlIndentFormatter('', True) return formatter.indent(text) else: formatter = JsonFormatter( JsonReader(text), { 'remove_comments': True, 'spacer': '', 'newline': '', 'indent_character': '' }) return formatter.format()
def test_should_format_strip_comments_and_white_pace(self): formatter = XmlIndentFormatter('', True, True) formattedText = formatter.indent('<root>\n\n\n\n <!-- comment -->\n <child a="1"\nb="2"></child>\n</root>') expect(formattedText).to_equal('<root><child a="1" b="2"></child></root>')
def test_should_format_xml_removing_white_space(self): formatter = XmlIndentFormatter(' ', False, True) formattedText = formatter.indent('<root>\n\n\n<!--comment--><child></child></root>') expect(formattedText).to_equal('<root>\n <!--comment-->\n <child></child>\n</root>')
def test_should_format_malformed_xml(self): formatter = XmlIndentFormatter(' ') formattedText = formatter.indent('<root><child><child with="this attribute" and="that attribute" /></root>') expect(formattedText).to_equal('<root>\n <child>\n <child with="this attribute" and="that attribute" />\n </root>')
def test_should_format_xml_with_comment(self): formatter = XmlIndentFormatter(' ') formattedText = formatter.indent('<root><!--comment--><child></child></root>') expect(formattedText).to_equal('<root>\n <!--comment-->\n <child></child>\n</root>')