ChatGPT解决这个技术问题 Extra ChatGPT

How to escape indicator characters (colon and hyphen) in YAML

In a config file, I have a key to which I wish to assign a URL. The problem is that YAML interprets : and - characters as either creating mappings or lists, so it has a problem with the line

url: http://www.some-site.example/

(both because of the colon following HTTP and the hyphen in the middle)

Is there an explicit way to escape : and -? Or would it work to just put the whole thing in single quotes and call it a day?


S
Stephen Ostermiller

Quotes:

"url: http://www.some-site.example/"

To clarify, I meant “quote the value” and originally thought the entire thing was the value. If http://www.some-site.example/ is the value, just quote it like so:

url: "http://www.some-site.example/"

It depends on the parser, apparently. This didn't work with Jekyll YAML.
YAMLDotNet also renders quotes.
Nothing better? Because then quotes need to be escaped themselves, which does not solve the problem but simply moves it forward ...
Well, it would be much cooler to have an error-proof document, just like markdown, so the non-tech guys of the team can edit it (Eg. locale files in Rails) without any risk of breaking it!
@ivan_pozdeev: The quotes go around the entire string. - 'PS4="+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }"'
O
Oliver Salzburg

What also works and is even nicer for long, multiline texts, is putting your text indented on the next line, after a pipe or greater-than sign:

text: >
    Op dit plein stond het hoofdkantoor van de NIROM: Nederlands Indische 
    Radio Omroep

A pipe preserves newlines, a gt-sign turns all the following lines into one long string.


...and a newline is added at the end, which is usually not what you want.
@equaeghe: You can use >- or |- in order to prevent this.
This is a wonderful solution. Completely avoids having to escape other characters in your text. +++90000 points
is it possible to have a list of multilines somehow? I've tried - > but the items after the first are ignored.
@ffghfgh - urg! I can't figure out how to format the code properly in the comment and now I can't edit the original comment. Basically, I used a list item with a pipe, like this: - | and then on a new line I indented the list item text so that the first character lined up with the pipe. I hope that helps, it worked for me in a Rails 4.2 locale file.
S
Stephen Ostermiller

According to the YAML spec, neither the : nor the - should be a problem. : is only a key separator with a space after it, and - is only an array indicator at the start of a line with a space after it.

But if your YAML implementation has a problem with it, you potentially have lots of options:

- url: 'http://www.some-site.example/'
- url: "http://www.some-site.example/"
- url:
    http://www.some-site.example/
- url: >-
    http://www.some-site.example/
- url: |-
    http://www.some-site.example/

There is explicitly no form of escaping possible in "plain style", however.


The linter of travisCI complains about colons in unusual - lint.travis-ci.org
For Travis CI, it seems colons inside single quotes are OK.
Be caution when pasting a json in a yaml file. It's quite common to naively add a space after :.
S
Stephen Ostermiller

Quotes, but I prefer them on the just the value:

url: "http://www.some-site.example/"

Putting them across the whole line looks like it might cause problems.


p
ptomato

Another way that works with the YAML parser used in Jekyll:

title: My Life: A Memoir

Colons not followed by spaces don't seem to bother Jekyll's YAML parser, on the other hand. Neither do dashes.


… are character entities part of YAML? And is what Jekyll uses actually YAML?
Jekyll claims to use it: jekyllrb.com/docs/frontmatter I can't find anything about character entities in the YAML spec, so I suspect Jekyll is aberrant, but I think this answer serves well for people Googling "yaml escape colon" like me ;-)
A
Andy Brown

If you're using @ConfigurationProperties with Spring Boot 2 to inject maps with keys that contain colons then you need an additional level of escaping using square brackets inside the quotes because spring only allows alphanumeric and '-' characters, stripping out the rest. Your new key would look like this:

"[8.11.32.120:8000]": GoogleMapsKeyforThisDomain

See this github issue for reference.


This is the most helpful in my current use case. Thanks
H
Hein Andre Grønnestad

I came here trying to get my Azure DevOps Command Line task working. The thing that worked for me was using the pipe (|) character. Using > did not work.

Example:

steps:
- task: CmdLine@2
  inputs:
    script: |
      echo "Selecting Mono version..."
      /bin/bash -c "sudo $AGENT_HOMEDIRECTORY/scripts/select-xamarin-sdk.sh 5_18_1"
      echo "Selecting Xcode version..."
      /bin/bash -c "echo '##vso[task.setvariable variable=MD_APPLE_SDK_ROOT;]'/Applications/Xcode_10.2.1.app;sudo xcode-select --switch /Applications/Xcode_10.2.1.app/Contents/Developer"

I used same thing in .gitlab-ci.yml, but until I needed pipe '|' in script - it fails silently on it :(
This appears to have nothing to do with the question. The question is about colons and dashes in YAML. This looks like a solution to an entirely different problem.
@StephenOstermiller I don't agree that this is related to an entirely different problem. The answer adds additional information about a specific use case and based on the upvotes it has helped some people. When I Google for something and a related question shows up in the results, I find it very useful to share my solution so that when others search for the same thing, they can find the solution as well.
A
Alex Cohn

GitHub actions complain about

curl -L -H "Authorization: token ${{ secrets.TOKEN }}"  https://example.com/try.txt

but it's fine when there is no space after colon, like

curl -L -H "Authorization:token ${{ secrets.TOKEN }}"  https://example.com/try.txt

Use multi-line commands, either by starting with > or | and then write you command on the next line.
This appears to have nothing to do with the question. The question is about colons in YAML. This looks like a solution to an entirely different problem.