Topic: Pass variable to textilize

I'm using textile in an editor, and I am trying to add an additional tag to the link it creates, e.g from:

<a href="/some/address/">Link1</a>

<a href="/some/address/" class="test Link1">Link1</a>

I have edited the redcloth.rb so it can display:
<a href="/some/address/" class="Link1">Link1</a>

..but I am not sure how to get the 'test' in there, test is a variable and is dependant upon the field I am editing.

This is the edited portion of redcloth.rb:

    def inline_textile_link( text ) 
        text.gsub!( LINK_RE ) do |m|
            pre,atts,text,title,url,slash,post = $~[1..7]

            url, url_title = check_refs( url )
            title ||= url_title
            atts = pba( atts )
            atts = " href=\"#{ url }#{ slash }\"#{ atts }"
            atts << " title=\"#{ title }\"" if title
            atts = shelve( atts ) if atts
            "#{ pre }<a#{ atts } class=\"#{ text }\">#{ text }</a>#{ post }"

Any ideas?


Last edited by JasonK (2006-08-15 13:53:30)

Re: Pass variable to textilize

I'm assuming you want to change the class so you can display the links differently using CSS. How about just wrapping the textile output in a div with a class/id so CSS can determine the proper styling through that. It's easier to explain through an example:

<div class="test">
<%= textilize(content) %>

.test a { /* links inside test div */
  color: red;

Seems a lot easier than trying to change RedCloth's internals.

Railscasts - Free Ruby on Rails Screencasts

Re: Pass variable to textilize

I should have been more specific, I used class as an example so as not to confuse, in reality it is a custom alias tag that needs to be applied for tracking purposes. (Ugly I know, but unfortunately neccessary).
<a href="/some/address/" alias="test Link1">Link1</a>

Within the paragraph being edited there maybe several links, they all need to share the same first part of the alias tag ('test' in the example), which is based off a variable representing field being edited, the second part is simply the text portion of the link.

Thanks for the help though!

Re: Pass variable to textilize

You can set up an instance variable accessor in RedCloth and then make a helper method for setting it.

class RedCloth
  attr_accessor :current_field

def textilize_with_field(content, field = nil)
  red_cloth =
  red_cloth.current_field = field

Then you can access the variable in the inline_textile_link method with @current_field. Will that work?

Railscasts - Free Ruby on Rails Screencasts