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

Default to literal style for multiline strings #822

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions lib/yaml/emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ScalarAnalysis:
def __init__(self, scalar, empty, multiline,
allow_flow_plain, allow_block_plain,
allow_single_quoted, allow_double_quoted,
allow_block):
allow_block, maximum_line_length):
self.scalar = scalar
self.empty = empty
self.multiline = multiline
Expand All @@ -27,6 +27,7 @@ def __init__(self, scalar, empty, multiline,
self.allow_single_quoted = allow_single_quoted
self.allow_double_quoted = allow_double_quoted
self.allow_block = allow_block
self.maximum_line_length = maximum_line_length

class Emitter:

Expand Down Expand Up @@ -506,6 +507,11 @@ def choose_scalar_style(self):
if (not self.flow_level and not self.simple_key_context
and self.analysis.allow_block):
return self.event.style
if (not self.event.style and not self.flow_level and
self.analysis.allow_block and self.analysis.multiline):
allowed_length = self.best_width - self.column;
if self.analysis.maximum_line_length < allowed_length:
return '|'
if not self.event.style or self.event.style == '\'':
if (self.analysis.allow_single_quoted and
not (self.simple_key_context and self.analysis.multiline)):
Expand Down Expand Up @@ -630,7 +636,7 @@ def analyze_scalar(self, scalar):
return ScalarAnalysis(scalar=scalar, empty=True, multiline=False,
allow_flow_plain=False, allow_block_plain=True,
allow_single_quoted=True, allow_double_quoted=True,
allow_block=False)
allow_block=False, maximum_line_length=0)

# Indicators and special characters.
block_indicators = False
Expand Down Expand Up @@ -665,6 +671,9 @@ def analyze_scalar(self, scalar):
previous_break = False

index = 0
line_length = 0
maximum_line_length = 0
only_linebreaks = 1
while index < len(scalar):
ch = scalar[index]

Expand Down Expand Up @@ -696,6 +705,11 @@ def analyze_scalar(self, scalar):
# Check for line breaks, special, and unicode characters.
if ch in '\n\x85\u2028\u2029':
line_breaks = True
maximum_line_length = max(line_length, maximum_line_length)
line_length = 0
else:
only_linebreaks = 0
line_length += 1
if not (ch == '\n' or '\x20' <= ch <= '\x7E'):
if (ch == '\x85' or '\xA0' <= ch <= '\uD7FF'
or '\uE000' <= ch <= '\uFFFD'
Expand Down Expand Up @@ -748,8 +762,10 @@ def analyze_scalar(self, scalar):
allow_flow_plain = allow_block_plain = False

# We do not permit trailing spaces for block scalars.
if trailing_space:
if trailing_space or only_linebreaks:
allow_block = False
if only_linebreaks:
allow_single_quoted = False

# Spaces at the beginning of a new line are only acceptable for block
# scalars.
Expand Down Expand Up @@ -781,7 +797,8 @@ def analyze_scalar(self, scalar):
allow_block_plain=allow_block_plain,
allow_single_quoted=allow_single_quoted,
allow_double_quoted=allow_double_quoted,
allow_block=allow_block)
allow_block=allow_block,
maximum_line_length=maximum_line_length)

# Writers.

Expand Down
Loading