In YAML, how do I break a string over multiple lines?
在山药里,我有一根很长的绳子。我想将它保存在编辑器的80列(或更多)视图中,所以我想中断字符串。这个的语法是什么?
换句话说,我有:
1 | Key: 'this is my very very very very very very long string' |
我想要这个(或是一些有这个效果的东西):
1 2 | Key: 'this is my very very very ' + 'long string' |
我想使用上面的引号,所以不需要转义字符串中的任何内容。
在yaml中有不同的写入多行字符串的方法。
DR通常,您需要
> :1
2
3key: >
Your long
string here.如果希望换行符在字符串中保留为
(例如,嵌入带段落的标记),请使用
| 。1
2
3
4
5key: |
### Heading
* Bullet
* Points如果不希望在末尾附加换行符,请使用
>- 或|- 。如果需要在单词中间拆分行或在
中键入换行符,请使用双引号:
1
2
3
4key:"Antidisestab\
lishmentarianism.
Get on it."YAML是疯狂的。
块标量样式(
它们允许不转义的字符,如
1 2 3 | Key: > this is my very very very long string |
→
1 2 3 | Key: | this is my very very very long string |
→
long string
这是Yaml规范1.2的官方定义。
Scalar content can be written in block notation, using a literal style (indicated by"|") where all line breaks are significant. Alternatively, they can be written with the folded style (denoted by">") where each line break is folded to a space unless it ends an empty or a more-indented line.
带块选择指示器的块样式(
您可以通过添加块chomping指示器字符来控制字符串中最后一行和任何尾随空白行(
)的处理:
> 、| "剪辑":保持换行,删除尾随空白行。>- 、|- "剥线":去掉送线,去掉尾随的空白线。>+ 和|+ 中的"保持":保持换行,保持尾随空白行。
"流"标量样式(
它们有有限的转义,并且构造一个没有新行字符的单行字符串。它们可以与键在同一行上开始,也可以先用其他换行符开始。
普通样式(不转义,不使用
1 2 | Key: this is my very very very long string |
双引号样式(
1 2 3 4 | Key:"this is my very very "very" loooo\ ng string. Love, YAML." |
→
Love, YAML."。
单引号样式(文字
1 2 | Key: 'this is my very very"very" long string, isn''t it.' |
→
本表中,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | > | " ' >- >+ |- |+ -------------------------|------|-----|-----|-----|------|------|------|------ Trailing spaces | Kept | Kept | | | | Kept | Kept | Kept | Kept Single newline => | _ | | _ | _ | _ | _ | _ | | Double newline => | | | | | | | | | Final newline => | | | | | | | | | Final dbl nl's => | | | | | | | Kept | | Kept In-line newlines | No | No | No | | 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 | |
实例
注意"空格"前一行的尾随空格。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | - > very"long" 'string' with paragraph gap, and spaces. - | very"long" 'string' with paragraph gap, and spaces. - very"long" 'string' with paragraph gap, and spaces. -"very "long" 'string' with paragraph gap, and s\ p\ a\ c\ e\ s." - 'very"long" ''string'' with paragraph gap, and spaces.' - >- very"long" 'string' with paragraph gap, and spaces. [ "very "long" 'string' with paragraph gap, \ and spaces. ", "very "long" 'string' with paragraph gap, \ and spaces. ", "very "long" 'string' with paragraph gap, \ and spaces.", "very "long" 'string' with paragraph gap, and spaces.", "very "long" 'string' with paragraph gap, \ and spaces.", "very "long" 'string' with paragraph gap, \ and spaces." ] |
带缩进标记的块样式
为了防止上面的内容对您来说不够,您可以添加一个"块缩进指示符"(如果您有块缩进指示符,请在块chomping指示符之后添加):
1 2 3 4 5 6 | - >8 My long string starts over here - |+1 This one starts here |
补遗
如果您在不是第一行的开始处插入折叠样式的额外空格,它们将保留,并有一个额外的换行符。这不适用于流样式:
1 2 3 4 5 | - > my long string - my long string |
→
string
","my long string"]
我甚至不能。
这里也总结了一些信息。
使用yaml折叠样式,每个换行符都被一个空格替换。将忽略每行中的缩进。行尾将插入换行符。
1 2 3 4 5 | > 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网站
您可以使用"块chomping指示符"消除尾随行中断,如下所示:
1 2 3 4 5 | >- 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 2 3 4 5 | | 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但哪一个将呈现为字符串? n保留换行符。"
1。块表示法:删除块后,换行符变为空格和多余的换行符
1 2 3 4 5 6 7 8 | --- # Note: It has 1 new line after the string content: Arbitrary free text over multiple lines stopping after indentation changes... ... |
等效JSON
1 2 3 | { "content":"Arbitrary free text over multiple lines stopping after indentation changes..." } |
2。文本块标量:文本块标量将包括换行符和任何尾随空格。但是去掉了多余的
块后换行。
1 2 3 4 5 6 7 8 9 | --- # After string we have 2 spaces and 2 new lines content1: | Arbitrary free text over"multiple lines" stopping after indentation changes... ... |
等效JSON
1 2 3 4 5 6 | { "content1":"Arbitrary free text over "multiple lines" stopping after indentation changes... " } |
三。+带文本块标量的指示器:在块后保留多余的换行符
1 2 3 4 5 6 7 8 | --- # After string we have 2 new lines plain: |+ This unquoted scalar spans many lines. ... |
等效JSON
1 2 3 4 5 6 7 | { "plain":"This unquoted scalar spans many lines. " } |
4。–带有文本块标量的指示符:–表示字符串末尾的换行符被删除。
1 2 3 4 5 6 7 8 | --- # After string we have 2 new lines plain: |- This unquoted scalar spans many lines. ... |
等效JSON
1 2 3 4 | { "plain":"This unquoted scalar spans many lines." } |
5。折叠块标量(>):
将换行符折叠到空格中,但在块后删除多余的换行符。
1 2 3 4 5 6 7 8 | --- folded_newlines: > this is really a single line of text despite appearances ... |
等效JSON
1 2 3 4 | { "fold_newlines":"this is really a single line of text despite appearances " } |
更多信息,请访问我的博客
您可能不相信,但Yaml也可以使用多行键:
1 2 3 4 5 6 7 | ? > multi line key : value |
要连接不带空格的长行,请使用双引号并用反斜杠转义新行:
1 2 | key:"Loremipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemp\ orincididuntutlaboreetdoloremagnaaliqua." |
(谢谢@ Tobia)
如果您在symfony中使用yml和twig进行翻译,并且希望在javascript中使用多行翻译,则在翻译后立即添加回车。所以即使是下面的代码:
有以下YML翻译:
1 2 3 4 | key: > This is a multi line translation. |
仍将在HTML中生成以下代码:
1 2 | var javascriptVariable ="This is a multi line translation. "; |
所以,树枝中的负号并不能解决这个问题。解决方案是在YML中的大于号后加上这个减号:
1 2 3 4 | key: >- This is a multi line translation. |
将有适当的结果,多行翻译在一行的细枝上:
1 | var javascriptVariable ="This is a multi line translation."; |
对于字符串是否包含空格的情况,我更喜欢双引号和带反斜杠的行继续符:
1 2 3 | key:"String \ with long c\ ontent" |
但是请注意,如果一个延续行以空格开头,那么它就需要被转义(因为它将在其他地方被剥离):
1 2 3 | key:"String\ \ with lon\ g content" |
如果字符串包含换行符,则需要用C样式的
另请参见此问题。
以上所有的解决方案都不适合我,在Jekyll项目的yaml文件中。在尝试了许多选项之后,我意识到使用
姓名:
至少对我有用。不知道与此方法相关的问题。