Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Add custom rules to redcloth/textile how?

Reply
Thread Tools

Add custom rules to redcloth/textile how?

 
 
Ingo Weiss
Guest
Posts: n/a
 
      02-18-2007
Hi,

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

Thanks!
Ingo

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

 
Reply With Quote
 
 
 
 
SonOfLilit
Guest
Posts: n/a
 
      02-18-2007
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 <> wrote:
> Hi,
>
> has anybody successfully added custom inline tag rules to
> RedCloth/textile?
>
> Thanks!
> Ingo
>
> --
> Posted via http://www.ruby-forum.com/.
>
>


 
Reply With Quote
 
 
 
 
SonOfLilit
Guest
Posts: n/a
 
      02-18-2007
Oh, and have a look at the adopt-a-newbie thread, here in ruby-talk


Aur Saraf

 
Reply With Quote
 
Ingo Weiss
Guest
Posts: n/a
 
      02-18-2007
Thanks so much!
Ingo

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

 
Reply With Quote
 
Ingo Weiss
Guest
Posts: n/a
 
      02-18-2007
> 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-forum.com/.

 
Reply With Quote
 
SonOfLilit
Guest
Posts: n/a
 
      02-18-2007
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 <> 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-forum.com/.
>
>


 
Reply With Quote
 
Ingo Weiss
Guest
Posts: n/a
 
      02-19-2007
> 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-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FxCop Custom Rules Implementation amitmnagarwal@gmail.com ASP .Net 0 07-21-2008 01:46 PM
Can we override the Authorization Module to write custom access rules? (.NET 2.0) dorionda@gmail.com ASP .Net Security 3 03-10-2006 10:25 PM
rules for Cisco PIX 525 firewall rules KAS Cisco 2 10-02-2005 07:12 PM
System Requirements: Trying to add USB2 to be able to use iPOD and add DVD Burner core Computer Support 3 05-04-2005 10:09 AM
How to add shared assembly in the add reference babu dhayal via .NET 247 ASP .Net 2 08-17-2004 06:11 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57