当前位置: 动力学知识库 > 问答 > 编程问答 >

Convert longhand YAML syntax to shorthand YAML syntax

问题描述:

I have a YAML file that has been written in the long syntax

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: { }

I would like to convert it to use the shorthand syntax similar to this

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

I know YAML has certain rules about using the shorthand syntax, how can I use it?

网友答案:

The only way specified in the YAML specification to shorten YAML is by using anchors and references to these anchors (called aliases). Any other shortening would have to be done within the program that uses the data that YAML produces by semantically interpreting specific scalars.

The built-in anchor mechanism is normally used to have YAML documents share original data, e.g. if one mappings is referenced in two places in the hierarchical structure of mappings and sequences. That allows you to rewrite your input as:

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: {  }

I.e. you mark a node (and everything underneath) with a &+"unique id" and you "insert" that same node with *+"unique id"

This is not a rewrite mechanism of the strings, such as the macros in C, and therefore there is no parametrising: the node structure must match exactly for this to work. When reading in such a YAML file you should end up with one object used in two locations.

If you want parametrisation, you would need to create some mechanism for loading (and writing) yourself, and that could further reduce the length of your file, but YAML would not be able to expand that as only your program would know how to do that.

分享给朋友:
您可能感兴趣的文章:
随机阅读: