public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* How to deal with nested divs in lua filter
@ 2020-10-28  6:28 jiewuza
  2020-10-28  9:28 ` jiewuza
       [not found] ` <m2a6w6zwxu.fsf-9Onoh4P/yGk@public.gmane.org>
  0 siblings, 2 replies; 8+ messages in thread
From: jiewuza @ 2020-10-28  6:28 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw


I have nested divs in my document.
I have in my lua filter:

```
function handleDiv(el)
    if el.classes:includes("parent") then
       return handleChild(el.content)     ----- here content is {}
    end
    return {}
end
```

But I find the content of the parent div is empty. And it seems that
each child div triggers `handleDiv` before the parent does.

What I want is to deal with the child divs only in `handleChild` without
triggering `handleDiv`.

Is there any suggestions?


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

* Re: How to deal with nested divs in lua filter
  2020-10-28  6:28 How to deal with nested divs in lua filter jiewuza
@ 2020-10-28  9:28 ` jiewuza
       [not found] ` <m2a6w6zwxu.fsf-9Onoh4P/yGk@public.gmane.org>
  1 sibling, 0 replies; 8+ messages in thread
From: jiewuza @ 2020-10-28  9:28 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

jiewuza <jiewuza-9Onoh4P/yGk@public.gmane.org> writes:

I guess I have to return nil to keep the content.
And deal with it in `handleChild`.

> I have nested divs in my document.
> I have in my lua filter:
>
> ```
> function handleDiv(el)
>     if el.classes:includes("parent") then
>        return handleChild(el.content)     ----- here content is {}
>     end
>     return {}
> end
> ```
>
> But I find the content of the parent div is empty. And it seems that
> each child div triggers `handleDiv` before the parent does.
>
> What I want is to deal with the child divs only in `handleChild` without
> triggering `handleDiv`.
>
> Is there any suggestions?


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

* Re: How to deal with nested divs in lua filter
       [not found] ` <m2a6w6zwxu.fsf-9Onoh4P/yGk@public.gmane.org>
@ 2020-10-28 17:22   ` John MacFarlane
  2020-10-29  5:57     ` jiewuza
                       ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: John MacFarlane @ 2020-10-28 17:22 UTC (permalink / raw)
  To: jiewuza, pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw


Yes, it's a bottom-up traversal of the tree, so child nodes
will be processed before the parent.

Doing top-down traversals in filters is currently tricky.

You can, of course, do it manually, with a for loop around
the blocks in Pandoc, but that's clunky.

Maybe Albert has ideas on how we could make top-down
traversals possible.  (I needed one in pandoc's citation
processing and resorted to an inefficient workaround.)

jiewuza <jiewuza-9Onoh4P/yGk@public.gmane.org> writes:

> I have nested divs in my document.
> I have in my lua filter:
>
> ```
> function handleDiv(el)
>     if el.classes:includes("parent") then
>        return handleChild(el.content)     ----- here content is {}
>     end
>     return {}
> end
> ```
>
> But I find the content of the parent div is empty. And it seems that
> each child div triggers `handleDiv` before the parent does.
>
> What I want is to deal with the child divs only in `handleChild` without
> triggering `handleDiv`.
>
> Is there any suggestions?
>
> -- 
> 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/m2a6w6zwxu.fsf%40163.com.


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

* Re: How to deal with nested divs in lua filter
  2020-10-28 17:22   ` John MacFarlane
@ 2020-10-29  5:57     ` jiewuza
  2020-10-29  7:48     ` jiewuza
       [not found]     ` <m2d012uuxk.fsf-jF64zX8BO08an7k8zZ43ob9bIa4KchGshsV+eolpW18@public.gmane.org>
  2 siblings, 0 replies; 8+ messages in thread
From: jiewuza @ 2020-10-29  5:57 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw


Nice to know it.
Now I just keep them when it is dealing with the child nodes.
And do the process when it comes to the parent.

John MacFarlane <jgm-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org> writes:

> Yes, it's a bottom-up traversal of the tree, so child nodes
> will be processed before the parent.
>
> Doing top-down traversals in filters is currently tricky.
>
> You can, of course, do it manually, with a for loop around
> the blocks in Pandoc, but that's clunky.
>
> Maybe Albert has ideas on how we could make top-down
> traversals possible.  (I needed one in pandoc's citation
> processing and resorted to an inefficient workaround.)
>
> jiewuza <jiewuza-9Onoh4P/yGk@public.gmane.org> writes:
>
>> I have nested divs in my document.
>> I have in my lua filter:
>>
>> ```
>> function handleDiv(el)
>>     if el.classes:includes("parent") then
>>        return handleChild(el.content)     ----- here content is {}
>>     end
>>     return {}
>> end
>> ```
>>
>> But I find the content of the parent div is empty. And it seems that
>> each child div triggers `handleDiv` before the parent does.
>>
>> What I want is to deal with the child divs only in `handleChild` without
>> triggering `handleDiv`.
>>
>> Is there any suggestions?
>>
>> -- 
>> 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/m2a6w6zwxu.fsf%40163.com.


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

* Re: How to deal with nested divs in lua filter
  2020-10-28 17:22   ` John MacFarlane
  2020-10-29  5:57     ` jiewuza
@ 2020-10-29  7:48     ` jiewuza
       [not found]     ` <m2d012uuxk.fsf-jF64zX8BO08an7k8zZ43ob9bIa4KchGshsV+eolpW18@public.gmane.org>
  2 siblings, 0 replies; 8+ messages in thread
From: jiewuza @ 2020-10-29  7:48 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw


Nice to know it.
Now I just keep the content untouched when it is dealing with the child
nodes. And do the process when it comes to the parent.


John MacFarlane <jgm-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org> writes:

> Yes, it's a bottom-up traversal of the tree, so child nodes
> will be processed before the parent.
>
> Doing top-down traversals in filters is currently tricky.
>
> You can, of course, do it manually, with a for loop around
> the blocks in Pandoc, but that's clunky.
>
> Maybe Albert has ideas on how we could make top-down
> traversals possible.  (I needed one in pandoc's citation
> processing and resorted to an inefficient workaround.)
>
> jiewuza <jiewuza-9Onoh4P/yGk@public.gmane.org> writes:
>
>> I have nested divs in my document.
>> I have in my lua filter:
>>
>> ```
>> function handleDiv(el)
>>     if el.classes:includes("parent") then
>>        return handleChild(el.content)     ----- here content is {}
>>     end
>>     return {}
>> end
>> ```
>>
>> But I find the content of the parent div is empty. And it seems that
>> each child div triggers `handleDiv` before the parent does.
>>
>> What I want is to deal with the child divs only in `handleChild` without
>> triggering `handleDiv`.
>>
>> Is there any suggestions?
>>
>> -- 
>> 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/m2a6w6zwxu.fsf%40163.com.


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

* Re: How to deal with nested divs in lua filter
       [not found]     ` <m2d012uuxk.fsf-jF64zX8BO08an7k8zZ43ob9bIa4KchGshsV+eolpW18@public.gmane.org>
@ 2020-10-30 15:36       ` Albert Krewinkel
       [not found]         ` <87r1pflo9a.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
  0 siblings, 1 reply; 8+ messages in thread
From: Albert Krewinkel @ 2020-10-30 15:36 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

John MacFarlane writes:

> Doing top-down traversals in filters is currently tricky.
>
> You can, of course, do it manually, with a for loop around
> the blocks in Pandoc, but that's clunky.
>
> Maybe Albert has ideas on how we could make top-down
> traversals possible.  (I needed one in pandoc's citation
> processing and resorted to an inefficient workaround.)

I don't have any good ideas for that yet. Top-down filtering would
probably work best by descending manually; otherwise its just too easy
to accidentally create infinite loops. Maybe what we really need is to
revisit the idea of an XPATH like element selection method?

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


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

* Re: How to deal with nested divs in lua filter
       [not found]         ` <87r1pflo9a.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
@ 2020-10-30 16:37           ` John MacFarlane
       [not found]             ` <m2a6w3r7oh.fsf-jF64zX8BO08an7k8zZ43ob9bIa4KchGshsV+eolpW18@public.gmane.org>
  0 siblings, 1 reply; 8+ messages in thread
From: John MacFarlane @ 2020-10-30 16:37 UTC (permalink / raw)
  To: Albert Krewinkel, pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

Albert Krewinkel <albert+pandoc-9EawChwDxG8hFhg+JK9F0w@public.gmane.org> writes:

> John MacFarlane writes:
>
>> Doing top-down traversals in filters is currently tricky.
>>
>> You can, of course, do it manually, with a for loop around
>> the blocks in Pandoc, but that's clunky.
>>
>> Maybe Albert has ideas on how we could make top-down
>> traversals possible.  (I needed one in pandoc's citation
>> processing and resorted to an inefficient workaround.)
>
> I don't have any good ideas for that yet. Top-down filtering would
> probably work best by descending manually; otherwise its just too easy
> to accidentally create infinite loops. Maybe what we really need is to
> revisit the idea of an XPATH like element selection method?

I can't remember: do Lua filters use pandoc's native walk,
or did you reimplement the AST-walking logic in pure Lua?

If the former, note that we do have Text.Pandoc.Generic
which includes a top down traversal.  It's a lot slower
than 'walk', but it works.  Maybe we could wire it up
to something like walk_block_top_down?


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

* Re: How to deal with nested divs in lua filter
       [not found]             ` <m2a6w3r7oh.fsf-jF64zX8BO08an7k8zZ43ob9bIa4KchGshsV+eolpW18@public.gmane.org>
@ 2020-10-30 16:50               ` Albert Krewinkel
  0 siblings, 0 replies; 8+ messages in thread
From: Albert Krewinkel @ 2020-10-30 16:50 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw


John MacFarlane writes:

> I can't remember: do Lua filters use pandoc's native walk,
> or did you reimplement the AST-walking logic in pure Lua?
>
> If the former, note that we do have Text.Pandoc.Generic
> which includes a top down traversal.  It's a lot slower
> than 'walk', but it works.  Maybe we could wire it up
> to something like walk_block_top_down?

Oh, I had forgotten about that! It should work, with some limitations.

We use pandoc's native walk for filtering, but wrapped it in a custom
`Walkable` instance to enable list splicing and element deletion. Using
`T.P.Generic.bottomUp` would require filters to be more limited,
as deletion and splicing would no longer be possible.

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


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

end of thread, other threads:[~2020-10-30 16:50 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-28  6:28 How to deal with nested divs in lua filter jiewuza
2020-10-28  9:28 ` jiewuza
     [not found] ` <m2a6w6zwxu.fsf-9Onoh4P/yGk@public.gmane.org>
2020-10-28 17:22   ` John MacFarlane
2020-10-29  5:57     ` jiewuza
2020-10-29  7:48     ` jiewuza
     [not found]     ` <m2d012uuxk.fsf-jF64zX8BO08an7k8zZ43ob9bIa4KchGshsV+eolpW18@public.gmane.org>
2020-10-30 15:36       ` Albert Krewinkel
     [not found]         ` <87r1pflo9a.fsf-9EawChwDxG8hFhg+JK9F0w@public.gmane.org>
2020-10-30 16:37           ` John MacFarlane
     [not found]             ` <m2a6w3r7oh.fsf-jF64zX8BO08an7k8zZ43ob9bIa4KchGshsV+eolpW18@public.gmane.org>
2020-10-30 16:50               ` Albert Krewinkel

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