Пример #1
0
    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
Пример #2
0
    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
Пример #3
0
    def render_value(self, val, indent):
        output = ''

        if val.value_type == 'string':
            output = wrap_quotes(val.value, self._options['quote_char'])
        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)
        else:
            output = val.value

        return output
Пример #4
0
    def render_value(self, val, indent):
        output = ''

        if val.value_type == 'string':
            output = wrap_quotes(val.value, self._options['quote_char'])
        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)
        else:
            output = val.value

        return output
Пример #5
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
Пример #6
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
Пример #7
0
 def test_should_wrap_quoted_string_in_quotes(self):
     output = wrap_quotes('"hello"')
     expect(output).to_equal('"\\"hello\\""')
Пример #8
0
 def test_should_wrap_basic_string_in_quotes(self):
     output = wrap_quotes("hello")
     expect(output).to_equal('"hello"')
Пример #9
0
 def test_should_wrap_quoted_string_in_quotes(self):
     output = wrap_quotes('"hello"')
     expect(output).to_equal('"\\"hello\\""')
Пример #10
0
 def test_should_wrap_basic_string_in_quotes(self):
     output = wrap_quotes('hello')
     expect(output).to_equal('"hello"')