在 ConTeXt mkiv 中,我如何注册回调?callback.register()
什么都不做(并且 IIRC 大多数回调无论如何都是冻结的)并且在某处我发现tasks.appendaction
,但这给了我一个tasks
未定义的错误(nil)。
我想在post_linebreak_filter
回调中添加一些内容。
编辑:感谢 Khaled 的回答和邮件列表的帮助,我现在有了解决方案。关键是创建我自己的命名空间:
moduledata.mystuff={}
并添加我的功能:
moduledata.mystuff.show_hyph = show_hyph
并将该函数注册到“finalizers”回调中,这与post_linebreak_filter
ConTeXt 中的功能等效。
nodes.tasks.appendaction("finalizers","after","moduledata.mystuff.show_hyph")
看我的答案完整示例
答案1
应该是nodes.tasks.appendaction()
,我不知道官方的使用方法,但经过反复试验(并通过源代码进行 grepping),我想出了:
bidi.handle_bidi = nodes.installattributehandler {
name = "bidi",
namespace = bidi,
processor = bidi.ctxprocess,
}
nodes.tasks.appendaction("processors", "characters", "bidi.handle_bidi")
和:
nodes.tasks.enableaction("processors", "bidi.handle_bidi")
nodes.tasks.disableaction("processors", "bidi.handle_bidi")
(摘自我的bidi
模块,这相当于pre_linepreak_filter
)
最好向汉斯寻求一个明确的答案。
答案2
我已经采取了来自 LuaTeX wiki 的示例并在 Khaled 和 Hans 的帮助下将其转换为 ConTeXt 代码:
\mainlanguage[de]
\de
\startluacode
moduledata.mystuff={}
show_hyph = function(head)
while head do
if head.id == 0 or head.id == 1 then -- % hlist, vlist
show_hyph(head.list) -- % head.head in LuaTeX > 0.65
elseif head.id == 7 then -- % disc
local n = node.new("whatsit","pdf_literal")
n.mode = 0
n.data = "q 0.3 w 0 2 m 0 7 l S Q"
n.next = head.next
n.prev = head
head.next = n
head = n
end
head = head.next
end
return true
end
moduledata.mystuff.show_hyph = show_hyph
nodes.tasks.appendaction("finalizers","after","moduledata.mystuff.show_hyph")
\stopluacode
\starttext
Seit zwei Jahren...
\stoptext
“终结器”是回调的 ConTeXt 等价物post_linebreak_filter
。