tags:

views:

111

answers:

3

An ampersand at the start of a YAML entry is normally seen as a label for a set of data that can be referenced later. How do you escape a legitimate ampersand at the start of a YAML entry. For example:

---
- news:
    news_text: “Text!’

I am looking to not have &ldquo be a label within the yaml file, but rather when I get parse the YAML file to have the news_text come back with the “ in the entry.

+4  A: 

Just put quotes around the text

require 'yaml'

data = <<END
---
- news:
    news_text: "&ldquo;Text!&rsquo;"
END

puts YAML::load(data).inspect

# produces => [{"news"=>{"news_text"=>"&ldquo;Text!&rsquo;"}}]
dan
A: 

Putting the entire string in single quotes would do what you want:

---
- news:
    news_text: '&ldquo;Text!&rsquo;'

But, I think that any yaml library should be smart enough to do that for you?

pioto
+1  A: 

You probably can enclose the text in quotes:

---
- news:
    news_text: "&ldquo;Text!&rsquo;"

Besides, you can probably just as well use the proper characters there:

---
- news:
    news_text: “Text!’

Putting escapes specific to a totally different markup language into a document written in another markup language seems ... odd to me, somehow.

Joey