# Add custom rules to redcloth/textile how?

**URL:** <https://www.ruby-forum.com/t/add-custom-rules-to-redcloth-textile-how/85957>\
**Category:** Ruby\
**Created:** [February 18, 2007, 4:01am UTC](https://www.ruby-forum.com/t/add-custom-rules-to-redcloth-textile-how/85957 "2007-02-18T04:01:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [February 18, 2007, 4:01am UTC](https://www.ruby-forum.com/t/add-custom-rules-to-redcloth-textile-how/85957/1 "2007-02-18T04:01:41Z")

</div>

Hi,

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

Thanks!  
Ingo

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [February 18, 2007, 12:54pm UTC](https://www.ruby-forum.com/t/add-custom-rules-to-redcloth-textile-how/85957/2 "2007-02-18T12:54:19Z")

</div>

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 S.

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [February 18, 2007, 12:55pm UTC](https://www.ruby-forum.com/t/add-custom-rules-to-redcloth-textile-how/85957/3 "2007-02-18T12:55:19Z")

</div>

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

Aur S.

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [February 18, 2007, 7:15pm UTC](https://www.ruby-forum.com/t/add-custom-rules-to-redcloth-textile-how/85957/4 "2007-02-18T19:15:03Z")

</div>

Thanks so much!  
Ingo

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [February 18, 2007, 7:16pm UTC](https://www.ruby-forum.com/t/add-custom-rules-to-redcloth-textile-how/85957/5 "2007-02-18T19:16:18Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [February 19, 2007, 1:51am UTC](https://www.ruby-forum.com/t/add-custom-rules-to-redcloth-textile-how/85957/6 "2007-02-19T01:51:14Z")

</div>

> 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: content  
“\<abbr class=“geo” title=”#{lat};#{lng}"\>#{content}"  
end  
end

def to\_html  
super(\*RULES)  
end

end

---

<div class="post-metadata">

**Author:** ![ingoweiss](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@ingoweiss](https://www.ruby-forum.com/u/ingoweiss)\
**Post date:** [February 18, 2007, 8:00pm UTC](https://www.ruby-forum.com/t/add-custom-rules-to-redcloth-textile-how/85957/7 "2007-02-18T20:00:01Z")

</div>

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
