def indent(self, xml): output = '' pos = 0 m = None e = None newline = self.beforeString if self.indentString == '': newline = '' while True: m = self.getFirstMatch(xml, pos) if m: match = self.trimExp.sub('', m.group(0)) pre = self.trimExp.sub('', xml[pos:m.start()]) pos = m.end() if m.re == self.openExp: output = join(newline, output + pre, (self.indentString * self.depth) + self.openExpSpace.sub(' ', match)) self.depth += 1 self.add = True elif m.re == self.closeExp: if self.depth == self.prevDepth and self.add: #close on new line output += pre + match self.depth -= 1 else: self.depth -= 1 output = join(newline, output + pre, (self.indentString * self.depth) + match) self.add = False elif m.re == comment_exp: if self.removeComments: continue output = join( newline, output + pre, format_comment(m.group(0), self.indentString * self.depth)) self.add = False else: output = join(newline, output + pre, (self.indentString * self.depth) + match) self.add = False self.prevDepth = self.depth else: output += xml[pos:] break if self.removeEmptyLines: blank = re.compile(r'^\s*$\r?\n', re.M) return blank.sub('', output) return output
def render_comments(self, comments, indent): output = '' for comment in comments: if output == '': output = join(' ', output, self.render_comment(comment, indent)) else: output = join('\n', output, self.render_comment(comment, indent + 1)) return output
def indent(self, xml): output = '' pos = 0 m = None e = None newline = self.beforeString if self.indentString == '': newline = '' while True: m = self.getFirstMatch(xml, pos) if m: match = self.trimExp.sub('', m.group(0)) pre = self.trimExp.sub('', xml[pos:m.start()]) pos = m.end() if m.re == self.openExp: output = join(newline, output + pre, (self.indentString * self.depth) + self.openExpSpace.sub(' ', match)) self.depth += 1 self.add = True elif m.re == self.closeExp: if self.depth == self.prevDepth and self.add: #close on new line output += pre + match self.depth -= 1 else: self.depth -= 1 output = join(newline, output + pre, (self.indentString * self.depth) + match) self.add = False elif m.re == comment_exp: if self.removeComments: continue output = join(newline, output + pre, format_comment(m.group(0), self.indentString * self.depth)) self.add = False else: output = join(newline, output + pre, (self.indentString * self.depth) + match) self.add = False self.prevDepth = self.depth else: output += xml[pos:] break if self.removeEmptyLines: blank = re.compile(r'^\s*$\r?\n', re.M) return blank.sub('', output) return output
def render_document(self, doc, indent): output = '' for item in doc.children: if isinstance(item, Comment): output = strip_trailing(output, ' ') + self.render_comment(item, indent) elif isinstance(item, Property): output += '\n' + indent * self._options['indent_character'] if re.compile('[^\\w]').search(item.name.value): output += wrap_quotes(item.name.value, self._options['quote_char']) + ': ' else: output += item.name.value + ': ' if len(item.name.comments): output = join(' ', output, self.render_comments(item.name.comments, indent + 1)) output += '\n' + (indent + 1) * self._options['indent_character'] output += self.render_value(item.value, indent + 1) elif isinstance(item.value, Value): if isinstance(item.value.value, Document): output = strip_trailing(output, ' ') output += self.render_value(item.value, indent) output = strip_trailing(output, ' ') elif isinstance(item, Value): output += '\n' + indent * self._options['indent_character'] + '- ' if isinstance(item.value, Document): output = strip_trailing(output, ' ') output += self.render_value(item, indent) return output
def render_document(self, doc, indent): output = '' for item in doc.children: if isinstance(item, Comment): output = strip_trailing(output, ' ') + self.render_comment( item, indent) elif isinstance(item, Property): output += '\n' + indent * self._options['indent_character'] if re.compile('[^\\w]').search(item.name.value): output += wrap_quotes(item.name.value, self._options['quote_char']) + ': ' else: output += item.name.value + ': ' if len(item.name.comments): output = join( ' ', output, self.render_comments(item.name.comments, indent + 1)) output += '\n' + (indent + 1) * self._options['indent_character'] output += self.render_value(item.value, indent + 1) elif isinstance(item.value, Value): if isinstance(item.value.value, Document): output = strip_trailing(output, ' ') output += self.render_value(item.value, indent) output = strip_trailing(output, ' ') elif isinstance(item, Value): output += '\n' + indent * self._options[ 'indent_character'] + '- ' if isinstance(item.value, Document): output = strip_trailing(output, ' ') output += self.render_value(item, indent) return output
def render_document(self, doc, indent): start = '' end = '' output = '' if isinstance(doc, Collection): start += '[' else: start += '{' for item in doc.children: if isinstance(item, Comment): output = strip_trailing(output, ' ') + self.render_comment( item, indent) elif isinstance(item, Property): output += '\n' + (indent + 1) * self._options['indent_character'] output += wrap_quotes(item.name.value, self._options['quote_char']) + ': ' if len(item.name.comments): output = join( ' ', output, self.render_comments(item.name.comments, indent)) output += '\n' + (indent + 2) * self._options['indent_character'] output += self.render_value(item.value, indent + 1) else: output = join(' ', output, self.render_value(item.value, indent)) output += ',' elif isinstance(item, Value): output = join(' ', output, self.render_value(item, indent)) output += ',' output = re.compile(', ?$').sub('', output) if isinstance(doc, Collection): end += ']' else: if len(doc.children): end += '\n' end += indent * self._options['indent_character'] end += '}' return start + output + end
def render_document(self, doc, indent): start = '' end = '' output = '' if isinstance(doc, Collection): start += '[' else: start += '{' for item in doc.children: if isinstance(item, Comment): output = strip_trailing(output, ' ') + self.render_comment(item, indent) elif isinstance(item, Property): output += '\n' + (indent + 1) * self._options['indent_character'] output += wrap_quotes(item.name.value, self._options['quote_char']) + ': ' if len(item.name.comments): output = join(' ', output, self.render_comments(item.name.comments, indent)) output += '\n' + (indent + 2) * self._options['indent_character'] output += self.render_value(item.value, indent + 1) else: output = join(' ', output, self.render_value(item.value, indent)) output += ',' elif isinstance(item, Value): output = join(' ', output, self.render_value(item, indent)) output += ',' output = re.compile(', ?$').sub('', output) if isinstance(doc, Collection): end += ']' else: if len(doc.children): end += '\n' end += indent * self._options['indent_character'] end += '}' return start + output + end
def render_value(self, val, indent): if val.value_type == 'string': output = val.value elif val.value_type == 'object': output = self.render_document(val.value, indent + 1) elif val.value_type == 'array': output = self.render_document(val.value, indent + 1) else: output = val.value if len(val.comments): output = join(' ', output, self.render_comments(val.comments, indent + 1)) return output
def format_block_comment(comment, indent): def indenter(match): if len(match.group(1)) == 0: return '\n' + indent elif match.group(1)[0] == '*': return '\n' + indent + ' ' + match.group(1) else: return '\n' + indent + ' ' + match.group(1) match = block_comment_exp.search(comment) if match: output = join(' ', match.group(1), preserve_indent_exp.sub(indenter, match.group(2)), match.group(3)) return output return comment
def test_should_join_multiple_strings_preserving_first_string_stripping_others(self): output = join("/", "/1/", "/2/", "/3") expect(output).to_equal("/1/2/3")
def test_should_join_multiple_strings_with_regex_special_character(self): output = join("\\", "1", "2", "3") expect(output).to_equal("1\\2\\3")
def test_should_join_multiple_strings_with_spaces(self): output = join(" ", "1", "2", "3") expect(output).to_equal("1 2 3")
def format(self): output = '' indent = 0 reader = self._reader has_properties = False is_last_token_comment = False spacer = self._options['spacer'] newline = self._options['newline'] remove_comments = self._options['remove_comments'] while True: should_linebreak = False result = reader.read() is_comment = False if not result: break if is_last_token_comment: should_linebreak = True is_last_token_comment = False if result.type == 'begin_object': has_properties = False indent += 1 elif result.type == 'end_object': indent -= 1 if has_properties: should_linebreak = True elif result.type == 'property': should_linebreak = True has_properties = True elif result.type == 'new_line_comment': is_comment = True is_last_token_comment = True should_linebreak = True elif result.type == 'end_line_comment': is_comment = True is_last_token_comment = True elif result.type == 'new_line_comment_block': is_comment = True should_linebreak = True elif result.type == 'in_line_comment_block': is_comment = True if is_comment and remove_comments: continue # new line if should_linebreak: output = join(newline, strip_trailing(output, spacer), indent * self._options['indent_character']) # actual character if result.type == 'property' and 'force_property_quotes' in self._options and self._options[ 'force_property_quotes']: output += ensure_quotes(result.value, self._options['quote_char']) elif result.type == 'value' and 'normalize_strings' in self._options and self._options[ 'normalize_strings'] and is_string_value(result.value): output += ensure_quotes(result.value, self._options['quote_char']) elif result.type == 'end_line_comment': output = strip_trailing(output, spacer) output += spacer + result.value elif result.type == 'new_line_comment_block': output += format_block_comment( result.value, indent * self._options['indent_character']) + newline elif result.type == 'in_line_comment_block': output = strip_trailing(output, spacer) output += spacer + format_block_comment( result.value, indent * self._options['indent_character']) else: output += result.value # suffix if result.type == 'property_separator': output += spacer elif result.type == 'value_separator': output += spacer elif result.type == 'in_line_comment_block': output += spacer return output
def test_should_join_whitespace_to_json_with_new_line(self): output = join('\n', '{\n "hello": "world"', ' ') expect(output).to_equal('{\n "hello": "world"\n ')
def test_should_join_whitespace_to_json_with_new_line(self): output = join("\n", '{\n "hello": "world"', " ") expect(output).to_equal('{\n "hello": "world"\n ')
def test_should_join_multiple_strings_with_regex_special_character(self): output = join('\\', '1', '2', '3') expect(output).to_equal('1\\2\\3')
def test_should_join_multiple_strings_preserving_first_string_stripping_others( self): output = join('/', '/1/', '/2/', '/3') expect(output).to_equal('/1/2/3')
def test_should_join_multiple_strings_with_new_lines(self): output = join('\n', '', '1', '2', '3') expect(output).to_equal('1\n2\n3')
def format(self): output = '' indent = 0 reader = self._reader has_properties = False is_last_token_comment = False spacer = self._options['spacer'] newline = self._options['newline'] remove_comments = self._options['remove_comments'] while True: should_linebreak = False result = reader.read() is_comment = False if not result: break if is_last_token_comment: should_linebreak = True is_last_token_comment = False if result.type == 'begin_object': has_properties = False indent += 1 elif result.type == 'end_object': indent -= 1 if has_properties: should_linebreak = True elif result.type == 'property': should_linebreak = True has_properties = True elif result.type == 'new_line_comment': is_comment = True is_last_token_comment = True should_linebreak = True elif result.type == 'end_line_comment': is_comment = True is_last_token_comment = True elif result.type == 'new_line_comment_block': is_comment = True should_linebreak = True elif result.type == 'in_line_comment_block': is_comment = True if is_comment and remove_comments: continue # new line if should_linebreak: output = join(newline, strip_trailing(output, spacer), indent * self._options['indent_character']) # actual character if result.type == 'property' and 'force_property_quotes' in self._options and self._options['force_property_quotes']: output += ensure_quotes(result.value, self._options['quote_char']) elif result.type == 'value' and 'normalize_strings' in self._options and self._options['normalize_strings'] and is_string_value(result.value): output += ensure_quotes(result.value, self._options['quote_char']) elif result.type == 'end_line_comment': output = strip_trailing(output, spacer) output += spacer + result.value elif result.type == 'new_line_comment_block': output += format_block_comment(result.value, indent * self._options['indent_character']) + newline elif result.type == 'in_line_comment_block': output = strip_trailing(output, spacer) output += spacer + format_block_comment(result.value, indent * self._options['indent_character']) else: output += result.value # suffix if result.type == 'property_separator': output += spacer elif result.type == 'value_separator': output += spacer elif result.type == 'in_line_comment_block': output += spacer return output
def test_should_join_multiple_strings_with_new_lines(self): output = join("\n", "", "1", "2", "3") expect(output).to_equal("1\n2\n3")
def test_should_join_end_object_to_json_with_new_line(self): output = join("\n", '{\n "hello": "world"', "}") expect(output).to_equal('{\n "hello": "world"\n}')
def test_should_join_multiple_strings_with_spaces(self): output = join(' ', '1', '2', '3') expect(output).to_equal('1 2 3')
def create_issue_url(command_name): return join('?', issue_url, urlencode({'body': issue_template % command_name}))
def test_should_join_end_object_to_json_with_new_line(self): output = join('\n', '{\n "hello": "world"', '}') expect(output).to_equal('{\n "hello": "world"\n}')