2

I have with RedCloth saved plain text in a form and converted it to HTML. For example, writing this in my form, and saving it, would make it display the exact same way I wrote it :

This sentence
gets inserted into it
proper html syntax
to preserve line breakage.

With this code :

def parse_code
  self.text = RedCloth.new(text).to_html
end

And then I can redisplay it with this :

= raw post.text

But when I want to edit it, it it returns to me as :

<p>This sentence</p>
<p>gets inserted into it</p>
<p>proper html syntax</p>
<p>to preserve line breakage</p>

How can I make it, so that when I edit it, it looks the same way it did before I went and saved it ?

Thanks!

Trip
  • 26,756
  • 46
  • 158
  • 277

3 Answers3

3

I would leave the textile code stored in textile and do the conversion to HTML only in the view:

= raw RedCloth.new(@post.text).to_html

Converting between textile and HTML does not feel to be a good practice. Your parse_code method seem that it caused your text to be converted to HTML.. and than stored to the Db.

However if you want to convert HTML to textile, maybe clothred is for you or read this blog.

fifigyuri
  • 5,771
  • 8
  • 30
  • 50
  • This would be the ideal correct answer, imo. If you have a ton of pre-existing data stored as HTML though, see my solution. – Chuck Callebs Dec 08 '10 at 20:37
  • The trouble with this is that if I save it without adding RedCloth html into it, then it will save as a solid string, and later I will not be able to add page breaks..right? Or am I wrong, i'm not sure.. – Trip Dec 10 '10 at 22:20
  • Wow I got it! Thanks fifyurgiri. I wish I had your answer before I started RedCloth. Works like a charm. – Trip Dec 11 '10 at 00:08
1

Edit: Shoot! I misunderstood the question!

You'd assign that text area's value back to textile using ClothRed:

ClothRed.new(html).to_textile

Sorry!

Chuck Callebs
  • 16,293
  • 8
  • 56
  • 71
  • Hey Chuck, thanks for the answer, but how would I implement your response when my form says `= f.text_area :text` – Trip Dec 08 '10 at 19:42
  • Updated my answer. I misunderstood what you were going for. I'm sorry! – Chuck Callebs Dec 08 '10 at 19:54
  • Chuck, you're the best! Thank you. I have to do a little research to make this make sense. I'm thinking of the whole task now. Saving the data with the formatted text, and then retrieving the data formatted but without tags in an editable text area. "ClothRed"? What a creative name.. Haha. – Trip Dec 09 '10 at 03:18
  • would you, by any chance, know how to implement your answer in a form_for where I instantiate the part needed to ClothRed as `f.text_area :text` ? I can't seem to figure that part out.. – Trip Dec 10 '10 at 22:21
0

If I understood you right, you are storing the HTML output in the database. Instead of doing that, store the raw Textile contents and then convert them to HTML when showing it to the user.

Veeti
  • 5,270
  • 3
  • 31
  • 37
  • Ah interesting. Not to get too off topic, but then how would I save it formatted, and be able to retrieve it again? Or better yet is there a blog on this? I can't seem to find one. Thanks Veeti! – Trip Dec 09 '10 at 03:16