【问题标题】:Convert longhand YAML syntax to shorthand YAML syntax将简写 YAML 语法转换为简写 YAML 语法
【发布时间】:2015-08-04 04:56:04
【问题描述】:

我有一个用长语法编写的 YAML 文件

children:
  -
    type: section
    subtype: false
    title: Top-wrapper
    attributes: {  }
    children:
      -
        type: section
        subtype: false
        title: Second-wrapper
        attributes: {  }
        children:
          -
            type: section
            subtype: false
            title: Header
            attributes: {  }
            children:
              -
                type: container
                subtype: false
                title: Untitled
                attributes: {  }
                children:
                  -
                    type: grid
                    subtype: false
                    title: Untitled
                    attributes: {  }
                    children:
                      -
                        type: block
                        subtype: false
                        title: Untitled
                        attributes:
                          size: 100
                        children:
                          -
                            type: spacer
                            subtype: false
                            title: Spacer
                            attributes:
                              enabled: 1
                            children: {  }
          -
            type: section
            subtype: false
            title: Navigation
            attributes: {  }
            children:
              -
                type: container
                subtype: false
                title: Untitled
                attributes: {  }
                children:
                  -
                    type: grid
                    subtype: false
                    title: Untitled
                    attributes: {  }
                    children:
                      -
                        type: block
                        subtype: false
                        title: Untitled
                        attributes:
                          size: 100
                        children:
                          -
                            type: spacer
                            subtype: false
                            title: Spacer
                            attributes:
                              enabled: 1
                            children: {  }
      -
        type: section
        subtype: false
        title: Showcase
        attributes: {  }
        children:
          -
            type: container
            subtype: false
            title: Untitled
            attributes: {  }
            children:
              -
                type: grid
                subtype: false
                title: Untitled
                attributes: {  }
                children:
                  -
                    type: block
                    subtype: false
                    title: Untitled
                    attributes:
                      size: 100
                    children:
                      -
                        type: spacer
                        subtype: false
                        title: Spacer
                        attributes:
                          enabled: 1
                        children: {  }
  -
    type: section
    subtype: false
    title: Feature
    attributes: {  }
    children:
      -
        type: container
        subtype: false
        title: Untitled
        attributes: {  }
        children:
          -
            type: grid
            subtype: false
            title: Untitled
            attributes: {  }
            children:
              -
                type: block
                subtype: false
                title: Untitled
                attributes:
                  size: 100
                children:
                  -
                    type: spacer
                    subtype: false
                    title: Spacer
                    attributes:
                      enabled: 1
                    children: {  }
  -
    type: section
    subtype: false
    title: Main-content
    attributes: {  }
    children:
      -
        type: grid
        subtype: false
        title: Untitled
        attributes: {  }
        children:
          -
            type: block
            subtype: false
            title: Untitled
            attributes:
              size: 20
            children:
              -
                type: section
                subtype: false
                title: Sidebar-left
                attributes: {  }
                children:
                  -
                    type: grid
                    subtype: false
                    title: Untitled
                    attributes: {  }
                    children:
                      -
                        type: block
                        subtype: false
                        title: Untitled
                        attributes:
                          size: 100
                        children:
                          -
                            type: position
                            subtype: false
                            title: Sidebar-left
                            attributes:
                              enabled: 1
                              key: sidebar-left
                              title: Sidebar-left
                            children: {  }
          -
            type: block
            subtype: false
            title: Untitled
            attributes:
              size: 60
            children:
              -
                type: section
                subtype: false
                title: Main
                attributes: {  }
                children:
                  -
                    type: grid
                    subtype: false
                    title: Untitled
                    attributes: {  }
                    children:
                      -
                        type: block
                        subtype: false
                        title: Untitled
                        attributes:
                          size: 100
                        children:
                          -
                            type: pagecontent
                            subtype: false
                            title: Pagecontent
                            attributes:
                              enabled: 1
                            children: {  }
          -
            type: block
            subtype: false
            title: Untitled
            attributes:
              size: 20
            children:
              -
                type: section
                subtype: false
                title: Sidebar-right
                attributes: {  }
                children:
                  -
                    type: grid
                    subtype: false
                    title: Untitled
                    attributes: {  }
                    children:
                      -
                        type: block
                        subtype: false
                        title: Untitled
                        attributes:
                          size: 100
                        children:
                          -
                            type: position
                            subtype: false
                            title: Sidebar-right
                            attributes:
                              enabled: 1
                              key: sidebar-right
                              title: Sidebar-right
                            children: {  }
  -
    type: section
    subtype: false
    title: Subfeature
    attributes: {  }
    children:
      -
        type: container
        subtype: false
        title: Untitled
        attributes: {  }
        children:
          -
            type: grid
            subtype: false
            title: Untitled
            attributes: {  }
            children:
              -
                type: block
                subtype: false
                title: Untitled
                attributes:
                  size: 100
                children:
                  -
                    type: spacer
                    subtype: false
                    title: Spacer
                    attributes:
                      enabled: 1
                    children: {  }
  -
    type: section
    subtype: false
    title: Footer
    attributes: {  }
    children:
      -
        type: container
        subtype: false
        title: Untitled
        attributes: {  }
        children:
          -
            type: grid
            subtype: false
            title: Untitled
            attributes: {  }
            children:
              -
                type: block
                subtype: false
                title: Untitled
                attributes:
                  size: 100
                children:
                  -
                    type: spacer
                    subtype: false
                    title: Spacer
                    attributes:
                      enabled: 1
                    children: {  }
  -
    type: offcanvas
    subtype: false
    title: Offcanvas
    attributes:
      name: 'Offcanvas Section'
    children: {  }
  -
    type: atoms
    subtype: false
    title: Atoms
    attributes:
      name: 'Atoms Section'
    children:
      -
        type: grid
        subtype: false
        title: Untitled
        attributes: {  }
        children: {  }

我想把它转换成类似这样的简写语法

layout:
  1:
   - top-wrapper 100:
      header:
        container:
          - spacer

      navigation:
        container:
          - [particle-logo 20, particle-menu 80]

      showcase:
        container:
          - spacer

  feature:
    container:
      - spacer

  main:
    container:
      - system-messages
      - pagecontent

  footer:
    container:
      - spacer

  debug:
    container:
      - spacer

  offcanvas:
    - particle-mobile-menu

我知道 YAML 对使用简写语法有一定的规定,我该如何使用它?

【问题讨论】:

    标签: syntax yaml shorthand


    【解决方案1】:

    YAML 规范中指定的缩短 YAML 的唯一方法是使用 anchors 和对这些锚点的引用(称为别名)。任何其他缩短都必须在使用 YAML 通过语义解释特定标量产生的数据的程序中完成。

    内置的锚机制通常用于让 YAML 文档共享原始数据,例如如果一个映射在映射和序列的层次结构中的两个位置被引用。这允许您将输入重写为:

    children:
      -
        type: section
        subtype: false
        title: Top-wrapper
        attributes: {  }
        children:
          -
            type: section
            subtype: false
            title: Second-wrapper
            attributes: {  }
            children:
              -
                type: section
                subtype: false
                title: Header
                attributes: {  }
                children: &cont00   # <- define anchor for the sequence node
                  -
                    type: container
                    subtype: false
                    title: Untitled
                    attributes: {  }
                    children:
                      -
                        type: grid
                        subtype: false
                        title: Untitled
                        attributes: {  }
                        children:
                          -
                            type: block
                            subtype: false
                            title: Untitled
                            attributes:
                              size: 100
                            children:
                              -
                                type: spacer
                                subtype: false
                                title: Spacer
                                attributes:
                                  enabled: 1
                                children: {  }
              -
                type: section
                subtype: false
                title: Navigation
                attributes: {  }
                children: *cont00
          -
            type: section
            subtype: false
            title: Showcase
            attributes: {  }
            children: *cont00  # <- use the achored node
      -
        type: section
        subtype: false
        title: Feature
        attributes: {  }
        children: *cont00  # <- use the anchored node
      -
        type: section
        subtype: false
        title: Main-content
        attributes: {  }
        children:
          -
            type: grid
            subtype: false
            title: Untitled
            attributes: {  }
            children:
              -
                type: block
                subtype: false
                title: Untitled
                attributes:
                  size: 20
                children:
                  -
                    type: section
                    subtype: false
                    title: Sidebar-left
                    attributes: {  }
                    children:
                      -
                        type: grid
                        subtype: false
                        title: Untitled
                        attributes: {  }
                        children:
                          -
                            type: block
                            subtype: false
                            title: Untitled
                            attributes:
                              size: 100
                            children:
                              -
                                type: position
                                subtype: false
                                title: Sidebar-left
                                attributes:
                                  enabled: 1
                                  key: sidebar-left
                                  title: Sidebar-left
                                children: {  }
              -
                type: block
                subtype: false
                title: Untitled
                attributes:
                  size: 60
                children:
                  -
                    type: section
                    subtype: false
                    title: Main
                    attributes: {  }
                    children:
                      -
                        type: grid
                        subtype: false
                        title: Untitled
                        attributes: {  }
                        children:
                          -
                            type: block
                            subtype: false
                            title: Untitled
                            attributes:
                              size: 100
                            children:
                              -
                                type: pagecontent
                                subtype: false
                                title: Pagecontent
                                attributes:
                                  enabled: 1
                                children: {  }
              -
                type: block
                subtype: false
                title: Untitled
                attributes:
                  size: 20
                children:
                  -
                    type: section
                    subtype: false
                    title: Sidebar-right
                    attributes: {  }
                    children:
                      -
                        type: grid
                        subtype: false
                        title: Untitled
                        attributes: {  }
                        children:
                          -
                            type: block
                            subtype: false
                            title: Untitled
                            attributes:
                              size: 100
                            children:
                              -
                                type: position
                                subtype: false
                                title: Sidebar-right
                                attributes:
                                  enabled: 1
                                  key: sidebar-right
                                  title: Sidebar-right
                                children: {  }
      -
        type: section
        subtype: false
        title: Subfeature
        attributes: {  }
        children: *cont00
      -
        type: section
        subtype: false
        title: Footer
        attributes: {  }
        children: *cont00
      -
        type: offcanvas
        subtype: false
        title: Offcanvas
        attributes:
          name: 'Offcanvas Section'
        children: {  }
      -
        type: atoms
        subtype: false
        title: Atoms
        attributes:
          name: 'Atoms Section'
        children:
          -
            type: grid
            subtype: false
            title: Untitled
            attributes: {  }
            children: {  }
    children:
      -
        type: section
        subtype: false
        title: Top-wrapper
        attributes: {  }
        children:
          -
            type: section
            subtype: false
            title: Second-wrapper
            attributes: {  }
            children:
              -
                type: section
                subtype: false
                title: Header
                attributes: {  }
                children: *cont00
              -
                type: section
                subtype: false
                title: Navigation
                attributes: {  }
                children: *cont00
          -
            type: section
            subtype: false
            title: Showcase
            attributes: {  }
            children: *cont00
      -
        type: section
        subtype: false
        title: Feature
        attributes: {  }
        children: *cont00
      -
        type: section
        subtype: false
        title: Main-content
        attributes: {  }
        children:
          -
            type: grid
            subtype: false
            title: Untitled
            attributes: {  }
            children:
              -
                type: block
                subtype: false
                title: Untitled
                attributes:
                  size: 20
                children:
                  -
                    type: section
                    subtype: false
                    title: Sidebar-left
                    attributes: {  }
                    children:
                      -
                        type: grid
                        subtype: false
                        title: Untitled
                        attributes: {  }
                        children:
                          -
                            type: block
                            subtype: false
                            title: Untitled
                            attributes:
                              size: 100
                            children:
                              -
                                type: position
                                subtype: false
                                title: Sidebar-left
                                attributes:
                                  enabled: 1
                                  key: sidebar-left
                                  title: Sidebar-left
                                children: {  }
              -
                type: block
                subtype: false
                title: Untitled
                attributes:
                  size: 60
                children:
                  -
                    type: section
                    subtype: false
                    title: Main
                    attributes: {  }
                    children:
                      -
                        type: grid
                        subtype: false
                        title: Untitled
                        attributes: {  }
                        children:
                          -
                            type: block
                            subtype: false
                            title: Untitled
                            attributes:
                              size: 100
                            children:
                              -
                                type: pagecontent
                                subtype: false
                                title: Pagecontent
                                attributes:
                                  enabled: 1
                                children: {  }
              -
                type: block
                subtype: false
                title: Untitled
                attributes:
                  size: 20
                children:
                  -
                    type: section
                    subtype: false
                    title: Sidebar-right
                    attributes: {  }
                    children:
                      -
                        type: grid
                        subtype: false
                        title: Untitled
                        attributes: {  }
                        children:
                          -
                            type: block
                            subtype: false
                            title: Untitled
                            attributes:
                              size: 100
                            children:
                              -
                                type: position
                                subtype: false
                                title: Sidebar-right
                                attributes:
                                  enabled: 1
                                  key: sidebar-right
                                  title: Sidebar-right
                                children: {  }
      -
        type: section
        subtype: false
        title: Subfeature
        attributes: {  }
        children: *cont00
      -
        type: section
        subtype: false
        title: Footer
        attributes: {  }
        children: *cont00
      -
        type: offcanvas
        subtype: false
        title: Offcanvas
        attributes:
          name: 'Offcanvas Section'
        children: {  }
      -
        type: atoms
        subtype: false
        title: Atoms
        attributes:
          name: 'Atoms Section'
        children:
          -
            type: grid
            subtype: false
            title: Untitled
            attributes: {  }
            children: {  }
    

    即你用&amp;+"unique id" 标记一个节点(以及下面的所有东西) 然后你用*+"unique id"“插入”同一个节点

    这不是字符串的重写机制,例如 C 中的宏,因此没有参数化:节点结构必须完全匹配才能工作。在读取这样的 YAML 文件时,您最终应该会在两个位置使用一个对象。

    如果您想要参数化,您需要自己创建一些用于加载(和写入)的机制,这可以进一步减少文件的长度,但 YAML 无法扩展它,因为只有您的程序知道如何这样做。

    【讨论】:

      猜你喜欢
      • 2020-12-15
      • 2019-09-15
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2012-01-06
      相关资源
      最近更新 更多