我有一个非常长的字符串:
Key: 'this is my very very very very very very long string'
我想将它分成多行,例如:
Key: 'this is my very very very ' +
'long string'
我想使用上述引号,这样我就不需要在字符串内转义任何内容。
我有一个非常长的字符串:
Key: 'this is my very very very very very very long string'
我想将它分成多行,例如:
Key: 'this is my very very very ' +
'long string'
在YAML中,有5个、6个或者9个(根据计算方式而定)不同的方法来编写多行字符串。
大部分时间使用>
:内部换行会被去除,但是最后会有一个换行:
key: >
这里是你的
长字符串。
如果你希望保留这些换行作为\n
(例如,带有段落的嵌入式Markdown),请使用|
。
key: |
### 标题
* 项目
* 点
如果你不想在末尾添加换行,请使用>-
或|-
。
如果你需要在单词中间分割行或者想要直接输入\n
作为换行,请使用"..."
:
key: "Antidisestab\
lishmentarianism.\n\n开始吧。"
YAML太疯狂了。
>
,|
)允许使用\
和"
等字符而无需转义,并在字符串末尾添加一个新行(\n
)。
>
折叠样式会移除字符串中的单个换行符(但在末尾添加一个换行符,并将双重换行符转换为单个换行符):Key: >
this is my very very very
long string
→ 这是我的非常非常非常长的字符串\n
额外的前导空格被保留并导致额外的换行符。请参见下面的说明。
建议:使用这个。通常这是你想要的。
|
文字样式
将字符串中的每个换行符转换为实际的换行符,并在末尾添加一个:
Key: |
this is my very very very
long string
→ 这是我的非常非常非常长的字符串
这是来自YAML规范1.2的官方定义
标量内容可以使用块注释写入,使用文字样式(由“|”表示),其中所有换行都是有意义的。或者,它们可以使用折叠样式(由“>”表示),其中每个换行都折叠为一个空格,除非它结束一个空行或更高缩进的行。
建议:将格式化文本(特别是Markdown)作为值插入。
>-
,|-
,>+
,|+
)的块样式您可以通过添加块换行指示符字符来控制字符串中最后一个换行符和任何尾随的空行(\n\n
)的处理方式:
>
,|
:clip:保留换行符,删除尾部空行。>-
,|-
:strip:删除换行符,删除尾部空行。>+
,|+
:keep:保留换行符,保留尾部空行。
,"
,'
)这些样式有限的转义,并构造一个没有换行符的单行字符串。它们可以与键在同一行开始,或者在之前有额外的换行符,这些换行符会被删除。双倍的换行符会变成一个换行符。
plain style(无转义,无 #
或:
组合,第一个字符不能是"
,'
或其他许多标点符号字符):
Key: this is my very very very
long string
\
和"
必须通过\
进行转义,可以使用字面量\n
插入换行,可以使用尾随\
将行连接在一起而不需要空格):Key: "this is my very very \"very\" loooo\
ng string.\n\nLove, YAML."
→ "这是我的非常非常“非常”长的字符串。\n\n爱你,YAML。"
建议:只在非常特定的情况下使用。这是唯一的方式,你可以在不添加空格的情况下将非常长的标记(如URL)跨行分隔。而且,在一行中间添加换行符可能是有用的。
单引号样式(字面意义上的'
必须加倍,没有特殊字符,可能用于表示以双引号开头的字符串):
Key: 'this is my very very "very"
long string, isn''t it.'
→ 这是我的非常非常“非常”长的字符串,不是吗。
建议:尽量避免。好处很少,主要是不方便。
如果上述还不够满足您的需求,您可以在块折叠指示符之后添加一个“block indentation indicator”:
- >8
My long string
starts over here
- |+1
This one
starts here
>
)中,前导空格会被保留。此外,折叠不适用于包含前导空白的文本行周围的换行符。请注意,这样的更缩进的行可能只包含这样的前导空白。
- >
my long
string
many spaces above
- my long
string
many spaces above
在这个表格中:_
表示空格字符,\n
表示换行字符,除非另有说明。"Leading space"指的是第二行有额外的空格字符,而第一行只有空格(这样就形成了缩进)。
> |
| |
>- |
|- |
>+ |
|+ |
" |
' |
||
---|---|---|---|---|---|---|---|---|---|
Spaces/newlines converted to: | |||||||||
Trailing space → | _ | _ | _ | _ | _ | _ | |||
Leading space → | \n_ | \n_ | \n_ | \n_ | \n_ | \n_ | |||
Single newline → | _ | \n | _ | \n | _ | \n | _ | _ | _ |
Double newline → | \n | \n\n | \n | \n\n | \n | \n\n | \n | \n | \n |
Final newline → | \n | \n | \n | \n | |||||
Final double newline → | \n | \n | \n\n | \n\n | |||||
How to create a literal: | |||||||||
Single quote | ' | ' | ' | ' | ' | ' | ' | ' | '' |
Double quote | " | " | " | " | " | " | " | \" | " |
Backslash | \ | \ | \ | \ | \ | \ | \ | \\ | \ |
Other features | |||||||||
In-line newlines with literal \n |
✅ | ||||||||
Spaceless newlines with \ |
✅ | ||||||||
# or : in value |
✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |
Can start on same line as key |
✅ | ✅ | ✅ |
请注意在“spaces”之前一行的尾随空格。
- >
very "long"
'string' with
paragraph gap, \n and
spaces.
- |
very "long"
'string' with
paragraph gap, \n and
spaces.
- very "long"
'string' with
paragraph gap, \n and
spaces.
- "very \"long\"
'string' with
paragraph gap, \n and
s\
p\
a\
c\
e\
s."
- 'very "long"
''string'' with
paragraph gap, \n and
spaces.'
- >-
very "long"
'string' with
paragraph gap, \n and
spaces.
[
"very \"long\" 'string' with\nparagraph gap, \\n and spaces.\n",
"very \"long\"\n'string' with\n\nparagraph gap, \\n and \nspaces.\n",
"very \"long\" 'string' with\nparagraph gap, \\n and spaces.",
"very \"long\" 'string' with\nparagraph gap, \n and spaces.",
"very \"long\" 'string' with\nparagraph gap, \\n and spaces.",
"very \"long\" 'string' with\nparagraph gap, \\n and spaces."
]
*
2个块样式,每个样式有2个可能的块换行指示符(或没有),以及9个可能的缩进指示符(或没有),1个普通样式和2个引用样式:2 x (2 + 1) x (9 + 1) + 1 + 2 = 63
其中一些信息也已在这里进行了总结。
"..." + "..."
或者 Bash 中的反斜杠加换行符。 - TobiaKey: >
This is a very long sentence
that spans several lines in the YAML
but which will be rendered as a string
with only a single carriage return appended to the end.
http://symfony.com/doc/current/components/yaml/yaml_format.html
您可以使用“块咬合指示器”来消除尾随的换行符,如下所示:Key: >-
This is a very long sentence
that spans several lines in the YAML
but which will be rendered as a string
with NO carriage returns.
{{- 'key'|trans -}}
也无法解决。 - Rvanlaak>-
- DylanYoung要保留换行符,请使用|
,例如:
|
This is a very long sentence
that spans several lines in the YAML
but which will be rendered as a string
with newlines preserved.
被翻译为"This is a very long sentence\n that spans several lines in the YAML\n but which will be rendered as a string\n with newlines preserved.\n"
|
,这个原因对我来说并不明显:https://groups.google.com/forum/#!topic/pandoc-discuss/xuqEmhWgf9A - cboettigcat
时,会导致输出中添加前导空格(这对于YAML是必要的)。 - Kalle Richter1. 块表示法(普通、流式、标量):换行符变成空格,块后面的额外换行符被删除。
---
# 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. + indicator with Literal Block Scalar: 保留块后的额外换行符
---
# 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"
}
想要了解更多,您可以访问我的博客
如果要将长行连接 而不使用空格,请使用双引号并使用反斜杠转义新行:
key: "Loremipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemp\
orincididuntutlaboreetdoloremagnaaliqua."
也许你不相信,但 YAML 也可以使用多行键:
?
>
multi
line
key
:
value
key:value
这样的格式,但如果你的键包含换行符,你可以按照上述描述进行操作。 - goFrendiAsgard?
是关键指示器(就像映射中的键)。在许多情况下,当 (必需的) 值指示器 :
在键后使解析无歧义时,您可以省略键指示器。但这不是这种情况,您必须使用它来明确标记键。 - Anthonvar javascriptVariable = "{{- 'key'|trans -}}";
,它所对应的yml翻译也会被添加上一个回车符。key: >
This is a
multi line
translation.
var javascriptVariable = "This is a multi line translation.
";
key: >-
This is a
multi line
translation.
var javascriptVariable = "This is a multi line translation.";
对于字符串可能包含空格或不包含空格的情况,我更喜欢使用双引号和行续符(反斜杠)进行换行:
key: "String \
with long c\
ontent"
但是需要注意一个问题,如果换行符后面的行以空格开头,那么它需要进行转义(因为在其他地方会被去除):
key: "String\
\ with lon\
g content"
如果字符串中包含换行符,需要使用C样式的\n
进行书写。对我来说,以上的解决方案都没用,在Jekyll项目的一个YAML文件中。尝试了很多选项后,我意识到带有 <br>
的HTML注入也可以解决问题,因为最终所有东西都会呈现为HTML:
name: |
In a village of La Mancha <br>
whose name I don't <br>
want to remember.
至少这种方法对我有效。我不知道与此方法相关的问题。