This project is read-only.
Note: This page hasn't been reviewed for correctness yet

Minute Three: Munching On Blocks

Now that you know exactly how to use the basic structure of blocks in YAML, you can begin deciding whether you want to keep extra spacing and newlines or lose them altogether.

YAML provides a keep and a chomp operator, the + and - symbols respectively. Here's an example of how to use them in a literal block.

---
normal: |
  This has one newline.

kept: |+
  This has three newlines.

chomped: |-
  This has no newlines.

...

normal behaves as we would expect it. It would look something like this, because it preserves the final newline and ignores the others.

"This has one newline.
" 

kept is our first example of the keep operator. The keep operator tells YAML to keep the final newline as well as any extra newlines. So, kept would look like this.

"This has three newlines.

" 

Finally, chomped illustrates our first usage of the chomp operator. The chomp operator tells YAML to remove all newlines, including the final newline. So, chomped looks like this.

"This has no newlines."

At the end of our YAML document, we've added another new element, which looks exactly like an ellipsis. The ... tells YAML that the document is ended. It's not required, but it helps show that there's extra blank lines in our document.

Folded Blocks

In YAML, you can also apply the keep and chomp operators to a folded block. Here's another example, only slight different than the last one.

---
normal: >
  This has one newline.

kept: >+
  This has three newlines.

chomped: >-
  This has no newlines.

...

The first one, normal, works like expected. It has the final newline, and nothing more. It looks just like this.

"This has one newline.
" 

kept also behaves exactly as in the literal block, and preserves all the trailing newlines. It looks like this.

"This has three newlines.
" 

once again, chomped behaves as in the previous example, stripping all newlines. chomped looks like this.

"This has no newlines."

One Last Trick

YAML however still has one more whitespace trick up its little sleeves, this being the preservation of extra whitespace, besides newlines. Here's an example.

---
literal example: |+
  This has three newlines with extra spaces.

folded example: >+
  This also has three newlines with extra spaces.

...


This example is a little more difficult to follow, as the extra spaces are invisible, but take our word for it, there are two extra spaces on the final line of each block, before the newline. (There's no way for you to verify this because the Wiki software seems to strip the spaces.)

A literal example would show up as this, because a literal block still separates each line.

"This has three newlines with extra spaces.
" 

"
" 

"  
" 


A folded example would show up as this, because it concatenates each line.

}}
"This also has three newlines with extra spaces.
"
}}

That's essentially all the block tricks that YAML has to offer for now. Time to move onward.


Almost there: MinuteNine

Last edited Jan 20, 2009 at 6:54 PM by BurtHarris, version 1

Comments

No comments yet.