在YAML中,我有一个很长的字符串。我希望将其保存在编辑器的80列(或左右)视图中,因此我希望断开字符串。这是什么语法?
换句话说,我有这个:
Key: 'this is my very very very very very very long string'
我想要这个(或类似的东西):
Key: 'this is my very very very ' +
'long string'
我想使用上面的引号,所以我不需要转义字符串中的任何内容。
在YAML中,我有一个很长的字符串。我希望将其保存在编辑器的80列(或左右)视图中,因此我希望断开字符串。这是什么语法?
换句话说,我有这个:
Key: 'this is my very very very very very very long string'
我想要这个(或类似的东西):
Key: 'this is my very very very ' +
'long string'
我想使用上面的引号,所以我不需要转义字符串中的任何内容。
当前回答
1.块符号(普通、流样式、标量):删除块后,换行符变为空格和额外换行符
---
# Note: It has 1 new line after the string
content:
Arbitrary free text
over multiple lines stopping
after indentation changes...
...
等效JSON
{
"content": "Arbitrary free text over multiple lines stopping after indentation changes..."
}
2.文字块标量:文字块标量|将包括换行符和任何尾随空格。但删除了额外的
块后面的换行符。
---
# After string we have 2 spaces and 2 new lines
content1: |
Arbitrary free text
over "multiple lines" stopping
after indentation changes...
...
等效JSON
{
"content1": "Arbitrary free text\nover \"multiple lines\" stopping\nafter indentation changes... \n"
}
3.+带文字块标量的指示符:在块后保留额外的换行符
---
# After string we have 2 new lines
plain: |+
This unquoted scalar
spans many lines.
...
等效JSON
{
"plain": "This unquoted scalar\nspans many lines.\n\n\n"
}
4.–带有文字块标量的指示符:–表示删除字符串末尾的换行符。
---
# After string we have 2 new lines
plain: |-
This unquoted scalar
spans many lines.
...
等效JSON
{
"plain": "This unquoted scalar\nspans many lines."
}
5.折叠块标量(>):
将换行符折叠为空格,但在块之后删除多余的换行符。
---
folded_newlines: >
this is really a
single line of text
despite appearances
...
等效JSON
{
"fold_newlines": "this is really a single line of text despite appearances\n"
}
有关更多信息,请访问我的博客
其他回答
您可能不相信,但YAML也可以执行多行键:
?
>
multi
line
key
:
value
对于字符串可能包含空格或不包含空格的情况,我更喜欢双引号和带反斜杠的换行符:
key: "String \
with long c\
ontent"
但请注意,如果连续行以空格开头,则需要进行转义(因为它将在其他地方被剥离):
key: "String\
\ with lon\
g content"
如果字符串包含换行符,则需要用C样式\n编写。
另请参见此问题。
在Jekyll项目中的YAML文件中,上述解决方案都不适用于我。在尝试了许多选项后,我意识到使用<br>的HTML注入也可以,因为最终所有内容都呈现为HTML:
姓名:|在一个叫La Mancha的村庄<br>我不想记住他的名字<br>。
至少它对我有用。不知道与这种方法相关的问题。
要连接没有空格的长行,请使用双引号并用反斜杠转义换行符:
key: "Loremipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemp\
orincididuntutlaboreetdoloremagnaaliqua."
1.块符号(普通、流样式、标量):删除块后,换行符变为空格和额外换行符
---
# Note: It has 1 new line after the string
content:
Arbitrary free text
over multiple lines stopping
after indentation changes...
...
等效JSON
{
"content": "Arbitrary free text over multiple lines stopping after indentation changes..."
}
2.文字块标量:文字块标量|将包括换行符和任何尾随空格。但删除了额外的
块后面的换行符。
---
# After string we have 2 spaces and 2 new lines
content1: |
Arbitrary free text
over "multiple lines" stopping
after indentation changes...
...
等效JSON
{
"content1": "Arbitrary free text\nover \"multiple lines\" stopping\nafter indentation changes... \n"
}
3.+带文字块标量的指示符:在块后保留额外的换行符
---
# After string we have 2 new lines
plain: |+
This unquoted scalar
spans many lines.
...
等效JSON
{
"plain": "This unquoted scalar\nspans many lines.\n\n\n"
}
4.–带有文字块标量的指示符:–表示删除字符串末尾的换行符。
---
# After string we have 2 new lines
plain: |-
This unquoted scalar
spans many lines.
...
等效JSON
{
"plain": "This unquoted scalar\nspans many lines."
}
5.折叠块标量(>):
将换行符折叠为空格,但在块之后删除多余的换行符。
---
folded_newlines: >
this is really a
single line of text
despite appearances
...
等效JSON
{
"fold_newlines": "this is really a single line of text despite appearances\n"
}
有关更多信息,请访问我的博客