>>>>> "M" == Mikkel Bruun <mikkel.bruun / valtech.dk> writes:

M> So, the .handle method should go instead of method, the symbol and the
M> string should be contained in args.
M> *block should be puts.

 This is just an example, it's stupid but it's normal :-)

pigeon% cat tt.c
#include <ruby.h>

extern VALUE rb_defout;
static VALUE tt_cDispatch;

static VALUE
tt_iter(tmp)
    VALUE *tmp;
{
    return rb_funcall2(tmp[0], rb_intern("handle"), 2, tmp + 1);
}

static VALUE
tt_yield(element)
    VALUE element;
{
    return rb_funcall2(rb_defout, rb_intern("puts"), 1, &element);
}

static VALUE
tt_foo(obj, xml)
    VALUE obj, xml;
{
    VALUE tmp[3];

    xml = rb_str_to_str(xml);
    tmp[0] = rb_funcall2(tt_cDispatch, rb_intern("new"), 1, &xml);
    tmp[1] = ID2SYM(rb_intern("start_element"));
    tmp[2] = rb_ary_new3(1, rb_str_new2("chapter"));
    rb_iterate(tt_iter, (VALUE)tmp, tt_yield, 0);
    return rb_funcall2(tmp[0], rb_intern("start"), 0, 0);
}

void Init_tt()
{
    VALUE tt_cTt;

    rb_require("nqxml/dispatcher");
    tt_cDispatch = rb_const_get(rb_const_get(rb_cObject, rb_intern("NQXML")),
                                rb_intern("Dispatcher"));
    tt_cTt = rb_define_class("Tt", rb_cObject);
    rb_define_singleton_method(tt_cTt, "foo", tt_foo, 1);
}
pigeon% 

pigeon% cat b.rb
#!/usr/bin/ruby
require 'tt'

xml = '<?xml version="1.0"?>
<!DOCTYPE book SYSTEM "foo.bar">
<book>My Book
  <!-- My Book, which is mine, is a book. That is mine. -->
  <intro>
    <p>This is the intro to my book,which has<bold>bold ideas</bold>
    </p>
  </intro>
  <chapter>
  <p>This is the first chapter.  It has <bold>bold text</bold></p>
  <note>Should match chapter/*</note>
  </chapter>
  <appendix>
    <chapter>a chapter in the appendix
      <figure>Should match chapter/*</figure>
    </chapter>
  </appendix>
</book>

<chapter>
  <p>Should match chapter/*</p>
</chapter>
'

Tt::foo(xml)
pigeon% 

pigeon% b.rb
<chapter>
<chapter>
<chapter>
pigeon% 


Guy Decoux