* How to tell an Inlines object from a table in a Lua filter? @ 2022-05-24 6:30 Shane Liesegang [not found] ` <84bdd685-384d-4fc4-bee5-75344028f42fn-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> 0 siblings, 1 reply; 3+ messages in thread From: Shane Liesegang @ 2022-05-24 6:30 UTC (permalink / raw) To: pandoc-discuss [-- Attachment #1.1: Type: text/plain, Size: 1238 bytes --] Is there a way to tell whether a given object is a pandoc.Inlines as opposed to a plain Lua table? When I call `type(obj)` on it, it just returns "table." I note that if I print it, it shows "Inlines:" instead of "table:" but is there any smarter way to determine? I see some code in the Lua filters example repository <https://github.com/pandoc/lua-filters/blob/03c0fef0e56274ec9c958cdfe508a20618a1b623/scholarly-metadata/scholarly-metadata.lua#L45> that does this kind of check, but I don't know if it's crucial to that code or not, if something has changed recently, etc. (My use case here is walking through my Meta object and building something that will get passed to different functions -- if it encounters a nested table of data, I need to recurse, but if it's an Inlines object, I want to stringify it.) -- You received this message because you are subscribed to the Google Groups "pandoc-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/84bdd685-384d-4fc4-bee5-75344028f42fn%40googlegroups.com. [-- Attachment #1.2: Type: text/html, Size: 1613 bytes --] ^ permalink raw reply [flat|nested] 3+ messages in thread
[parent not found: <84bdd685-384d-4fc4-bee5-75344028f42fn-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>]
* Re: How to tell an Inlines object from a table in a Lua filter? [not found] ` <84bdd685-384d-4fc4-bee5-75344028f42fn-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> @ 2022-05-24 6:49 ` Shane Liesegang [not found] ` <0a182969-9df6-4462-aae8-28f34d5b2e37n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> 0 siblings, 1 reply; 3+ messages in thread From: Shane Liesegang @ 2022-05-24 6:49 UTC (permalink / raw) To: pandoc-discuss [-- Attachment #1.1: Type: text/plain, Size: 1760 bytes --] I've figured out a workaround, but just wondering if there's a better way than diving into the metatables: ~~~ function fix_table_strings(t) for k, v in pairs(t) do if type(v) == "table" then local metatable = getmetatable(v) if metatable ~= nil and metatable.__name == "Inlines" then t[k] = pandoc.utils.stringify(v) else fix_table_strings(t[k]) end end end end ~~~ On Tuesday, May 24, 2022 at 9:30:41 AM UTC+3 Shane Liesegang wrote: > Is there a way to tell whether a given object is a pandoc.Inlines as > opposed to a plain Lua table? When I call `type(obj)` on it, it just > returns "table." I note that if I print it, it shows "Inlines:" instead of > "table:" but is there any smarter way to determine? > > I see some code in the Lua filters example repository > <https://github.com/pandoc/lua-filters/blob/03c0fef0e56274ec9c958cdfe508a20618a1b623/scholarly-metadata/scholarly-metadata.lua#L45> > that does this kind of check, but I don't know if it's crucial to that code > or not, if something has changed recently, etc. > > (My use case here is walking through my Meta object and building something > that will get passed to different functions -- if it encounters a nested > table of data, I need to recurse, but if it's an Inlines object, I want to > stringify it.) > > -- You received this message because you are subscribed to the Google Groups "pandoc-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/0a182969-9df6-4462-aae8-28f34d5b2e37n%40googlegroups.com. [-- Attachment #1.2: Type: text/html, Size: 2885 bytes --] ^ permalink raw reply [flat|nested] 3+ messages in thread
[parent not found: <0a182969-9df6-4462-aae8-28f34d5b2e37n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>]
* Re: How to tell an Inlines object from a table in a Lua filter? [not found] ` <0a182969-9df6-4462-aae8-28f34d5b2e37n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> @ 2022-05-24 9:13 ` BPJ 0 siblings, 0 replies; 3+ messages in thread From: BPJ @ 2022-05-24 9:13 UTC (permalink / raw) To: pandoc-discuss [-- Attachment #1: Type: text/plain, Size: 2996 bytes --] You can use the pandoc.utils.type() function. It will return a string 'Inlines' for an Inlines object but 'table' for a regular table. Unfortunately metadata mappings are now represented as regular tables so you may have to check whether a regular table is an array or a mapping. I usually do that with a function like this: ``````lua local function is_map (val) if 'table' == pandoc.utils.type(val) then if 0 == #val then if next(val) then return true end end end return false end `````` Den tis 24 maj 2022 08:50Shane Liesegang <liesegang-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> skrev: > I've figured out a workaround, but just wondering if there's a better way > than diving into the metatables: > > ~~~ > function fix_table_strings(t) > for k, v in pairs(t) do > if type(v) == "table" then > local metatable = getmetatable(v) > if metatable ~= nil and metatable.__name == "Inlines" then > t[k] = pandoc.utils.stringify(v) > else > fix_table_strings(t[k]) > end > end > end > end > ~~~ > > On Tuesday, May 24, 2022 at 9:30:41 AM UTC+3 Shane Liesegang wrote: > >> Is there a way to tell whether a given object is a pandoc.Inlines as >> opposed to a plain Lua table? When I call `type(obj)` on it, it just >> returns "table." I note that if I print it, it shows "Inlines:" instead of >> "table:" but is there any smarter way to determine? >> >> I see some code in the Lua filters example repository >> <https://github.com/pandoc/lua-filters/blob/03c0fef0e56274ec9c958cdfe508a20618a1b623/scholarly-metadata/scholarly-metadata.lua#L45> >> that does this kind of check, but I don't know if it's crucial to that code >> or not, if something has changed recently, etc. >> >> (My use case here is walking through my Meta object and building >> something that will get passed to different functions -- if it encounters a >> nested table of data, I need to recurse, but if it's an Inlines object, I >> want to stringify it.) >> >> -- > You received this message because you are subscribed to the Google Groups > "pandoc-discuss" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msgid/pandoc-discuss/0a182969-9df6-4462-aae8-28f34d5b2e37n%40googlegroups.com > <https://groups.google.com/d/msgid/pandoc-discuss/0a182969-9df6-4462-aae8-28f34d5b2e37n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "pandoc-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/CADAJKhBdw0MQ1d4rf9H-jr0EpCoPSw9zCORUnAjp8cztsOnsfg%40mail.gmail.com. [-- Attachment #2: Type: text/html, Size: 4565 bytes --] ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2022-05-24 9:13 UTC | newest] Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2022-05-24 6:30 How to tell an Inlines object from a table in a Lua filter? Shane Liesegang [not found] ` <84bdd685-384d-4fc4-bee5-75344028f42fn-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> 2022-05-24 6:49 ` Shane Liesegang [not found] ` <0a182969-9df6-4462-aae8-28f34d5b2e37n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> 2022-05-24 9:13 ` BPJ
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).