[英]Pandoc Lua filters: how to specify attributes for Span element
I have a Markdown document containing raw LaTeX commands. 我有一个包含原始LaTeX命令的Markdown文档。 I am trying to use a Lua filter with Pandoc (2.0.1.1) to convert the LaTeX commands into something more portable. 我正在尝试使用带有Pandoc(2.0.1.1)的Lua过滤器将LaTeX命令转换为更便携的东西。 In particular, commands that specify the language of text should be converted into spans with a lang
attribute. 特别是,指定文本语言的命令应转换为具有lang
属性的跨度。 The problem is that I don't know how to pass the attributes to the pandoc.Span
constructor. 问题是我不知道如何将属性传递给pandoc.Span
构造函数。 This is my attempt at a filter ( filter.lua
): 这是我尝试过滤器( filter.lua
):
function RawInline(elem)
if elem.format == "tex" then
text = string.match(elem.text, "\\textspanish{(.+)}")
if text then
contents = {pandoc.Str(text)}
attrs = pandoc.Attr("",{},{lang = "es-SP"})
return pandoc.Span(contents, attrs)
end
else
return elem
end
end
Sample usage: 样品用法:
echo '\textspanish{hola}' | pandoc -f markdown -t native --lua-filter=filter.lua
The output is [Para [Span ("",[],[]) [Str "hola"]]]
, with no attributes on the span. 输出为[Para [Span ("",[],[]) [Str "hola"]]]
,跨度上没有属性。
If I pass a name and/or class to pandoc.Attr
, these come through, eg, attrs = pandoc.Attr("name",{"class"},{lang = "es-SP"})
produces [Para [Span ("name",["class"],[]) [Str "hola"]]]
. 如果我将名称和/或类传递给pandoc.Attr
,这些来自,例如, attrs = pandoc.Attr("name",{"class"},{lang = "es-SP"})
产生[Para [Span ("name",["class"],[]) [Str "hola"]]]
。 But attributes I pass to the constructor never appear in the output. 但是我传递给构造函数的属性永远不会出现在输出中。 What is the right way to pass attributes to pandoc.Attr
? 将属性传递给pandoc.Attr
的正确方法是什么?
You found one of the rough edges in the lua filter implementation; 你找到了lua过滤器实现中的一个粗糙边缘; this should be ironed out and be made more user friendly. 这应该被解决,并使用户更友好。
The current implementation uses two-element tables to hold key-value pairs: 当前实现使用两元素表来保存键值对:
attrs = pandoc.Attr("", {}, {{"lang", "es-SP"}})
This is not a great way to represent pairs. 这不是表示对的好方法。 The reason for the current implementation is two-fold: 目前实施的原因有两个:
The last part is important when one wants to guarantee that the order of attributes won't be changed when passing through a filter. 当人们想要保证在通过过滤器时不会改变属性的顺序时,最后一部分很重要。 There is no rule in lua which determines the order of keys in a table: the lua table {one = 1, two = 2}
could be read back into pandoc as the attribute list {one="1" two="2}
or as {two="2" one="1"}
. Now, the order of attributes shouldn't matter for most applications, but we cannot be sure. Hence the less-than-intuitive representation. lua中没有规则确定表中键的顺序:lua表{one = 1, two = 2}
可以作为属性列表{one="1" two="2}
读回到pandoc中as {two="2" one="1"}
。现在,属性的顺序对于大多数应用程序而言并不重要,但我们无法确定。因此,不太直观的表示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.