Is it possible to anchor a Literal Block in YAML?

python, pyyaml, yaml

Solution

Is this what you want to do?

literal1: &literal1 |
    line
    of 
    text and stuff

literal2: &literal2 |
    another line
    of text and new stuff

literals:
-  *literal1
-  *literal2    

The following program will print ...

['line\nof \ntext and stuff\n', 'another line\nof text and new stuff\n']

import yaml

data="""
literal1: &literal1 |
    line
    of 
    text and stuff

literal2: &literal2 |
    another line
    of text and new stuff

literals:
-  *literal1
-  *literal2    
"""

pydata = yaml.load(data)
literals = pydata [ 'literals' ]

print ( type(literals), literals )

Problem

What I'm doing is creating several Literal Blocks like the one below, and putting them on a list. ``` literal1: | line of text and stuff literal2: | ... ``` and now the part which I can't figure out is to put them on a list. I've figured that I'm going to use anchors and aliases but they don't seem to work on literal blocks. Doing this doesn't work ``` literal1: | &literal1 line of text and stuff ``` it spits out an error. And also I'd rather not have to create a dict ``` literals: &literal1 literal1: | .... ``` for this to work. I'm sure theres an easy way to do this but I just cant seem to find it.

Original source