在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 *,具体取决于您的计算方式) .
这些允许 \ 和 " 等字符无需转义,并在字符串末尾添加一个新行( \n ) .
\
"
\n
> Folded style删除字符串中的单个换行符(但最后添加一个换行符,并将双换行符转换为单个换行符):
>
Key: > this is my very very very long string
→ this is my very very very long string\n
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
this is my very very very\nlong string\n
这是YAML Spec 1.2的官方定义
标量内容可以用块表示法编写,使用文字样式(用“|”表示),其中所有换行符都很重要 . 或者,它们可以用折叠样式(用“>”表示)书写,其中每个换行符折叠到一个空格,除非它结束空行或更多缩进行 .
您可以通过添加block chomping indicator字符来控制字符串中最后一个新行的处理以及任何尾随空行( \n\n ):
\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."
"this is my very very \"very\" loooong string.\n\nLove, YAML."
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."
"this is my very very \"very\" long string, isn't it."
在此表中, _ 表示 space character . \n 表示"newline character"(在JavaScript中为 \n ),除了"in-line newlines"行,其中字面意思是反斜杠和n) .
_
space character
> | " ' >- >+ |- |+ -------------------------|------|-----|-----|-----|------|------|------|------ Trailing spaces | Kept | Kept | | | | Kept | Kept | Kept | Kept 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 dbl nl's => | | | | | | | Kept | | Kept In-line newlines | No | No | No | \n | No | No | No | No | No Spaceless newlines| No | No | No | \ | No | No | No | No | No Single quote | ' | ' | ' | ' | '' | ' | ' | ' | ' Double quote | " | " | " | \" | " | " | " | " | " Backslash | \ | \ | \ | \\ | \ | \ | \ | \ | \ " #", ": " | Ok | Ok | No | Ok | Ok | Ok | Ok | Ok | Ok Can start on same | No | No | Yes | Yes | Yes | No | No | No | No 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." ]
如果以上情况对您来说还不够,可以添加一个“block indentation indicator”(在您的块选择指示符之后,如果有的话):
- >8 My long string starts over here - |+1 This one starts here
如果您在折叠样式的非第一行的开头插入额外的空格,它们将被保留,并带有奖励换行符 . 流样式不会发生这种情况:
- > my long string - my long string
→ ["my long\n string\n", "my long string"]
["my long\n string\n", "my long string"]
我甚至不能 .
* 2个块样式,每个样式有2个可能的块阻塞指示符(或者没有),并且有9个可能的缩进指示符(或者没有),1个普通样式和2个引用样式:2 x(2 1)x(9 1)1 2 = 63
*
其中一些信息也已被总结here .
如果您在Symfony中使用yml和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 编写 .
另见this question .
要保留 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 ”
使用yaml折叠样式,每个换行符由空格替换 . 每行中的缩进将被忽略 . 最后会插入换行符 .
> 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
您可以使用“块咀嚼指示器”来消除拖尾换行符,如下:
>- 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/
1. Block Notation: 删除块后,换行符将成为空格和额外的换行符
--- # Note: It has 1 new line after the string content: Arbitrary free text over multiple lines stopping after indentation changes... ...
Equivalent JSON
{ "content": "Arbitrary free text over multiple lines stopping after indentation changes..." }
2. Literal Block Scalar: Literal Block Scalar | 将包含换行符和任何尾随空格 . 但删除额外的
块之后的换行符 .
--- # After string we have 2 spaces and 2 new lines content1: | Arbitrary free text over "multiple lines" stopping after indentation changes... ...
{ "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. ...
{ "plain": "This unquoted scalar\nspans many lines.\n\n\n" }
4. – indicator with Literal Block Scalar: – 表示删除字符串末尾的换行符 .
--- # After string we have 2 new lines plain: |- This unquoted scalar spans many lines. ...
{ "plain": "This unquoted scalar\nspans many lines." }
5. Folded Block Scalar(>):
将换行符折叠到空格,但删除块后的额外换行符 .
--- folded_newlines: > this is really a single line of text despite appearances ...
{ "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
8 回答
在YAML中写入多行字符串的方法有5 6 NINE (或63 *,具体取决于您的计算方式) .
块标量样式(>,|)
这些允许
\
和"
等字符无需转义,并在字符串末尾添加一个新行(\n
) .>
Folded style删除字符串中的单个换行符(但最后添加一个换行符,并将双换行符转换为单个换行符):→
this is my very very very long string\n
|
Literal style将字符串中的每个换行符转换为文字换行符,并在末尾添加一个换行符:→
this is my very very very\nlong string\n
这是YAML Spec 1.2的官方定义
具有块咀嚼指示符的块样式(> - ,| - ,>,|)
您可以通过添加block chomping indicator字符来控制字符串中最后一个新行的处理以及任何尾随空行(
\n\n
):>
,|
:"clip":保持换行,删除尾随空白行 .>-
,|-
:"strip":删除换行符,删除尾随空白行 .>+
,|+
:"keep":保持换行,保持尾随空白行 .“Flow”标量样式(,“,')
它们具有有限的转义,并构造一个没有换行符号的单行字符串 . 它们可以与键位于同一行,也可以首先使用其他换行符 .
plain style(没有转义,没有
#
或:
组合,第一个字符的限制):double-quoted style(
\
和"
必须由\
转义,新行可以插入文字\n
序列,行可以连接,不带空格,尾随\
):→
"this is my very very \"very\" loooong string.\n\nLove, YAML."
single-quoted style(文字
'
必须加倍,没有特殊字符,可能对表示以双引号开头的字符串有用):→
"this is my very very \"very\" long string, isn't it."
摘要
在此表中,
_
表示space character
.\n
表示"newline character"(在JavaScript中为\n
),除了"in-line newlines"行,其中字面意思是反斜杠和n) .例子
注意"spaces."之前的行上的尾随空格
具有缩进指示符的块样式
如果以上情况对您来说还不够,可以添加一个“block indentation indicator”(在您的块选择指示符之后,如果有的话):
附录
如果您在折叠样式的非第一行的开头插入额外的空格,它们将被保留,并带有奖励换行符 . 流样式不会发生这种情况:
→
["my long\n string\n", "my long string"]
我甚至不能 .
*
2个块样式,每个样式有2个可能的块阻塞指示符(或者没有),并且有9个可能的缩进指示符(或者没有),1个普通样式和2个引用样式:2 x(2 1)x(9 1)1 2 = 63其中一些信息也已被总结here .
如果您在Symfony中使用yml和Twig进行翻译,并希望在Javascript中使用多行翻译,则会在翻译后立即添加回车符 . 所以即使是以下代码:
var javascriptVariable = "{{- 'key'|trans -}}";
其中有以下yml翻译:
仍然会在html中产生以下代码:
所以,Twig中的减号并没有解决这个问题 . 解决方案是在大于登录yml后添加此减号:
将在Twig的一行中获得正确的结果,多行翻译:
对于字符串可能包含空格或不包含空格的情况,我更喜欢双引号和带反斜杠的行继续:
但请注意延续线以空格开头的情况下的缺陷,需要进行转义(因为它会在其他地方被剥离):
如果字符串包含换行符,则需要以C样式
\n
编写 .另见this question .
要保留 newlines ,请使用 | ,例如:
被翻译为“这是一个非常长的句子 \n ,它跨越YAML \n 中的几行,但它将被呈现为字符串 \n 并保留换行符 . \n ”
使用yaml折叠样式,每个换行符由空格替换 . 每行中的缩进将被忽略 . 最后会插入换行符 .
http://symfony.com/doc/current/components/yaml/yaml_format.html
您可以使用“块咀嚼指示器”来消除拖尾换行符,如下:
还有其他控制工具可用(例如,用于控制压痕) .
见https://yaml-multiline.info/
1. Block Notation: 删除块后,换行符将成为空格和额外的换行符
Equivalent JSON
2. Literal Block Scalar: Literal Block Scalar | 将包含换行符和任何尾随空格 . 但删除额外的
块之后的换行符 .
Equivalent JSON
3. + indicator with Literal Block Scalar: 阻止后保留额外的换行符
Equivalent JSON
4. – indicator with Literal Block Scalar: – 表示删除字符串末尾的换行符 .
Equivalent JSON
5. Folded Block Scalar(>):
将换行符折叠到空格,但删除块后的额外换行符 .
Equivalent JSON
更多你可以访问我的blog
要连接没有空格的长行,请使用双引号并使用反斜杠转义换行符:
(谢谢@Tobia)
您可能不相信,但YAML也可以执行多行键: