[luatex] multiple callbacks

Hans Hagen pragma at wxs.nl
Sun Jan 4 17:01:41 CET 2009


Taco Hoekwater wrote:
> Arthur Reutenauer wrote:
>>> untested, but something like this can do the job (untested)
>>
>>   Looks cool.  Élie, can you test it and make a small proof of concept
>> with a simple callback?
> 
> Keep in mind that not all callbacks have the same i/o characteristics.
> For example, 'process_input_buffer' has:
> 
>   function(<string> buffer)
>     return <string> adjusted_buffer
>   end
> 
> so if you chain processing functions, you will want to feed the
> output of earlier functions into later functions (otherwise, the net
> effect is exactly the same as running only the last function).

sure, this is why i called it listhandler, one can also have

function callback.datahandler(data)
     return function(data,...)
         for _, func in ipairs(list[name]) do
             data = func(data,...)
         end
         return data
     end
end

but in this case looks quite the same; in both cases func is supposed to 
return something

Anyhow, here is a test:

local list = {}

function callback.add(name,func,priority)
     list[name] = list[name] or { }
     if not priority then
         table.insert(list[name],func)
     elseif priority < 1 then
         table.insert(list[name],1,func)
     elseif priority > #list[name] then
         table.insert(list[name],func)
     else
         table.insert(list[name],priority,func)
     end
end

function callback.listhandler(name)
     return function(head,...)
         local done = false
         for _, func in ipairs(list[name]) do
             head, ok = func(head,...)
             if ok then done = true end
         end
         return head, done
     end
end

function callback.datahandler(data)
     return function(data,...)
         for _, func in ipairs(list[name]) do
             data = func(data,...)
         end
         return data
     end
end

callback.add("lists",function(head,...)
     texio.write_nl("I'm number 1")
     return head, true
end,1)

callback.add("lists",function(head,...)
     texio.write_nl("I'm number 2")
     return head, true
end,2)

callback.add("lists",function(head,...)
     texio.write_nl("No, I'm number 1")
     return head, true
end,1)

callback.register("hpack_filter",callback.listhandler("lists"))

this will give ..

No, I'm number 1
I'm number 1
I'm number 2


-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
      tel: 038 477 53 69 | fax: 038 477 53 74 | www.pragma-ade.com
                                              | www.pragma-pod.nl
-----------------------------------------------------------------


More information about the luatex mailing list