public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* Lua Filter: Detect Type of Inlines
@ 2023-05-12 17:13 ThomasH
       [not found] ` <4d901ab9-179d-4274-8ce9-fdf36056905fn-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 4+ messages in thread
From: ThomasH @ 2023-05-12 17:13 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 839 bytes --]

I want to detect the types of the inline elements of a paragraph. I 
understand there is pandoc.utils.type() that basically does that, but when 
I run

function Para(para)
    for i = 1,#para.content,1 do
        print(tostring(pandoc.utils.type(para.content[i])))
    end
end

all that is printed is "Inline" for all elements, not specific types like 
Str, Span, Link or Image. How can I get at the specific types?

Thanks,
T.

-- 
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/4d901ab9-179d-4274-8ce9-fdf36056905fn%40googlegroups.com.

[-- Attachment #1.2: Type: text/html, Size: 1266 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Lua Filter: Detect Type of Inlines
       [not found] ` <4d901ab9-179d-4274-8ce9-fdf36056905fn-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2023-05-12 17:32   ` Albert Krewinkel
       [not found]     ` <87o7mpqwfd.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
  0 siblings, 1 reply; 4+ messages in thread
From: Albert Krewinkel @ 2023-05-12 17:32 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw


ThomasH <therch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:

> I want to detect the types of the inline elements of a paragraph. I
> understand there is pandoc.utils.type() that basically does that, but
> when I run
>
> function Para(para)
>     for i = 1,#para.content,1 do
>         print(tostring(pandoc.utils.type(para.content[i])))
>     end
> end
>
> all that is printed is "Inline" for all elements, not specific types
> like Str, Span, Link or Image. How can I get at the specific types?

The trick here is that we are using Haskell terminology: Inline is a
*type*, and Str, Span, Link, etc. are *constructors* for this type. In
Lua (and JSON) contexts, the property that identifies the name of the
constructor of a value is called a "tag". Try this:

``` lua
function Para(para)
  for i = 1,#para.content do
    print(para.content[i].tag))
  end
end
```

The `.t` property is an alias for `.tag` and can be used as well.

I have plans to change the behavior of `pandoc.utils.type` and to make
the function return two results. The first result would stay as-is, with
the second result containing the constructor name. But I need to run
more tests to ensure that this won't lead to performance degradation.


-- 
Albert Krewinkel
GPG: 8eed e3e2 e8c5 6f18 81fe  e836 388d c0b2 1f63 1124

-- 
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/87o7mpqwfd.fsf%40zeitkraut.de.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Lua Filter: Detect Type of Inlines
       [not found]     ` <87o7mpqwfd.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
@ 2023-05-13  9:00       ` ThomasH
       [not found]         ` <f31b8b9b-f30e-45c6-91d5-caddb494f5b0n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 4+ messages in thread
From: ThomasH @ 2023-05-13  9:00 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 1912 bytes --]

Great, thanks.

On Friday, May 12, 2023 at 7:41:50 PM UTC+2 Albert Krewinkel wrote:

>
> ThomasH <the...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:
>
> > I want to detect the types of the inline elements of a paragraph. I
> > understand there is pandoc.utils.type() that basically does that, but
> > when I run
> >
> > function Para(para)
> >     for i = 1,#para.content,1 do
> >         print(tostring(pandoc.utils.type(para.content[i])))
> >     end
> > end
> >
> > all that is printed is "Inline" for all elements, not specific types
> > like Str, Span, Link or Image. How can I get at the specific types?
>
> The trick here is that we are using Haskell terminology: Inline is a
> *type*, and Str, Span, Link, etc. are *constructors* for this type. In
> Lua (and JSON) contexts, the property that identifies the name of the
> constructor of a value is called a "tag". Try this:
>
> ``` lua
> function Para(para)
> for i = 1,#para.content do
> print(para.content[i].tag))
> end
> end
> ```
>
> The `.t` property is an alias for `.tag` and can be used as well.
>
> I have plans to change the behavior of `pandoc.utils.type` and to make
> the function return two results. The first result would stay as-is, with
> the second result containing the constructor name. But I need to run
> more tests to ensure that this won't lead to performance degradation.
>
>
> -- 
> Albert Krewinkel
> GPG: 8eed e3e2 e8c5 6f18 81fe e836 388d c0b2 1f63 1124
>

-- 
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/f31b8b9b-f30e-45c6-91d5-caddb494f5b0n%40googlegroups.com.

[-- Attachment #1.2: Type: text/html, Size: 2623 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Lua Filter: Detect Type of Inlines
       [not found]         ` <f31b8b9b-f30e-45c6-91d5-caddb494f5b0n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2023-05-13 10:56           ` 'William Lupton' via pandoc-discuss
  0 siblings, 0 replies; 4+ messages in thread
From: 'William Lupton' via pandoc-discuss @ 2023-05-13 10:56 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

[-- Attachment #1: Type: text/plain, Size: 3835 bytes --]

A couple of things:

   1. This seems a good moment to mention that my
   https://github.com/wlupton/pandoc-lua-logging has moved to
   https://github.com/pandoc-ext/logging (it's redirected automatically)

   2. Its logging.lua module contains this wrapper around
   pandoc.utils.type() (sorry for the rather non-obvious ({Inline=1,
   Block=1})[typ] idiom ... I no longer think that this is a particularly
   good idea!)

logging.type = function(value)
    -- this can return 'Inlines', 'Blocks', 'Inline', 'Block' etc., or
    -- anything that built-in type() can return, namely 'nil', 'number',
    -- 'string', 'boolean', 'table', 'function', 'thread', or 'userdata'
    local typ = pandoc.utils.type(value)

    -- it seems that it can also return strings like 'pandoc Row'; replace
    -- spaces with periods
    -- XXX I'm not sure that this is done consistently, e.g. I don't think
    --     it's done for pandoc.Attr or pandoc.List?
    typ = typ:gsub(' ', '.')

    -- map Inline and Block to the tag name
    -- XXX I guess it's intentional that it doesn't already do this?
    return ({Inline=1, Block=1})[typ] and value.tag or typ
end

On Sat, 13 May 2023 at 10:00, ThomasH <therch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:

> Great, thanks.
>
> On Friday, May 12, 2023 at 7:41:50 PM UTC+2 Albert Krewinkel wrote:
>
>>
>> ThomasH <the...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:
>>
>> > I want to detect the types of the inline elements of a paragraph. I
>> > understand there is pandoc.utils.type() that basically does that, but
>> > when I run
>> >
>> > function Para(para)
>> >     for i = 1,#para.content,1 do
>> >         print(tostring(pandoc.utils.type(para.content[i])))
>> >     end
>> > end
>> >
>> > all that is printed is "Inline" for all elements, not specific types
>> > like Str, Span, Link or Image. How can I get at the specific types?
>>
>> The trick here is that we are using Haskell terminology: Inline is a
>> *type*, and Str, Span, Link, etc. are *constructors* for this type. In
>> Lua (and JSON) contexts, the property that identifies the name of the
>> constructor of a value is called a "tag". Try this:
>>
>> ``` lua
>> function Para(para)
>> for i = 1,#para.content do
>> print(para.content[i].tag))
>> end
>> end
>> ```
>>
>> The `.t` property is an alias for `.tag` and can be used as well.
>>
>> I have plans to change the behavior of `pandoc.utils.type` and to make
>> the function return two results. The first result would stay as-is, with
>> the second result containing the constructor name. But I need to run
>> more tests to ensure that this won't lead to performance degradation.
>>
>>
>> --
>> Albert Krewinkel
>> GPG: 8eed e3e2 e8c5 6f18 81fe e836 388d c0b2 1f63 1124
>>
> --
> 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/f31b8b9b-f30e-45c6-91d5-caddb494f5b0n%40googlegroups.com
> <https://groups.google.com/d/msgid/pandoc-discuss/f31b8b9b-f30e-45c6-91d5-caddb494f5b0n%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/CAEe_xxj%2BmWtBs2%3DPF31Dh24VEApmG8ZAY%2B_GpWfBFEwCVPpMuw%40mail.gmail.com.

[-- Attachment #2: Type: text/html, Size: 5432 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-05-13 10:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-12 17:13 Lua Filter: Detect Type of Inlines ThomasH
     [not found] ` <4d901ab9-179d-4274-8ce9-fdf36056905fn-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2023-05-12 17:32   ` Albert Krewinkel
     [not found]     ` <87o7mpqwfd.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
2023-05-13  9:00       ` ThomasH
     [not found]         ` <f31b8b9b-f30e-45c6-91d5-caddb494f5b0n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2023-05-13 10:56           ` 'William Lupton' via pandoc-discuss

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).