在 YAML 中,我有一个很长的字符串。我想将其保留在编辑器的 80 列(左右)视图中,因此我想断开字符串。这个的语法是什么?
换句话说,我有这个:
Key: 'this is my very very very very very very long string'
我想要这个(或类似的东西):
Key: 'this is my very very very ' +
'long string'
我想使用上面的引号,所以我不需要转义字符串中的任何内容。
在 YAML 中编写多行字符串有 5 6 NINE(或 63*,取决于您的计数方式)不同的方式。
TL;博士
大部分时间使用 > :内部换行符被去掉,尽管你在最后得到一个: key: > 你的长字符串在这里。
使用 |如果您希望将这些换行符保留为 \n (例如,带有段落的嵌入式降价)。键: | ### 标题 * 项目符号 * 点
如果您不想在末尾附加换行符,请使用 >- 或 |- 。
如果您需要在单词中间分割行或想要将换行符按字面意思键入为 \n: key: "Antidisestab\ lishmentarianism。\n\n 开始使用,请使用 "..."。
YAML 很疯狂。
块标量样式 (>, |)
它们允许 \
和 "
等字符不转义,并在字符串末尾添加新行 (\n
)。
>
Folded style 删除字符串中的单个换行符(但在末尾添加一个,并将双换行符转换为单个换行符):
Key: >
this is my very very very
long string
→ this is my very very very long string\n
额外的前导空格被保留并导致额外的换行符。请参阅下面的注释。
建议:使用这个。通常这就是你想要的。
|
Literal style 将字符串中的每个换行符转换为文字换行符,并在末尾添加一个:
Key: |
this is my very very very
long string
→ this is my very very very\nlong string\n
这是来自 YAML Spec 1.2 的官方定义
标量内容可以用块表示法编写,使用文字样式(用“|”表示),其中所有换行符都很重要。或者,它们可以使用折叠样式(用“>”表示)编写,其中每个换行符都折叠到一个空格,除非它以空行或缩进更多的行结尾。
建议:使用它来插入格式化文本(尤其是 Markdown)作为值。
带有块咀嚼指示器的块样式(>-、|-、>+、|+)
您可以通过添加 block chomping indicator 字符来控制字符串中最后一个新行以及任何尾随空白行 (\n\n
) 的处理:
>, |: "clip": 保持换行,去掉尾随的空行。
>-, |-: "strip": 去掉换行,去掉尾随的空行。
>+, |+: "keep": 保持换行,保持尾随空行。
"流" 标量样式 (, ", ')
这些具有有限的转义,并构造一个没有换行符的单行字符串。它们可以与键在同一行开始,也可以先使用额外的换行符,这些换行符被剥离。加倍的换行符变成一个换行符。
plain style(没有转义,没有 #
或 :
组合,第一个字符不能是 "
、'
或许多其他标点字符):
Key: this is my very very very
long string
建议:避免。可能看起来很方便,但您可能会因意外使用禁止的标点符号并触发语法错误而自取其辱。
double-quoted style(\
和 "
必须用 \
转义,可以使用文字 \n
序列插入换行符,可以在不带空格的情况下使用尾随 \
连接行) :
Key: "this is my very very \"very\" loooo\
ng string.\n\nLove, YAML."
→ "this is my very very \"very\" loooong string.\n\nLove, YAML."
建议:在非常特殊的情况下使用。这是您可以在不添加空格的情况下跨行打破非常长的标记(如 URL)的唯一方法。也许在中线添加换行符是有用的。
single-quoted style(文字 '
必须加倍,没有特殊字符,可能对表示以双引号开头的字符串有用):
Key: 'this is my very very "very"
long string, isn''t it.'
→ "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
注意:折叠样式中的前导空格 (>)
如果您在折叠样式的非第一行的开头插入额外的空格,它们将被保留,并带有一个额外的换行符。 (流样式不会发生这种情况。)Section 6.5:
此外,折叠不适用于包含前导空白的文本行周围的换行符。请注意,这样一个缩进更多的行可能只包含这样的前导空格。
- >
my long
string
many spaces above
- my long
string
many spaces above
→ ["my long\n string\n \nmany spaces above\n","my long string\nmany spaces above"]
概括
在此表中,_
表示 space character
。 \n
表示“换行符”(JavaScript 中的 \n
),“其他功能”下除外。 “前导空格”在第一行之后应用(建立缩进)
> | " ' >- >+ |- |+ 空格/换行符转换为:尾随空格 → _ _ _ _ _ _ 前导空格 → \n_ \n_ \n_ \n_ \n_ \n_ 单个换行符 → _ \n _ _ _ _ _ \n \n 双换行 → \n \n\n \n \n \n \n \n \n\n \n\n 最终换行 → \n \n \n \n 最终双换行 → \n\ n \n\n 如何创建文字: 单引号 ' ' ' ' '' ' ' ' ' 双引号 " " " \" " " " " " 反斜杠 \ \ \ \\ \ \ \ \ \ \ 其他功能 In-用 \n 🚫 🚫 🚫 ✅ 🚫 🚫 🚫 🚫 🚫 用 \ 🚫 🚫 🚫 ✅ 🚫 🚫 🚫 🚫 🚫 # 或 : 在值中 ✅ ✅ 🚫 ✅ ✅ ✅ ✅ ✅ ✅ 可以在同一行开始✅ ✅ ✅ 🚫 🚫 🚫 🚫
例子
请注意“空格”之前行中的尾随空格。
- >
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
其中一些信息也已汇总here。
使用 yaml 折叠样式。每行的缩进将被忽略。最后将插入一个换行符。
Key: >
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
您可以使用“block chomping indicator”来消除尾随换行符,如下所示:
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.
在任何一种情况下,每个换行符都被一个空格替换。
还有其他可用的控制工具(例如,用于控制缩进)。
请参阅https://yaml-multiline.info/
{{- 'key'|trans -}}
也不起作用。
\n
。这可能是也可能不是您正在寻找的。
>-
要保留 newlines,请使用 |
,例如:
|
This is a very long sentence
that spans several lines in the YAML
but which will be rendered as a string
with newlines preserved.
被翻译为“这是一个非常长的句子\n,它在 YAML 中跨越了几行\n,但将呈现为字符串\n,并保留换行符。\n”
|
,原因对我来说并不明显:groups.google.com/forum/#!topic/pandoc-discuss/xuqEmhWgf9A
cat
时,这会导致将前导空格(YAML 所必需的)添加到输出中。
1. Block Notation(plain, flow-style, scalar):删除块后换行符变成空格和多余的换行符
---
# 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"
}
如需更多信息,您可以访问我的Blog
要连接没有空格的长行,请使用双引号并使用反斜杠转义换行符:
key: "Loremipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemp\
orincididuntutlaboreetdoloremagnaaliqua."
(感谢@Tobia)
你可能不相信,但是 YAML 也可以做多行键:
?
>
multi
line
key
:
value
key:value
之类的操作,但如果您的密钥包含换行符,您可以按照上述方式进行操作
?
是关键指示符(如映射中的键)。在许多情况下,您可能会省略键指示符,因为键后面的(必需)值指示符 :
使解析明确。但事实并非如此,您必须使用它来显式标记密钥。
如果您在 Symfony 中使用 YAML 和 Twig 进行翻译,并希望在 Javascript 中使用多行翻译,则在翻译后立即添加回车。所以即使是下面的代码:
var javascriptVariable = "{{- 'key'|trans -}}";
其中有以下 yml 翻译:
key: >
This is a
multi line
translation.
仍然会在 html 中生成以下代码:
var javascriptVariable = "This is a multi line translation.
";
所以,Twig 中的减号并不能解决这个问题。解决方案是在 yml 中的大于号之后添加这个减号:
key: >-
This is a
multi line
translation.
将得到正确的结果,在 Twig 中的一行上进行多行翻译:
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:
姓名:|
在拉曼查村<br>
,我不想<br>
记住它的名字。
至少它对我有用。不知道与此方法相关的问题。
"..." + "..."
,或者在 Bash 中的换行符之前写成反斜杠。:
会使 YAML 将其解释为对象数组。它违反了 principle of least astonishment。