diff options
-rw-r--r-- | CHANGES | 3 | ||||
-rw-r--r-- | README.rst | 3 | ||||
-rw-r--r-- | __init__.py | 4 | ||||
-rw-r--r-- | _doc/detail.ryd | 21 | ||||
-rw-r--r-- | comments.py | 10 | ||||
-rw-r--r-- | emitter.py | 18 | ||||
-rw-r--r-- | tokens.py | 4 |
7 files changed, 50 insertions, 13 deletions
@@ -1,3 +1,6 @@ +[0, 15, 31]: 2017-08-15 + - fix Comment dumping + [0, 15, 30]: 2017-08-14 - fix for issue with "compact JSON" not parsing: ``{"in":{},"out":{}}`` (reported on `StackOverflow <https://stackoverflow.com/q/45681626/1307905>`_ by @@ -35,6 +35,9 @@ ChangeLog .. should insert NEXT: at the beginning of line for next key +0.15.31 (2017-08-15): + - fix Comment dumping + 0.15.30 (2017-08-14): - fix for issue with "compact JSON" not parsing: ``{"in":{},"out":{}}`` (reported on `StackOverflow <https://stackoverflow.com/q/45681626/1307905>`_ by diff --git a/__init__.py b/__init__.py index 8fd81f8..7f40acf 100644 --- a/__init__.py +++ b/__init__.py @@ -7,8 +7,8 @@ if False: # MYPY _package_data = dict( full_package_name='ruamel.yaml', - version_info=(0, 15, 30), - __version__='0.15.30', + version_info=(0, 15, 31), + __version__='0.15.31', author='Anthon van der Neut', author_email='a.van.der.neut@ruamel.eu', description='ruamel.yaml is a YAML parser/emitter that supports roundtrip preservation of comments, seq/map flow style, and map key order', # NOQA diff --git a/_doc/detail.ryd b/_doc/detail.ryd index 7867169..ea976e1 100644 --- a/_doc/detail.ryd +++ b/_doc/detail.ryd @@ -92,6 +92,27 @@ intuitive results. **but this is not enforced**. Depending on your structure, not following this advice **might lead to invalid output**. +Inconsistently indented YAML +++++++++++++++++++++++++++++ + +If your input is inconsistently indented, such indentation cannot be preserverd. +The first round-trip will make it consistent/normalize it. Here are some +inconsistently indented YAML examples. + +``b`` indented 3, ``c`` indented 4 positions:: + + a: + b: + c: 1 + +Top level sequence is indented 2 without offset, the other sequence 4 (with offset 2):: + + - key: + - foo + - bar + + + Positioning ':' in top level mappings, prefixing ':' ==================================================== diff --git a/comments.py b/comments.py index 00f57de..d77b981 100644 --- a/comments.py +++ b/comments.py @@ -250,13 +250,15 @@ class CommentedBase(object): if after_indent is None: after_indent = indent + 2 - if before and before[-1] == '\n': + if before and (len(before) > 1) and before[-1] == '\n': before = before[:-1] # strip final newline if there if after and after[-1] == '\n': after = after[:-1] # strip final newline if there start_mark = CommentMark(indent) c = self.ca.items.setdefault(key, [None, [], None, None]) - if before: + if before == '\n': + c[1].append(comment_token('', start_mark)) + elif before: for com in before.split('\n'): c[1].append(comment_token(com, start_mark)) if after: @@ -947,13 +949,13 @@ def dump_comments(d, name='', sep='.', out=sys.stdout): recurisively dump domments all but the toplevel preceded by the path in dotted form x.0.a """ - if isinstance(d, dict): + if isinstance(d, dict) and hasattr(d, 'ca'): if name: print(name) print(d.ca, file=out) # type: ignore for k in d: dump_comments(d[k], name=(name + sep + k) if name else k, sep=sep, out=out) - elif isinstance(d, list): + elif isinstance(d, list) and hasattr(d, 'ca'): if name: print(name) print(d.ca, file=out) # type: ignore @@ -74,6 +74,10 @@ class Indents(object): # -1 for the dash return self.values[-1][0] + seq_indent - column - 1 + def __len__(self): + # type: () -> int + return len(self.values) + class Emitter(object): DEFAULT_TAG_PREFIXES = { @@ -255,9 +259,13 @@ class Emitter(object): elif not indentless: self.indent += (self.best_sequence_indent if self.indents.last_seq() else self.best_map_indent) - # if ()self.sequence_context and (self.sequence_dash_offset + 2) > - # self.best_sequence_indent): - # self.indent = self.sequence_dash_offset + 2 + # if self.indents.last_seq(): + # if self.indent == 0: # top level block sequence + # self.indent = self.best_sequence_indent - self.sequence_dash_offset + # else: + # self.indent += self.best_sequence_indent + # else: + # self.indent += self.best_map_indent # States. @@ -552,8 +560,8 @@ class Emitter(object): self.write_pre_comment(self.event) nonl = self.no_newline if self.column == 0 else False self.write_indent() - self.write_indicator((u' ' * self.sequence_dash_offset) + u'-', True, - indention=True) + ind = self.sequence_dash_offset # if len(self.indents) > 1 else 0 + self.write_indicator(u' ' * ind + u'-', True, indention=True) if nonl or self.sequence_dash_offset + 2 > self.best_sequence_indent: self.no_newline = True self.states.append(self.expect_block_sequence_item) @@ -255,10 +255,10 @@ class CommentToken(Token): def __repr__(self): # type: () -> Any - v = self.value + v = u'{!r}'.format(self.value) if SHOWLINES: try: v += u', line: ' + str(self.start_mark.line) except: pass - return 'CommentToken({!r})'.format(v) + return 'CommentToken({})'.format(v) |