Skip to content

feat: add YAML FAQ english document. #117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions docs/user_docs/support/faq-yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ sidebar_position: 3

# YAML

### 1. YAML 字符串使用单引号和双引号的区别是什么?
## 1. What is the difference between single and double quote YAML strings?

- YAML 双引号字符串是唯一能够表达任意字符串的样式,通过使用 `\` 转义字符,比如使用 `\"` 转义双引号 `"`,使用 `\\` 转义反斜杠 `\`,并且可以使用单个反斜杠 `\` 作为双引号字符串的续行符
- YAML 单引号字符串与 YAML 双引号字符串不同的是可以自由地使用 `\` `"` 而不需要转义,但是使用两个单引号 `''` 转义单引号 `'` 字符
- YAML double-quoted strings are the only style that can express arbitrary strings, by using `\` escape characters, such as `\"` to escape double quotes `"`, `\\` to escape backslashes `\`, and a single backslash `\` can be used as a continuation character for double-quoted strings.
- YAML single-quoted strings differ from YAML double-quoted strings in that `\` and `"` can be used freely without escaping, but two single-quotes `''` are used to escape single-quote `'` characters.

比如对于如下的例子,三个字符串变量的内容是相同的
For the following example, the contents of the three string variables are the same.

```yaml
string1: 'here '' s to "quotes"'
string2: "here's to \"quotes\""
string3: here's to "quotes"
```

因此,KCL 输出 YAML 字符串的策略是当字符串内容出现单引号时,优先输出无引号字符串或双引号字符串,其他情况输出单引号字符串以避免理解上的负担。
> Note: KCL's strategy for outputting YAML strings is to output unquoted strings or double-quoted strings preferentially when single quotes appear in the string content, and output single-quoted strings in other cases to avoid the burden of understanding.

更多细节可参考: [YAML 规范 v1.2](https://yaml.org/spec/1.2.1/)
For more details, please refer to [YAML Spec v1.2](https://yaml.org/spec/1.2.1/)

### 2. YAML 中出现的 | - + > 等符号是什么含义?
## 2. What is the meaning of symbols such as | - + > in YAML?

在使用 KCL 多行字符串(使用三引号括起来的字符串),输出的 YAML 经常会携带一些特殊的记号,如 `|`,`-`,`+` `>` 等,这些记号通常为 YAML 多行字符串的表示方法,比如对于如下 KCL 代码:
When using KCL multi-line strings (triple quote strings), the output YAML often carries some special tokens, such as `|`, `-`, `+` and `>`, etc. These tokens usually are the representation method of YAML multi-line string, such as the following KCL code:

```python
data = """This is a KCL multi line string (the first line)
Expand All @@ -35,7 +35,7 @@ This is a KCL multi line string (the third line)
var = 1
```

输出 YAML 为:
The output YAML is

```yaml
data: |+
Expand All @@ -47,17 +47,17 @@ data: |+
var: 1
```

- `|` 表示**块字符串样式**,用于表示一个多行字符串,其中的所有换行符都表示字符串真实的换行;
- `>` 表示**块折叠样式**,在其中所有的换行符将被空格替换;
- `+` `-` 用于控制在字符串末尾使用换行符的情况。默认情况为字符串末尾保留单个换行符,如果要删除所有换行符,可以在样式指示符 `|` 或 `>` 后面放置一个 `-` 来完成,如果要保留末尾的换行符,则需要在 `|` `>` 后面放置一个 `+`
- `|` represents **block style**, which is used to represent a multi-line string, where all newlines in the string represent the real newlines.
- `>` represents **folding style**, in which all newlines in the string will be replaced by spaces.
- `+` and `-` are used to control the use of newlines at the end of strings. The default is to keep a single newline at the end of the string. If we want to remove all newlines, we can put a `-` after the style indicator `|` or `>`. If we want to keep the newline at the end, we need to put a `+` after `|` or `>`.

更多细节可参考: [YAML 多行字符串](https://yaml-multiline.info/) [YAML 规范 v1.2](https://yaml.org/spec/1.2.1/)
For more details, please refer to [YAML Multiline String](https://yaml-multiline.info/) and [YAML Spec v1.2](https://yaml.org/spec/1.2.1/)

### 3. YAML 中在 | - + > 等符号之后出现的数字是什么含义?
## 3. What is the meaning of numbers that appear after symbols | - + > such as |1 and |2 in YAML?

数字表示 YAML 当中的**显式缩进指示符**。对于 YAML 中的长字符串,YAML 通常第一个非空行确定字符串的缩进级别,而当第一个非空行前面具有非前导字符时,比如换行符,YAML 要求必须使用**显式缩进指示符**来指定内容的缩进级别,比如 `|2` 和 `|1` 等
Numbers represent **explicit indentation indicators** in YAML. For long strings in YAML, YAML usually the first non-blank line determines the indentation level of the string, and when the first non-blank line is preceded by a non-leading character, such as a newline, we must use **explicit indent indicators** to specify the indent level of the content, such as `|1` and `|2` etc.

比如对于如下 KCL 代码:
For example, for the following KCL code:

```python
longStringStartWithEndline = """
Expand All @@ -74,17 +74,15 @@ longStringStartWithEndline: |2
This is the third line
```

如果不需要长字符串开头的空行或换行符,则可以以如下两种方式进行 KCL 长字符串书写

- 长字符串从第 1 行开始书写
- Writing long strings from the first line.

```python
longString = """This is the second line
This is the third line
"""
```

- 使用续行符
- Writing long strings with line continuation characters.

```python
longString = """\
Expand All @@ -93,12 +91,12 @@ This is the third line
"""
```

以上两种方式输出的 YAML 均为:
The YAML output by the above two methods is:

```yaml
longString: |
This is the second line
This is the third line
```

更多细节可参考: [YAML 规范 v1.2](https://yaml.org/spec/1.2.1/)
For more details, please refer to [YAML Spec v1.2](https://yaml.org/spec/1.2.1/)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ sidebar_position: 3

# YAML 语法

### 1. YAML 字符串使用单引号和双引号的区别是什么?
## 1. YAML 字符串使用单引号和双引号的区别是什么?

- YAML 双引号字符串是唯一能够表达任意字符串的样式,通过使用 `\` 转义字符,比如使用 `\"` 转义双引号 `"`,使用 `\\` 转义反斜杠 `\`,并且可以使用单个反斜杠 `\` 作为双引号字符串的续行符
- YAML 单引号字符串与 YAML 双引号字符串不同的是可以自由地使用 `\` 和 `"` 而不需要转义,但是使用两个单引号 `''` 转义单引号 `'` 字符
Expand All @@ -21,7 +21,7 @@ string3: here's to "quotes"

更多细节可参考: [YAML 规范 v1.2](https://yaml.org/spec/1.2.1/)

### 2. YAML 中出现的 | - + > 等符号是什么含义?
## 2. YAML 中出现的 | - + > 等符号是什么含义?

在使用 KCL 多行字符串(使用三引号括起来的字符串),输出的 YAML 经常会携带一些特殊的记号,如 `|`,`-`,`+` 和 `>` 等,这些记号通常为 YAML 多行字符串的表示方法,比如对于如下 KCL 代码:

Expand Down Expand Up @@ -53,7 +53,7 @@ var: 1

更多细节可参考: [YAML 多行字符串](https://yaml-multiline.info/) 和 [YAML 规范 v1.2](https://yaml.org/spec/1.2.1/)

### 3. YAML 中在 | - + > 等符号之后出现的数字是什么含义?
## 3. YAML 中在 | - + > 等符号之后出现的数字是什么含义?

数字表示 YAML 当中的**显式缩进指示符**。对于 YAML 中的长字符串,YAML 通常第一个非空行确定字符串的缩进级别,而当第一个非空行前面具有非前导字符时,比如换行符,YAML 要求必须使用**显式缩进指示符**来指定内容的缩进级别,比如 `|2` 和 `|1` 等

Expand Down