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*、数え方による)異なる方法が存在します。
通常、>
が欲しいところです。
key: >
あなたの長さ
の文字列をここに表示します。
改行を文字列の n
として保存したい場合 (例えば、段落のあるマークダウンを埋め込む場合)、|
を使用します。
キーになります。|
### ヘディング
*弾丸
* ポイント
最後に改行を入れたくない場合は、代わりに >-
または |-
を使用します。
単語の途中で行を分割する必要がある場合、または `n
のように文字通り改行する必要がある場合は、代わりに二重引用符を使用してください。
key: "Antidisestabab
lishmentarianism.゙Get on it."
YAMLはクレイジーです。
>
, |
)これらは や `"` などの文字をエスケープせずに許可し、文字列の末尾に改行(
n`)を追加します。
>
折りたたみスタイルは、文字列内の一重の改行を削除します(ただし末尾に一重を追加し、二重の改行を一重に変換します)。
Key: >
this is my very very very
long string
→ this is my very very very long stringn
.
|
リテラルスタイルは、文字列内のすべての改行をリテラル改行にし、最後に1つ追加します。
Key: |
this is my very very very
long string
→ this is my very very verynlong stringn
.
以下は、YAML Spec 1.2の公式定義です。
スカラーコンテンツは、ブロック表記で書くことができ、すべての改行が重要であるリテラルスタイル("|"で示される)を使用します。また、空行やよりインデントされた行の末尾でない限り、各改行がスペースに折り返される折り返しスタイル("> "で示される)で記述することもできる。
>-
, |-
, >+
, |+
)を追加しました。block chomping indicator](http://www.yaml.org/spec/1.2/spec.html#id2794534)文字を追加することで、文字列の最後の改行や、末尾の空行(`nn`)の処理を制御することができます。
>
, |
: "clip": 行送りを維持し、末尾の空白行を削除します。>-
, |-
: "strip": 改行を削除し、末尾の空白行を削除します。>+
, |+
: "keep": 行送りを維持し、末尾の空白行を維持します。`,
",
'`)これらの文字列は、エスケープに制限があり、改行文字のない1行の文字列を構成します。キーと同じ行で始めることもできるし、最初に改行を追加して始めることもできる。
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.Love, YAML."
.
singlequoted style (リテラル '
は必ず2重引用、特殊文字なし、ダブルクォートで始まる文字列を表現するのに便利かもしれません)。
Key: 'this is my very very "very"
long string, isn''t it.'
(・ω・)σ)(・ω・)σ)(・ω・)σ)(・ω・)σ)(・ω・)σ)(・ω・)ノ゙長い文字列ですね
この表では、_
は「スペース文字」を意味します。ただし、"in-line 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 |
quot;spaces.quot;の前の行の末尾の空白に注意してください。
- >
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 longn stringn", "my long string"]
.
I can't even.
``2つのブロックスタイル、それぞれ2つの可能なブロックチョッピングインジケータ(またはなし)、および9つの可能なインデントインジケータ(またはなし)、1つのプレーンスタイル、2つの引用スタイル:2 x (2 + 1) x (9 + 1) + 1 + 2 = 63*。
また、この情報の一部はこちらにまとめています。
yaml folded styleを使用すると、各行の改行がスペースに置き換えられます。各行の字下げは無視されます。末尾に改行が挿入される。
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
以下のように、"block chomping indicator"を使って、末尾の改行をなくすことができます。
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/ をご参照ください。
改行を保存する場合は、|**を使用する、など。
|
This is a very long sentence
that spans several lines in the YAML
but which will be rendered as a string
with newlines preserved.
は "This is very long sentencen that spans several lines in YAMLn but which will be render as a stringn with newlines preserved.n" と訳されます。
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。 リテラルブロックスカラー:リテラルブロックスカラー |には、改行と後続スペースが含まれます。 余分を削除します。
ブロック後の改行。
---
# 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。 +リテラルブロックスカラー付きインジケーター:ブロック後に追加の改行を保持します。
---
# 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。 –リテラルブロックスカラー付きのインジケーター: – は、文字列の最後にある改行が削除されることを意味します。
---
# After string we have 2 new lines
plain: |-
This unquoted scalar
spans many lines.
...
同等のJSON 。
{
"plain": "This unquoted scalar\nspans many lines."
}
5。 折りたたみ式ブロックスカラー(>):。
改行をスペースに折りたたみますが、ブロックの後に余分な改行を削除します。
---
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"
}
もっとあなたは私のブログを訪れることができます。
Symfonyの翻訳にYAMLと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:の1行で適切な結果、複数行の翻訳が行われます。
var javascriptVariable = "This is a multi line translation.";
文字列にスペースが含まれているかどうかに関係なく、二重引用符とバックスラッシュを使用した行の続きを好みます。
key: "String \
with long c\
ontent"
ただし、継続ラインがスペースで始まる場合は、それをエスケープする必要があります(他の場所で削除されるため)。
key: "String\
\ with lon\
g content"
文字列に改行が含まれている場合、これはCスタイル \ n
で記述する必要があります。
この質問も参照してください。