Add custom rules to redcloth/textile how?

I

Ingo Weiss

Hi,

has anybody successfully added custom inline tag rules to
RedCloth/textile?

Thanks!
Ingo
 
S

SonOfLilit

Here is all the code implementing one of the default rules.

To create custom rules would be simple:

class RedCloth
HYPERLINK = '(\S+?)([^\w\s/;=\?]*?)(?=\s|<|$)'

TEXTILE_REFS_RE = /(^ *)\[([^\n]+?)\](#{HYPERLINK})(?=\s|$)/

def refs_textile( text )
text.gsub!( TEXTILE_REFS_RE ) do |m|
@urlrefs ||= {} # in RedCloth code, initialized in
initialize, but here I am giving an example of how to extend it

flag, url = $~[2..3]
@urlrefs[flag.downcase] = [url, nil]
nil
end
end
end

textile_rules = [: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]


redclothvar.to_html(textile_rules)

From here, figuring how to add a rule is really simple.

Define a method in class RedCloth with some RegExp action in it, call
#to_html passing an array which is a copy of the textile_rules array
in #to_html but with your rule added (array sets precedence, so it
counts where your rule is).


Reading _why's code is a pleasure, you should try it once.

Careful: It might blow your mind and send you flying in a three headed
truck with a bouncy marmoset. Specifically, beware of Camping. That is
code on a level that might get those of us with less self confidence
to decide that perhaps shoemaking is a more befitting career.

Aur Saraf
 
I

Ingo Weiss

Oh, and have a look at the adopt-a-newbie thread, here in ruby-talk :)

I aplologize if my question was too 'newbie' to send to the list. I
guess my judgement was a little off..

Ingo
 
S

SonOfLilit

It wasn't. It was a perfectly OK question to ask in my opinion.

We are doing our best to help newbies learn Ruby.

The only too-newby thing would be to post another question simply
solvable by looking at the code without looking at it (what I mean is
that everything is OK as long as you learn "to fish" and not just "eat
every fish I give out").
At least that's how I feel. Others on the list might feel differently,
also I see this list called in many places "the kindest list on the
internet' and I agree, form those I know.

My pointing to the adopt-a-newbie thread has nothing to do with that.

I was merely pointing to it because I think it would benefit you and
as the current guy-in-charge of it I'm trying to spread knowledge of
it's existence to those who might find it useful.

So really, have a look - it is exactly about this, about teaching
newbies where to find solutions and how to find themselves in the
world of Ruby :)

Aur
 
I

Ingo Weiss

I was merely pointing to it because I think it would benefit you and
as the current guy-in-charge of it I'm trying to spread knowledge of
it's existence to those who might find it useful.

Oh I see! Sorry for the confusion, and thanks again. Following your
hints I got it working! Below is my code in case this is useful for
someone struggling with the same problem. Beware, the regex is probably
VERY naive!

:) Ingo




class CustomRedCloth < RedCloth

RULES = [:inline_textile_geo, :inline_textile_span,
:inline_textile_link, :block_textile_lists, :block_textile_prefix ]

# render 'geo' microformat (abbr pattern)
def inline_textile_geo( text )
# this: "content":-71.34534/22.34760
text.gsub!( /"([^"]+)":(-?\d+\.\d+)\/(-?\d+\.\d+)/ ) do |m|
content,lat,lng = $~[1..3]
# becomes this: <abbr class="geo"
title="-71.34534;22.34760">content</abbr>
"<abbr class=\"geo\" title=\"#{lat};#{lng}\">#{content}</abbr>"
end
end

def to_html
super(*RULES)
end

end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top