Skip to content

Commit

Permalink
Default to literal style for multiline strings
Browse files Browse the repository at this point in the history
  • Loading branch information
perlpunk committed Aug 10, 2024
1 parent 69c141a commit afa8411
Showing 1 changed file with 21 additions and 4 deletions.
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
line_length = 0
else:
only_linebreaks = 0
line_length += 1
maximum_line_length = max(line_length, maximum_line_length)
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

0 comments on commit afa8411

Please sign in to comment.