[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Add custom rules to redcloth/textile how?

Ingo Weiss

2/18/2007 3:02:00 AM

Hi,

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

Thanks!
Ingo

--
Posted via http://www.ruby-....

6 Answers

SonOfLilit

2/18/2007 11:53:00 AM

0

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

On 2/18/07, Ingo Weiss <ingoweiss@gmail.com> wrote:
> Hi,
>
> has anybody successfully added custom inline tag rules to
> RedCloth/textile?
>
> Thanks!
> Ingo
>
> --
> Posted via http://www.ruby-....
>
>

SonOfLilit

2/18/2007 11:54:00 AM

0

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


Aur Saraf

Ingo Weiss

2/18/2007 6:15:00 PM

0

Thanks so much!
Ingo

--
Posted via http://www.ruby-....

Ingo Weiss

2/18/2007 6:16:00 PM

0

> 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

--
Posted via http://www.ruby-....

SonOfLilit

2/18/2007 6:59:00 PM

0

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

On 2/18/07, Ingo Weiss <ingoweiss@gmail.com> wrote:
> > 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
>
> --
> Posted via http://www.ruby-....
>
>

Ingo Weiss

2/19/2007 12:51:00 AM

0

> 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



--
Posted via http://www.ruby-....