协慌网

登录 贡献 社区

在 YAML 中,如何在多行中断字符串?

在 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; DR

  • 通常,你想要>

    key: >
      Your long
      string here.
  • 如果您希望将换行符保留为字符串中的\n (例如,带有段落的嵌入式 markdown),请使用|

    key: |
      ### Heading
    
      * Bullet
      * Points
  • 如果您不希望在末尾添加换行符,请使用>-|-代替。

  • 如果你需要在单词的中间分割线条或字面上输入换行符\n ,请改用双引号:

    key: "Antidisestab\
     lishmentarianism.\n\nGet on it."
  • YAML 很疯狂。

块标量样式( >|

这些允许使用\"而不转义,并在字符串末尾添加新行( \n )。

> 折叠样式删除字符串中的单个换行符(但最后添加一个换行符,并将双换行符转换为单个换行符):

Key: >
  this is my very very very
  long string

this is my very very very long string\n

| 文字样式将字符串中的每个换行符转换为文字换行符,并在最后添加一个换行符:

Key: |
  this is my very very very 
  long string

this is my very very very\nlong string\n

这是YAML Spec 1.2的官方定义

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

使用块咀嚼指示器阻止样式( >-|->+|+

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

  • >| :“剪辑”:保持换行,删除尾随空白行。
  • >-|- :“strip”:删除换行符,删除尾随空白行。
  • >+|+ :“keep”:保持换行,保持尾随空行。

“流” 标量样式( "'

它们具有有限的转义,并构造一个没有换行符号的单行字符串。它们可以与键在同一行开始,或者首先使用其他换行符。

普通样式 (没有转义,没有#:组合,第一个字符的限制):

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

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

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 ),除了 “内联换行符” 行,其中字面意思是反斜杠和 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

附录

如果您在折叠样式的非第一行的开头插入额外的空格,它们将被保留,并带有奖励换行符。流样式不会发生这种情况:

- >
    my long
      string
- 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

一些信息也被归纳在这里

使用 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/

要保留换行符,请使用| , 例如:

|
  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