git.iko.soy/typeable/pandoc
on master help sign in
Lua filters: allow passing of HTML-like tables instead of Attr (#5750)
d0261d7Albert Krewinkel
data/pandoc.lua+75−16
@@ -50,11 +50,15 @@ local utils = M.utils
50-- @param indices list of indices, starting with the most deeply nested
50-- @param indices list of indices, starting with the most deeply nested
51-- @return newly created function
51-- @return newly created function
52-- @local
52-- @local
53−function make_indexing_function(template, indices)
53+function make_indexing_function(template, ...)
54+ local indices = {...}
54 local loadstring = loadstring or load
55 local loadstring = loadstring or load
55 local bracketed = {}
56 local bracketed = {}
56 for i = 1, #indices do
57 for i = 1, #indices do
57− bracketed[i] = string.format('[%d]', indices[#indices - i + 1])
58+ local idx = indices[#indices - i + 1]
59+ bracketed[i] = type(idx) == 'number'
60+ and string.format('[%d]', idx)
61+ or string.format('.%s', idx)
58 end
62 end
59 local fnstr = string.format('return ' .. template, table.concat(bracketed))
63 local fnstr = string.format('return ' .. template, table.concat(bracketed))
60 return assert(loadstring(fnstr))()
64 return assert(loadstring(fnstr))()
@@ -69,11 +73,16 @@ local function create_accessor_functions (fn_template, accessors)
69 local res = {}
73 local res = {}
70 function add_accessors(acc, ...)
74 function add_accessors(acc, ...)
71 if type(acc) == 'string' then
75 if type(acc) == 'string' then
72− res[acc] = make_indexing_function(fn_template, {...})
76+ res[acc] = make_indexing_function(fn_template, ...)
73 elseif type(acc) == 'table' and #acc == 0 and next(acc) then
77 elseif type(acc) == 'table' and #acc == 0 and next(acc) then
78+ -- Named substructure: the given names are accessed via the substructure,
79+ -- but the accessors are also added to the result table, enabling direct
80+ -- access from the parent element. Mainly used for `attr`.
74 local name, substructure = next(acc)
81 local name, substructure = next(acc)
75− res[name] = make_indexing_function(fn_template, {...})
82+ res[name] = make_indexing_function(fn_template, ...)
76− add_accessors(substructure, ...)
83+ for _, subname in ipairs(substructure) do
84+ res[subname] = make_indexing_function(fn_template, subname, ...)
85+ end
77 else
86 else
78 for i = 1, #(acc or {}) do
87 for i = 1, #(acc or {}) do
79 add_accessors(acc[i], i, ...)
88 add_accessors(acc[i], i, ...)
@@ -272,6 +281,35 @@ local function ensureDefinitionPairs (pair)
272 return {inlines, blocks}
281 return {inlines, blocks}
273end
282end
274
283
284+--- Split a string into it's words, using whitespace as separators.
285+local function words (str)
286+ local ws = {}
287+ for w in str:gmatch("([^%s]+)") do ws[#ws + 1] = w end
288+ return ws
289+end
290+
291+--- Try hard to turn the arguments into an Attr object.
292+local function ensureAttr(attr)
293+ if type(attr) == 'table' then
294+ if #attr > 0 then return M.Attr(table.unpack(attr)) end
295+
296+ -- assume HTML-like key-value pairs
297+ local ident = attr.id or ''
298+ local classes = words(attr.class or '')
299+ local attributes = attr
300+ attributes.id = nil
301+ attributes.class = nil
302+ return M.Attr(ident, classes, attributes)
303+ elseif attr == nil then
304+ return M.Attr()
305+ elseif type(attr) == 'string' then
306+ -- treat argument as ID
307+ return M.Attr(attr)
308+ end
309+ -- print(arg, ...)
310+ error('Could not convert to Attr')
311+end
312+
275------------------------------------------------------------------------
313------------------------------------------------------------------------
276--- Pandoc Document
314--- Pandoc Document
277-- @section document
315-- @section document
@@ -402,7 +440,7 @@ M.BulletList = M.Block:create_constructor(
402-- @treturn Block code block element
440-- @treturn Block code block element
403M.CodeBlock = M.Block:create_constructor(
441M.CodeBlock = M.Block:create_constructor(
404 "CodeBlock",
442 "CodeBlock",
405− function(text, attr) return {c = {attr or M.Attr(), text}} end,
443+ function(text, attr) return {c = {ensureAttr(attr), text}} end,
406 {{attr = {"identifier", "classes", "attributes"}}, "text"}
444 {{attr = {"identifier", "classes", "attributes"}}, "text"}
407)
445)
408
446
@@ -426,7 +464,7 @@ M.DefinitionList = M.Block:create_constructor(
426M.Div = M.Block:create_constructor(
464M.Div = M.Block:create_constructor(
427 "Div",
465 "Div",
428 function(content, attr)
466 function(content, attr)
429− return {c = {attr or M.Attr(), ensureList(content)}}
467+ return {c = {ensureAttr(attr), ensureList(content)}}
430 end,
468 end,
431 {{attr = {"identifier", "classes", "attributes"}}, "content"}
469 {{attr = {"identifier", "classes", "attributes"}}, "content"}
432)
470)
@@ -440,7 +478,7 @@ M.Div = M.Block:create_constructor(
440M.Header = M.Block:create_constructor(
478M.Header = M.Block:create_constructor(
441 "Header",
479 "Header",
442 function(level, content, attr)
480 function(level, content, attr)
443− return {c = {level, attr or M.Attr(), ensureInlineList(content)}}
481+ return {c = {level, ensureAttr(attr), ensureInlineList(content)}}
444 end,
482 end,
445 {"level", {attr = {"identifier", "classes", "attributes"}}, "content"}
483 {"level", {attr = {"identifier", "classes", "attributes"}}, "content"}
446)
484)
@@ -569,7 +607,7 @@ M.Cite = M.Inline:create_constructor(
569-- @treturn Inline code element
607-- @treturn Inline code element
570M.Code = M.Inline:create_constructor(
608M.Code = M.Inline:create_constructor(
571 "Code",
609 "Code",
572− function(text, attr) return {c = {attr or M.Attr(), text}} end,
610+ function(text, attr) return {c = {ensureAttr(attr), text}} end,
573 {{attr = {"identifier", "classes", "attributes"}}, "text"}
611 {{attr = {"identifier", "classes", "attributes"}}, "text"}
574)
612)
575
613
@@ -594,8 +632,7 @@ M.Image = M.Inline:create_constructor(
594 "Image",
632 "Image",
595 function(caption, src, title, attr)
633 function(caption, src, title, attr)
596 title = title or ""
634 title = title or ""
597− attr = attr or M.Attr()
598− return {c = {attr, ensureInlineList(caption), {src, title}}}
635+ return {c = {ensureAttr(attr), ensureInlineList(caption), {src, title}}}
599 end,
636 end,
600 {{attr = {"identifier", "classes", "attributes"}}, "caption", {"src", "title"}}
637 {{attr = {"identifier", "classes", "attributes"}}, "caption", {"src", "title"}}
601)
638)
@@ -619,7 +656,7 @@ M.Link = M.Inline:create_constructor(
619 "Link",
656 "Link",
620 function(content, target, title, attr)
657 function(content, target, title, attr)
621 title = title or ""
658 title = title or ""
622− attr = attr or M.Attr()
659+ attr = ensureAttr(attr)
623 return {c = {attr, ensureInlineList(content), {target, title}}}
660 return {c = {attr, ensureInlineList(content), {target, title}}}
624 end,
661 end,
625 {{attr = {"identifier", "classes", "attributes"}}, "content", {"target", "title"}}
662 {{attr = {"identifier", "classes", "attributes"}}, "content", {"target", "title"}}
@@ -743,7 +780,7 @@ M.Space = M.Inline:create_constructor(
743M.Span = M.Inline:create_constructor(
780M.Span = M.Inline:create_constructor(
744 "Span",
781 "Span",
745 function(content, attr)
782 function(content, attr)
746− return {c = {attr or M.Attr(), ensureInlineList(content)}}
783+ return {c = {ensureAttr(attr), ensureInlineList(content)}}
747 end,
784 end,
748 {{attr = {"identifier", "classes", "attributes"}}, "content"}
785 {{attr = {"identifier", "classes", "attributes"}}, "content"}
749)
786)
@@ -902,17 +939,21 @@ function M.Attr:new (identifier, classes, attributes)
902 identifier = identifier or ''
939 identifier = identifier or ''
903 classes = ensureList(classes or {})
940 classes = ensureList(classes or {})
904 attributes = setmetatable(to_alist(attributes or {}), AttributeList)
941 attributes = setmetatable(to_alist(attributes or {}), AttributeList)
905− return {identifier, classes, attributes}
942+ return setmetatable({identifier, classes, attributes}, self.behavior)
906end
943end
907M.Attr.behavior.clone = M.types.clone.Attr
944M.Attr.behavior.clone = M.types.clone.Attr
945+M.Attr.behavior.tag = 'Attr'
908M.Attr.behavior._field_names = {identifier = 1, classes = 2, attributes = 3}
946M.Attr.behavior._field_names = {identifier = 1, classes = 2, attributes = 3}
909M.Attr.behavior.__eq = utils.equals
947M.Attr.behavior.__eq = utils.equals
910M.Attr.behavior.__index = function(t, k)
948M.Attr.behavior.__index = function(t, k)
949+ return (k == 't' and t.tag) or
911− return rawget(t, getmetatable(t)._field_names[k]) or
950+ rawget(t, getmetatable(t)._field_names[k]) or
912 getmetatable(t)[k]
951 getmetatable(t)[k]
913end
952end
914M.Attr.behavior.__newindex = function(t, k, v)
953M.Attr.behavior.__newindex = function(t, k, v)
954+ if k == 'attributes' then
955+ rawset(t, 3, setmetatable(to_alist(v or {}), AttributeList))
915− if getmetatable(t)._field_names[k] then
956+ elseif getmetatable(t)._field_names[k] then
916 rawset(t, getmetatable(t)._field_names[k], v)
957 rawset(t, getmetatable(t)._field_names[k], v)
917 else
958 else
918 rawset(t, k, v)
959 rawset(t, k, v)
@@ -927,6 +968,24 @@ M.Attr.behavior.__pairs = function(t)
927 return make_next_function(fields), t, nil
968 return make_next_function(fields), t, nil
928end
969end
929
970
971+-- Monkey-patch setters for `attr` fields to be more forgiving in the input that
972+-- results in a valid Attr value.
973+function augment_attr_setter (setters)
974+ if setters.attr then
975+ local orig = setters.attr
976+ setters.attr = function(k, v)
977+ orig(k, ensureAttr(v))
978+ end
979+ end
980+end
981+for _, blk in pairs(M.Block.constructor) do
982+ augment_attr_setter(blk.behavior.setters)
983+end
984+for _, inln in pairs(M.Inline.constructor) do
985+ augment_attr_setter(inln.behavior.setters)
986+end
987+
988+
930-- Citation
989-- Citation
931M.Citation = AstElement:make_subtype'Citation'
990M.Citation = AstElement:make_subtype'Citation'
932M.Citation.behavior.clone = M.types.clone.Citation
991M.Citation.behavior.clone = M.types.clone.Citation
doc/lua-filters.md+13−1
@@ -1247,7 +1247,19 @@ Superscripted text
1247
1247
1248### Attr {#type-ref-Attr}
1248### Attr {#type-ref-Attr}
1249
1249
1250−A set of element attributes
1250+A set of element attributes. Values of this type can be created
1251+with the [`pandoc.Attr`](#Attr) constructor. For convenience, it
1252+is usually not necessary to construct the value directly if it is
1253+part of an element, and it is sufficient to pass an HTML-like
1254+table. E.g., to create a span with identifier "text" and classes
1255+"a" and "b", on can write:
1256+
1257+ local span = pandoc.Span('text', {id = 'text', class = 'a b'})
1258+
1259+This also works when using the `attr` setter:
1260+
1261+ local span = pandoc.Span 'text'
1262+ span.attr = {id = 'text', class = 'a b', other_attribute = '1'}
1251
1263
1252Object equality is determined via
1264Object equality is determined via
1253[`pandoc.utils.equals`](#utils-equals).
1265[`pandoc.utils.equals`](#utils-equals).
test/lua/module/pandoc.lua+41
@@ -86,6 +86,47 @@ return {
86 assert.are_equal(count, 3)
86 assert.are_equal(count, 3)
87 end)
87 end)
88 },
88 },
89+ group 'HTML-like attribute tables' {
90+ test('in element constructor', function ()
91+ local html_attributes = {
92+ id = 'the-id',
93+ class = 'class1 class2',
94+ width = '11',
95+ height = '12'
96+ }
97+ local attr = pandoc.Span('test', html_attributes).attr
98+ assert.are_equal(attr.identifier, 'the-id')
99+ assert.are_equal(attr.classes[1], 'class1')
100+ assert.are_equal(attr.classes[2], 'class2')
101+ assert.are_equal(attr.attributes.width, '11')
102+ assert.are_equal(attr.attributes.height, '12')
103+ end),
104+ test('element attr setter', function ()
105+ local html_attributes = {
106+ id = 'the-id',
107+ class = 'class1 class2',
108+ width = "11",
109+ height = "12"
110+ }
111+ local span = pandoc.Span 'test'
112+ span.attr = html_attributes
113+ assert.are_equal(span.attr.identifier, 'the-id')
114+ assert.are_equal(span.attr.classes[1], 'class1')
115+ assert.are_equal(span.attr.classes[2], 'class2')
116+ assert.are_equal(span.attr.attributes.width, '11')
117+ assert.are_equal(span.attr.attributes.height, '12')
118+ end),
119+ test('element attrbutes setter', function ()
120+ local attributes = {
121+ width = "11",
122+ height = "12"
123+ }
124+ local span = pandoc.Span 'test'
125+ span.attributes = attributes
126+ assert.are_equal(span.attr.attributes.width, '11')
127+ assert.are_equal(span.attr.attributes.height, '12')
128+ end)
129+ }
89 },
130 },
90
131
91 group 'clone' {
132 group 'clone' {