Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct inconsistent indentation of YAML #7459

Merged
merged 2 commits into from
Oct 26, 2020
Merged

Conversation

AvdN
Copy link
Contributor

@AvdN AvdN commented Sep 5, 2020

The YAML examples sometimes use two, sometimes use three spaces to indent block mapping values that are collections
(see the examples for https://docs.readthedocs.io/en/stable/config-file/v2.html#build resp. https://docs.readthedocs.io/en/stable/config-file/v2.html#requirements-file )

For sequences that are mapping values (and need as a miminum, an indent of two with an offset of zero, i.e. left aligned with the key) things are even more diverse:

This PR consistently formats mappings with an indent of two and sequences with an indent of four and offset of two.

I can make another PR with other indent schemes, this update was programmatically done using:

import sys
from pathlib import Path
import ruamel.yaml

yaml = ruamel.yaml.YAML()
yaml.indent(mapping=2, sequence=4, offset=2)

in_path = Path('v2.rst.org')
out_path = Path('v2.rst')
code_indent = 3  # YAML relative to '.. code-block:: yaml'
pattern = '.. code-block:: yaml'
pattern_indent = None
yaml_str = ""

class ReAddIndent:
    def __init__(self, indent):
        self.indent = ' ' * indent

    def __call__(self, s):
        # indent all non-empty lines
        return '\n' + '\n'.join([(self.indent + line if line else line) for line in s.splitlines()]) + '\n'


with out_path.open('w') as out:
    for line_nl in in_path.open():
        line = line_nl.rstrip('\n')
        if pattern_indent is not None:
            leading = line[0:pattern_indent + code_indent].strip()
            #if not yaml_str and not leading: # leading empty line(s)
            #    print(line, file=out)
            #    continue
            if leading != '':  # end of YAML block
                yaml.dump(yaml.load(yaml_str), out, transform=ReAddIndent(pattern_indent+code_indent))
                pattern_indent = None
                print(line, file=out)
                continue
            yaml_str += line[pattern_indent+code_indent:] + '\n'
            continue
        elif (ind := line.find(pattern)) >= 0:
            yaml_str = ""
            pattern_indent = ind
        print(line, file=out)

The YAML examples sometimes use two, sometimes use three spaces to indent block mapping values that are collections
(see the examples for https://docs.readthedocs.io/en/stable/config-file/v2.html#build resp.  https://docs.readthedocs.io/en/stable/config-file/v2.html#requirements-file )

For sequences that are mapping values (and need as a miminum, an indent of two with an offset of zero, i.e. left aligned with the key) is even more diverse:
- the first YAML example has an offset of two in an indent of four.
- the second example for **formats** ( https://docs.readthedocs.io/en/stable/config-file/v2.html#formats ), has an offset of three in an indent of five
- the value for `extra_requirements for **python** ( https://docs.readthedocs.io/en/stable/config-file/v2.html#python ) has an offset of four in an indent of six

This PR consistently formats mappings with an indent of two and sequences with an indent of four and offset of two.

I can make another PR with other indent schemes, this update was programmatically done using:

```
import sys
from pathlib import Path
import ruamel.yaml

yaml = ruamel.yaml.YAML()
yaml.indent(mapping=2, sequence=4, offset=2)

in_path = Path('v2.rst.org')
out_path = Path('v2.rst')
code_indent = 3  # YAML relative to '.. code-block:: yaml'
pattern = '.. code-block:: yaml'
pattern_indent = None
yaml_str = ""

class ReAddIndent:
    def __init__(self, indent):
        self.indent = ' ' * indent

    def __call__(self, s):
        # indent all non-empty lines
        return '\n' + '\n'.join([(self.indent + line if line else line) for line in s.splitlines()]) + '\n'


with out_path.open('w') as out:
    for line_nl in in_path.open():
        line = line_nl.rstrip('\n')
        if pattern_indent is not None:
            leading = line[0:pattern_indent + code_indent].strip()
            #if not yaml_str and not leading: # leading empty line(s)
            #    print(line, file=out)
            #    continue
            if leading != '':  # end of YAML block
                yaml.dump(yaml.load(yaml_str), out, transform=ReAddIndent(pattern_indent+code_indent))
                pattern_indent = None
                print(line, file=out)
                continue
            yaml_str += line[pattern_indent+code_indent:] + '\n'
            continue
        elif (ind := line.find(pattern)) >= 0:
            yaml_str = ""
            pattern_indent = ind
        print(line, file=out)
```
Copy link
Member

@stsewd stsewd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks like you need to resolve a conflict, so we can merge this PR

@stale
Copy link

stale bot commented Oct 24, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the Status: stale Issue will be considered inactive soon label Oct 24, 2020
@stsewd stsewd removed the Status: stale Issue will be considered inactive soon label Oct 26, 2020
@stsewd stsewd merged commit 7a0ac17 into readthedocs:master Oct 26, 2020
@AvdN AvdN deleted the patch-2 branch October 27, 2020 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants