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
Example #3
0
    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
Example #5
0
    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
Example #6
0
    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