在YAML中,我如何分割多行的string?

在YAML中,我有一个很长的string。 我想保留在编辑器的80列左右,所以我想打破这个string。 这是什么语法?

换句话说,我有这个:

Key: 'this is my very very very very very very long string' 

我想要这个(或者是这个效果):

 Key: 'this is my very very very ' + 'long string' 

我想使用上面的引号,所以我不需要在string中的任何内容。

使用yaml折叠样式,每个换行符被replace为一个空格。 每行中的缩进将被忽略。

 > This is a very long sentence that spans several lines in the YAML but which will be rendered as a string without carriage returns. 

http://symfony.com/doc/current/components/yaml/yaml_format.html

在YAML中有5 6个 NINE (或63 *,取决于你如何计算)不同的方式来编写多行string。

块标量样式( >|

这些允许转义,并添加一个新行( \n )到您的string的末尾。

> 折叠样式删除string中的换行符(但在末尾添加一个换行符):

 Key: > this is my very very very long string 

this is my very very very long string\n

| 文字样式将string内的换行符变成文字换行符,并在末尾添加一个换行符:

 Key: | this is my very very very long string 

this is my very very very\nlong string\n

这是YAML Spec 1.2的官方定义

标量内容可以用块表示,使用文字样式(用“|”表示),其中所有换行都很重要。 或者,它们可以用折叠样式(用“>”表示)来书写,其中每个换行符被折叠到一个空格,除非它结束了一个空的或更多的缩进的行。

用块状指示符( >-|->+|+

您可以通过添加块ch写指示符字符来控制string中最后一行的处理以及任何尾随空行( \n\n ):

  • >| :“剪辑”:保持换行,删除拖尾的空白行。
  • >-|- :“strip”:删除换行,删除拖尾的空白行。
  • >+|+ :“保持”:保持换行,保留空行。

“stream”标量样式( "'

这些限制了转义,并且构造了一个没有换行字符的单行string。 他们可以和钥匙开始在同一条线上,或者先与其他换行符开始。

简单的风格 (不转义,不#或组合,对第一个字符的限制):

 Key: this is my very very very long string 

双引号样式\"必须由\转义,换行符可以用一个文字\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."

单引号样式 (文字'必须加倍,没有特殊字符,可能对表示以双引号开头的string有用):

 Key: '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表示“换行符”(在JavaScript中为\n ),除了“inline newlines”这一行,它的意思是一个反斜杠和一个n)。

  > | " ' >- >+ |- |+ -------------------------|------|-----|-----|-----|------|------|------|------ 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 | 

例子

请注意“空格”之前的行尾空格。

 - > 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." ] 

用压痕指示器阻止样式

为了防止上述情况发生,您可以添加一个“ 块缩进指示符 ”(在您的块状指示符之后,如果有的话):

 - >8 My long string starts over here - |+1 This one starts here 

* 2个块样式,每个包含2个可能的块状指示符(或无),以及9个可能的缩进指示符(或无),1个平面样式和2个引用样式:2 x(2 + 1)x(9 + 1)+ 1 + 2 = 63

为了保留换行符,使用| , 例如:

 | 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中的几行,但将被渲染为一个string\ N与保留的新行。

你可能不相信,但YAML也可以做多线键:

 ? > multi line key : value 

如果您在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. "; 

所以,小枝的减号不能解决这个问题。 解决scheme是在yml中的大于号之后添加这个减号:

 key: >- This is a multi line translation. 

将有适当的结果,在树枝的一行上多行翻译:

 var javascriptVariable = "This is a multi line translation."; 

要连接没有空格的长行,请使用双引号并用反斜杠转义换行符:

 key: "Loremipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemp\ orincididuntutlaboreetdoloremagnaaliqua." 

(谢谢@Tobia)

对于string可能包含空格的情况,我更喜欢双引号和连续的反斜杠:

 key: "String \ with long c\ ontent" 

但是请注意关于延续线以空间开始的情况的缺陷,需要逃避(因为它将在其他地方被剥离:

 key: "String\ \ with lon\ g content" 

如果string包含换行符,则需要使用C风格编写\n

另见这个问题 。

    Interesting Posts