@@ -17,6 +17,21 @@ THIS SOFTWARE.
17
17
]]
18
18
local List = require ' pandoc.List'
19
19
20
+ --- Returns the type of a metadata value.
21
+ --
22
+ -- @param v a metadata value
23
+ -- @treturn string one of `Blocks`, `Inlines`, `List`, `Map`, `string`, `boolean`
24
+ local function metatype (v )
25
+ if PANDOC_VERSION <= ' 2.16.2' then
26
+ local metatag = type (v ) == ' table' and v .t and v .t :gsub (' ^Meta' , ' ' )
27
+ return metatag and metatag ~= ' Map' and metatag or type (v )
28
+ end
29
+ return pandoc .utils .type (v )
30
+ end
31
+
32
+ local type = pandoc .utils .type or metatype
33
+
34
+
20
35
-- Split a string at commas.
21
36
local function comma_separated_values (str )
22
37
local acc = List :new {}
28
43
29
44
--- Ensure the return value is a list.
30
45
local function ensure_list (val )
31
- if type (val ) ~= ' table' then
32
- -- create singleton list (or empty list if val == nil).
33
- return List :new {val }
34
- elseif val .t == ' MetaInlines' then
46
+ if type (val ) == ' List' then
47
+ return val
48
+ elseif type (val ) == ' Inlines' then
35
49
-- check if this is really a comma-separated list
36
50
local csv = comma_separated_values (pandoc .utils .stringify (val ))
37
51
if # csv >= 2 then
38
52
return csv
39
53
end
40
54
return List :new {val }
41
- elseif val . t == ' MetaList ' then
55
+ elseif type ( val ) == ' table ' and # val > 0 then
42
56
return List :new (val )
43
57
else
44
- -- MetaBlocks or MetaMap , use as a singleton
58
+ -- Anything else , use as a singleton (or empty list if val == nil).
45
59
return List :new {val }
46
60
end
47
61
end
67
81
-- (`nil`).
68
82
function to_named_object (obj )
69
83
local named = {}
70
- if type (obj ) ~= ' table' then
84
+ if type (obj ) == ' Inlines' then
85
+ -- Treat inlines as the name
86
+ named .name = obj
87
+ named .id = pandoc .utils .stringify (obj )
88
+ elseif type (obj ) ~= ' table' then
71
89
-- if the object isn't a table, just use its value as a name.
72
90
named .name = pandoc .MetaInlines {pandoc .Str (tostring (obj ))}
73
91
named .id = tostring (obj )
74
- elseif obj .t == ' MetaInlines' then
75
- -- Treat inlines as the name
76
- named .name = obj
77
- named .id = pandoc .utils .stringify (obj )
78
92
elseif obj .name ~= nil then
79
93
-- object has name attribute → just create a copy of the object
80
94
add_missing_entries (obj , named )
81
95
named .id = pandoc .utils .stringify (named .id or named .name )
82
96
elseif next (obj ) and next (obj , next (obj )) == nil then
83
- -- the entry's key is taken as the name, the value contains the
84
- -- attributes.
97
+ -- Single-entry table. The entry's key is taken as the name, the value
98
+ -- contains the attributes.
85
99
key , attribs = next (obj )
86
- if type (attribs ) == " string" or attribs . t == ' MetaInlines ' then
100
+ if type (attribs ) == ' string' or type ( attribs ) == ' Inlines ' then
87
101
named .name = attribs
88
102
else
89
103
add_missing_entries (attribs , named )
0 commit comments