The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Using RedCloth 3

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
Using RedCloth 3 Posted: Feb 18, 2005 1:45 PM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Red Handed.
Original Post: Using RedCloth 3
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

Yeah, I get a lot of e-mail about problems with RedCloth. I think the main problem is that I’ve neglected to explain things. I’ve only just recently update the documentation and it can be a bit much to sort through. Especially when you really only need to know a couple of things about RedCloth 3.

Getting Textile and Markdown to Hold Hands

RedCloth 3 supports both Textile and Markdown. Well, there’s a few Markdown items that haven’t been implemented. I believe lists, backticked code fragments, and images still need to be finished.

By default, these are supported in tandem, with preference given to Textile markup. This is only because RedCloth is historically a Textile library.

However, preference can be given to Markdown.

  RedCloth::DEFAULT_RULES.replace [:markdown, :textile]

You can also use only one of the processors, if you prefer.

This can also be done when you go to output:

 >> r = RedCloth( <> # testing
 >> RED
 => "
    \n\t
  1. testing
  2. \n\t
" >> r = RedCloth( <> # testing >> RED => "

testing

"

Furthermore, you can mix-and-match rules from either processor to form your own custom markups.

 >> r = RedCloth( <       :block_markdown_atx, :inline_textile_link 
The complete list of rules is available in the RDoc for the RedCloth#rules accessor.

Line Breaking with a Sledgehammer

Since RedCloth 3 supports both Textile and Markdown, there’s a bit of clash in some subtle rules. For example, the string 4 > 5 is handled differently.

Textile outputs

4 > 5

.

Markdown outputs

4 > 5

.

Generally, I side with Textile on these subtelties. Eventually, I hope that these behaviors can be placed in rules like the above. But I end up sort of stumbling across them inadvertantly, you know?

With line breaks, I decided to side with Markdown. I got so many complaints with line breaks converting to
tags in RedCloth 2. People just didn’t expect it.

Heh, well, now people are used to it and they’re pretty disoriented using RedCloth 3! Anyway, this is an easy one.

 class RedCloth
   def hard_breaks; true; end
 end

This will activate classic hard-breaking globally, like in the days of RedCloth 2’s salad days.

Again, can be done on a per-document basis.

 >> RedCloth.new( <> A short little paragraph
 >> of no consequence whatsoever.
 >> RED
 #=> 

A short, little paragraph of no consequence whatsoever.

>> RedCloth.new( <> A short little paragraph >> of no consequence whatsoever. >> RED #=>

A short, little paragraph
of no consequence whatsoever.

And, hey, I’m just doin what one guy thinks is best. If this stuff drives you nuts, I’m open to a reversal.

Adding Yer Custom Blocks

Everyone’s always asking for custom markup. The most common form being additional Textile prefixes.

Yo, that’s easy!!

 class CustomRedCloth < RedCloth
   def textile_code( tag, atts, cite, content )
     "\t
#{ content }
" end end

Creating textile block prefixes is simply a matter of adding a textile_#{ prefix } method. We now have a code tag.

 >> CustomRedCloth.new( <> code. # testing
 >>   a = 1
 >>   b = 2
 >>   c = 3
 >> RED
 
 => 
# testing
      a = 1
      b = 2
      c = 3

You can add your own markup rules as well, but it gets a bit more complicated and I’d encourage reading the source. It’s a matter of adding a method and a rule of the same name. Take a look at the block_markdown_setext or block_textile_prefix. Short six- or seven-line methods for adding more complex markup.

Read: Using RedCloth 3

Topic: I'm published! Previous Topic   Next Topic Topic: RubyToRuby

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use