Add custom rules to redcloth/textile how?

Discussion in 'Ruby' started by Ingo Weiss, Feb 18, 2007.

  1. Ingo Weiss

    Ingo Weiss Guest

    Hi,

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

    Thanks!
    Ingo

    --
    Posted via http://www.ruby-forum.com/.
     
    Ingo Weiss, Feb 18, 2007
    #1
    1. Advertising

  2. Ingo Weiss

    SonOfLilit Guest

    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/.
    >
    >
     
    SonOfLilit, Feb 18, 2007
    #2
    1. Advertising

  3. Ingo Weiss

    SonOfLilit Guest

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


    Aur Saraf
     
    SonOfLilit, Feb 18, 2007
    #3
  4. Ingo Weiss

    Ingo Weiss Guest

    Ingo Weiss, Feb 18, 2007
    #4
  5. Ingo Weiss

    Ingo Weiss Guest

    > 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/.
     
    Ingo Weiss, Feb 18, 2007
    #5
  6. Ingo Weiss

    SonOfLilit Guest

    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/.
    >
    >
     
    SonOfLilit, Feb 18, 2007
    #6
  7. Ingo Weiss

    Ingo Weiss Guest

    > 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/.
     
    Ingo Weiss, Feb 19, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. why the lucky stiff
    Replies:
    0
    Views:
    116
    why the lucky stiff
    Feb 6, 2004
  2. why the lucky stiff
    Replies:
    0
    Views:
    97
    why the lucky stiff
    Apr 2, 2004
  3. why the lucky stiff
    Replies:
    0
    Views:
    98
    why the lucky stiff
    Apr 8, 2004
  4. why the lucky stiff
    Replies:
    0
    Views:
    107
    why the lucky stiff
    Apr 14, 2004
  5. why the lucky stiff
    Replies:
    0
    Views:
    99
    why the lucky stiff
    Apr 16, 2004
Loading...

Share This Page