[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

YAML/RedCloth Question

mikeleonard

2/20/2005 5:55:00 PM

Hello all,

I'm very new to Ruby, so appy-polly-loggies in advance if the answer to
this is obvious.

I have a file, qbf.yml, the contents of which are:

----------------qbf.yml----------------
title: "on dogs and foxes"
author: "q.b. foks, esq."
text: "the quick brown

fox jumps

over

the lazy dog"
---------------------------------------

I want to import it into a Ruby hash and use RedCloth to convert the
"text" portion to html. The code I'm using is this:

----------------qbf.rb----------------
require 'yaml'
require 'redcloth'

qbf=YAML::load(File.open('qbf.yml'))

puts RedCloth.new(qbf["text"]).to_html
--------------------------------------

The output I get from this is:

<p>the quick brown
fox jumps
over
the lazy dog</p>

Whereas, I would like:

<p>the quick brown</p>
<p>fox jumps</p>
<p>over</p>
<p>the lazy dog</p>

It looks like when I import the YAML, my paragraph breaks get eaten up.
I'd like to avoid this if at all possible. I'm sure the solution is
very simple, but I can't seem to get my head around it. Any help would
be greatly appreciated.

Thanks in advance,

Mike Leonard

4 Answers

Jamis Buck

2/20/2005 6:25:00 PM

0

On 02:59 Mon 21 Feb , mikeleonard wrote:
> Hello all,
>
> I'm very new to Ruby, so appy-polly-loggies in advance if the answer to
> this is obvious.
>
> I have a file, qbf.yml, the contents of which are:
>
> ----------------qbf.yml----------------
> title: "on dogs and foxes"
> author: "q.b. foks, esq."
> text: "the quick brown
>
> fox jumps
>
> over
>
> the lazy dog"
> ---------------------------------------
>
> I want to import it into a Ruby hash and use RedCloth to convert the
> "text" portion to html. The code I'm using is this:
>
> ----------------qbf.rb----------------
> require 'yaml'
> require 'redcloth'
>
> qbf=YAML::load(File.open('qbf.yml'))
>
> puts RedCloth.new(qbf["text"]).to_html
> --------------------------------------
>
> The output I get from this is:
>
> <p>the quick brown
> fox jumps
> over
> the lazy dog</p>
>
> Whereas, I would like:
>
> <p>the quick brown</p>
> <p>fox jumps</p>
> <p>over</p>
> <p>the lazy dog</p>
>
> It looks like when I import the YAML, my paragraph breaks get eaten up.
> I'd like to avoid this if at all possible. I'm sure the solution is
> very simple, but I can't seem to get my head around it. Any help would
> be greatly appreciated.

Use triple line breaks to indicate paragraph breaks. It's just the way
YAML works:

1) A single newline gets eaten (resulting in one long line with no
newlines)

2) Two newlines get collapsed to one newline

3) Three newlines give you the expected blank line between paragraphs

Also, a syntax like this might read a little easier in your yaml file:

title: on dogs and foxes
author: q.b. foks, esq.
text: >-
the quick brown


fox jumps


over


the lazy dog

(Notice the quotes are almost always optional, except when there is
some question as to what format the data should be. Also, the >- after
text: says to eat all following lines wih a greater indentation level
and treat them as a single text value.)

Hope that helps,

Jamis

--
Jamis Buck
jamis_buck@byu.edu
http://jamis.jam...
------------------------------
"I am Victor of Borge. You will be assimil-nine-ed."



why the lucky stiff

2/20/2005 11:20:00 PM

0

Jamis Buck wrote:

> Use triple line breaks to indicate paragraph breaks. It's just the way
>
>YAML works:
>
>
Unless you use a literal block.

----------------qbf.yml----------------
title: "on dogs and foxes"
author: "q.b. foks, esq."
text: |
the quick brown

fox jumps

over

the lazy dog
---------------------------------------

Double-quoted multiline strings act like folded blocks (the rules Jamis described).

See 'Blocks' in the YAML Cookbook. <http://yaml4r.sourceforge.net/cookbook/#...

_why



Jamis Buck

2/20/2005 11:40:00 PM

0

On 08:19 Mon 21 Feb , why the lucky stiff wrote:
> Jamis Buck wrote:
>
> >Use triple line breaks to indicate paragraph breaks. It's just the way
> >
> >YAML works:
> >
> >
> Unless you use a literal block.
>
> ----------------qbf.yml----------------
> title: "on dogs and foxes"
> author: "q.b. foks, esq."
> text: |
> the quick brown
>
> fox jumps
>
> over
>
> the lazy dog
> ---------------------------------------
>
> Double-quoted multiline strings act like folded blocks (the rules Jamis
> described).
>
> See 'Blocks' in the YAML Cookbook.
> <http://yaml4r.sourceforge.net/cookbook/#...

Gah! Now *that's* something I wish I'd known six months ago. Would
have made it much less painful to do the Needle/Net::SSH/etc.
documentation in YAML.

Thanks for the tip, _why.

- Jamis

--
Jamis Buck
jamis_buck@byu.edu
http://jamis.jam...
------------------------------
"I am Victor of Borge. You will be assimil-nine-ed."



mikeleonard

2/21/2005 9:50:00 PM

0

Thanks for all the fast responses. I have very little programming
experience (just a few measly Bash scripts and an attempt to learn
Python a couple years ago), but something seems to have clicked for me
with Ruby. From Why's (Poignant) Guide and the archives of this group,
I've been able to figure enough to accomplish a bunch of little tasks
that were nagging at me. Can anyone recommend a good book? Is the
Pickaxe one pretty much the standard?

Thanks again,

Mike