Skip to content

Commit 9c386e7

Browse files
Ken KundertKen Kundert
authored andcommitted
resolve some nit regarding line terminators
1 parent 98be487 commit 9c386e7

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

doc/file_format.rst

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ The *NestedText* format follows a small number of simple rules. Here they are.
7373
item*. After comments and blank lines have been removed, adjacent string
7474
items with the same indentation level are combined in order into
7575
a multiline string. The string value is the multiline string with the
76-
tags removed. Any leading white space that follows the tag is retained, as
77-
is any trailing white space and all newlines except the last.
76+
tags removed. Any leading white space that follows the tag is retained, as
77+
is any trailing white space. The last newline is removed and all other
78+
newlines are converted to the default line terminator for the current
79+
operating system.
7880

7981
String values may contain any printing UTF-8 character.
8082

@@ -105,9 +107,10 @@ The *NestedText* format follows a small number of simple rules. Here they are.
105107
immediately by an ASCII space (``:␣``) or a line break, the line is a *key
106108
item*. After comments and blank lines have been removed, adjacent key items
107109
with the same indentation level are combined in order into a multiline key.
108-
The key itself is the multiline string with the tags removed. Any leading
109-
white space that follows the tag is retained, as is any trailing white space
110-
and all newlines except the last.
110+
The key itself is the multiline string with the tags removed. Any leading
111+
white space that follows the tag is retained, as is any trailing white
112+
space. The last newline is removed and all other newlines are converted to
113+
the default line terminator for the current operating system.
111114

112115
Key values may contain any printing UTF-8 character.
113116

doc/techniques.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,9 @@ This example uses the pretty-print function from the previous example.
547547
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
548548
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
549549
550+
An alternative to using a backslash to escape the newline is to simply join
551+
lines that end with a space. This might be more natural for non-programmers and
552+
can work well for prose.
550553

551554
.. _voluptuous: https://github.com/alecthomas/voluptuous
552555
.. _pydantic: https://pydantic-docs.helpmanual.io

nestedtext/nestedtext.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252

5353

5454
# Utility functions {{{1
55-
# convert_returns {{{2
56-
def convert_returns(text):
55+
# convert_line_terminators {{{2
56+
def convert_line_terminators(text):
5757
return text.replace("\r\n", "\n").replace("\r", "\n")
5858

5959

@@ -1320,7 +1320,7 @@ def loads(
13201320
'''
13211321

13221322
# code {{{3
1323-
lines = convert_returns(content).split("\n")
1323+
lines = convert_line_terminators(content).split("\n")
13241324
loader = NestedTextLoader(
13251325
lines, top, source, on_dup, keymap, normalize_key, dialect
13261326
)
@@ -1407,7 +1407,7 @@ def load(
14071407
# code {{{3
14081408
# Do not invoke the read method as that would read in the entire contents of
14091409
# the file, possibly consuming a lot of memory. Instead pass the file
1410-
# pointer into _read_all(), it will iterate through the lines, discarding
1410+
# pointer into loader, it will iterate through the lines, discarding
14111411
# them once they are no longer needed, which reduces the memory usage.
14121412

14131413
if isinstance(f, collections.abc.Iterator):
@@ -1527,7 +1527,7 @@ def render_key(self, key, keys):
15271527
raise NestedTextError(
15281528
key, template="keys must be strings.", culprit=keys
15291529
) from None
1530-
return convert_returns(key)
1530+
return convert_line_terminators(key)
15311531

15321532
# render_dict_item {{{3
15331533
def render_dict_item(self, key, value, keys, values):
@@ -1546,7 +1546,7 @@ def render_dict_item(self, key, value, keys, values):
15461546
return key + self.render_value(value, keys, values)
15471547
if is_str(value):
15481548
# force use of multiline value with multiline keys
1549-
value = convert_returns(value)
1549+
value = convert_line_terminators(value)
15501550
else:
15511551
value = self.render_value(value, keys, values)
15521552
return key + "\n" + add_leader(value, self.indent*" " + "> ")
@@ -1674,7 +1674,7 @@ def render_value(self, obj, keys, values):
16741674
content = "\n".join(content)
16751675

16761676
elif self.is_a_str(obj):
1677-
text = convert_returns(obj)
1677+
text = convert_line_terminators(obj)
16781678
if "\n" in text or level == 0:
16791679
content = add_leader(text, "> ")
16801680
need_indented_block = True

0 commit comments

Comments
 (0)