Typography is the craft of endowing human language with a durable visual form, and thus with an independent existence. (...) Typography remains a source of true delight, true knowledge, true surprise.

Robert Bringhurst, The Elements of Typographic Style

April 19, 2008

Ruby on Rails

(No comments)

The example on custom textile tags, given at the nubyonrails page is very well written, but left some individuals with the question, how to do custom inline tags. After some searching I found at least one reference in some forum, which I used to build the following code. What would one need a custom inline tag for, you might ask? In my case, I wanted a very simple way of referring to pages in a RoR mini-CMS I had built. To make abbreviated link names possible, a search action seemed appropriate. This means that something like ?"Lot 5"? would translate into <a href=”/titlesearch?searchterm=Lot 5″>Lot 5</a>. Using a class extending RedCloth already, all I had to do was to add the following lines of code:

RULES = [:inline_textile_search, :refs_textile, :block_textile_table,
:block_textile_lists, :block_textile_prefix, :inline_textile_image,
:inline_textile_link, :inline_textile_code, :inline_textile_span,
:glyphs_textile ]

# render internal links as title search links

def inline_textile_search(text)
    text.gsub!(/(?"?)([w|s]+?)("?)/) do |m|
        content = $~[2]
        "<a href="/titlesearch?searchterm=#{content}">#{content}</a>"
    end
end

def to_html
    super(*RULES)
end

I should mention that this solution is far from being perfect (I am overwriting the RULES, taken from the current version of RedCloth, to add my tag method). At the same time, it works the way I wanted it to.