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

时间:2010-09-24 19:47:10

标签: string yaml data-representation

在YAML中,我有一个非常长的字符串。我想将它保留在我的编辑器的80列(或左右)视图中,所以我想打破字符串。这是什么语法?

换句话说,我有这个:

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

我希望有这个(或者这个效果):

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

我想使用上面的引号,所以我不需要在字符串中转义任何内容。

9 个答案:

答案 0 :(得分:2830)

5 6 NINE (或63 *,具体取决于你算了)在YAML中编写多行字符串的不同方法。

TL; DR

  • 通常,您需要>

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

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

  • 如果您需要在单词中间拆分行或者将字符串类型换行为\n,请改为使用双引号:

    key: "Antidisestab\
     lishmentarianism.\n\nGet on it."
    
  • 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

的官方定义
  

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

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

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

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

“Flow”标量样式("'

这些具有有限的转义,并构造一个没有换行符的单行字符串。它们可以与键位于同一行,也可以首先使用其他换行符。

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

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

摘要

在此表中,_表示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."
]

使用缩进指示符块样式

以防上述情况对您来说不够,您可以添加“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"]

我甚至不能。

* 2个块样式,每个样式有2个可能的块阻塞指示符(或者没有),并且有9个可能的缩进指示符(或者没有),1个简单样式和2个引用样式:2 x(2 + 1)x(9 + 1)+ 1 + 2 = 63

其中一些信息也已经汇总here

答案 1 :(得分:736)

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

您可以使用“阻止限制指示符”来消除尾随换行符,如下所示:

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/

答案 2 :(得分:175)

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

|
  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

答案 3 :(得分:69)

<强> 1。块表示法:删除块后,换行符将成为空格和额外的换行符

---
# 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。 Literal Block Scalar:文字块标量 | 将包含换行符和任何尾随空格。但删除了额外的

阻止后的

换行符。

---
# 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。带有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。 - 带有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."
}

<强> 5。折叠块标量(&gt;):

会将换行符折叠到空格中,但会在块后删除额外的换行符。

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

答案 4 :(得分:39)

您可能不相信,但YAML也可以执行多行键:

?
 >
 multi
 line
 key
:
  value

答案 5 :(得分:35)

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

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

(谢谢@Tobia)

答案 6 :(得分:16)

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

答案 7 :(得分:8)

对于字符串可能包含空格的情况,我更喜欢双引号和带反斜杠的行继续:

key: "String \
  with long c\
  ontent"

但请注意延续线以空格开头的情况下的陷​​阱,需要进行转义(因为它会被剥离掉其他地方):

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

如果字符串包含换行符,则需要用C样式\n编写。

另见this question

答案 8 :(得分:0)

在Jekyll项目中的YAML文件中,以上解决方案均不适用于我。尝试了许多选项之后,我意识到使用<br>进行HTML注入也可以做到,因为最终所有内容都呈现为HTML:

名称:|       在拉曼恰<br>的一个村庄里,我不想<br>的名字想念。

至少对我有用。不知道与这种方法相关的问题。

相关问题