public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* Help with a filter to insert text from metadata in divs
@ 2020-03-13 17:58 jcr
       [not found] ` <6069e92d-8f77-45af-930f-8c6de206b9db-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 5+ messages in thread
From: jcr @ 2020-03-13 17:58 UTC (permalink / raw)
  To: pandoc-discuss


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

I have a document divided into numbered sections, each of which is 
represented as a div. I want my filter to add commentary from a table 
indexed by section number. Normally the commentary would be in a separate 
file containing only YAML metadata, and I would pass both to pandoc to 
merge them, but for the sake of simplicity, I supply below a test document 
in which everything is in one file. Here is my filter, which works, but I 
have a couple of questions.


function Pandoc (doc)
  -- save commentary for access when divs are traversed.
  local commentary = doc.meta.commentary
  doc.blocks = pandoc.walk_block(pandoc.Div(doc.blocks), {
    Div = function (el)
      if el.classes:includes('section') then
        local secnum = el.attributes.secnum
        local comment = commentary[secnum]
        if comment then
          el.content[#el.content+1] = pandoc.Div(comment[1], {class = 
'comment'})
        end
      end
      return el
    end
  }).content
  return doc
end

Questions:

   1. The variable comment has the type MetaBlocks. The only documented 
   field is t or tag, which returns a string. I want the blocks. By dumping 
   the content of the MetaBlocks table, I can see it's at index 1, so I access 
   it that way, but this doesn't seem like the right thing to do. Is there a 
   better way to get the blocks of a MetaBlocks?
   2. In this filter, I need to do something at every div based on the 
   document metadata. This seems like a common use case, but because filters 
   traverse the Pandoc element after its blocks, it seems necessary to deal 
   with this case as above: I have to call walk_blocks, and to use it I have 
   to wrap the blocks in a div, which I then have to unwrap to get a list of 
   blocks so I can set doc.blocks. Is there or should there be a simpler way 
   to do this?

Test document if you need it:
---
title: Test Document
commentary:
    1: |
        * This is a nice section.
    3: |
        * Here it would be better to write, "Lorem ipsum…"
---


::: {.section secnum="1"}
**1.** Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do 
eiusmod tempor incididunt ut labore et dolore magna aliqua.
:::


::: {.section secnum="2"}
**2.** Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris 
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in 
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur
.
:::


::: {.section secnum="3"}
**3.** Excepteur sint occaecat cupidatat non proident, sunt in culpa qui 
officia deserunt mollit anim id est laborum.
:::

-- 
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/6069e92d-8f77-45af-930f-8c6de206b9db%40googlegroups.com.

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

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

* Re: Help with a filter to insert text from metadata in divs
       [not found] ` <6069e92d-8f77-45af-930f-8c6de206b9db-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2020-03-14  3:09   ` EBkysko
       [not found]     ` <9a9ac01c-b187-4346-a07f-9ed480fd2668-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 5+ messages in thread
From: EBkysko @ 2020-03-14  3:09 UTC (permalink / raw)
  To: pandoc-discuss


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


On Friday, March 13, 2020 at 1:58:33 PM UTC-4, jcr wrote:
>
> In this filter, I need to do something at every div based on the document 
> metadata. This seems like a common use case, but because filters traverse 
> the Pandoc element after its blocks, it seems necessary to deal with this 
> case as above
>

You can force a filter on Meta to occur first:

return {
  {Meta = Meta},
  {Div = Div},
}

for example, mostly copy-pasting:

local commentary

function Div(el)
  if el.classes:includes('section') and el.attributes.secnum then
    local secnum = el.attributes.secnum
    local comment = commentary[secnum]
    if comment then
      el.content[#el.content+1] = pandoc.Div(comment[1], {class = 
'comment'})
    end
    return el
  end
end

return {
  {Meta = function(m) commentary = m.commentary end},
  {Div = Div},
}



-- 
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/9a9ac01c-b187-4346-a07f-9ed480fd2668%40googlegroups.com.

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

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

* Re: Help with a filter to insert text from metadata in divs
       [not found]     ` <9a9ac01c-b187-4346-a07f-9ed480fd2668-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2020-03-14  9:09       ` FI Apps
       [not found]         ` <CAGOSsdkWGpMnxkFxJo_yVWWzzzAfu3jD0CC3mUoXGckm9Qai3g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  0 siblings, 1 reply; 5+ messages in thread
From: FI Apps @ 2020-03-14  9:09 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

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

That's very helpful. I didn't realize that the order in which the functions
were returned was significant.

Does anyone have an answer to my other question? I am worried that using an
index of 1 to get the blocks from MetaBlocks could break at some time
in the future.

On Sat, Mar 14, 2020 at 4:09 AM EBkysko <ebkysko-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:

>
> On Friday, March 13, 2020 at 1:58:33 PM UTC-4, jcr wrote:
>>
>> In this filter, I need to do something at every div based on the document
>> metadata. This seems like a common use case, but because filters traverse
>> the Pandoc element after its blocks, it seems necessary to deal with this
>> case as above
>>
>
> You can force a filter on Meta to occur first:
>
> return {
>   {Meta = Meta},
>   {Div = Div},
> }
>
> for example, mostly copy-pasting:
>
> local commentary
>
> function Div(el)
>   if el.classes:includes('section') and el.attributes.secnum then
>     local secnum = el.attributes.secnum
>     local comment = commentary[secnum]
>     if comment then
>       el.content[#el.content+1] = pandoc.Div(comment[1], {class =
> 'comment'})
>     end
>     return el
>   end
> end
>
> return {
>   {Meta = function(m) commentary = m.commentary end},
>   {Div = Div},
> }
>
>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "pandoc-discuss" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/pandoc-discuss/iuNleux_sk0/unsubscribe.
> To unsubscribe from this group and all its topics, 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/9a9ac01c-b187-4346-a07f-9ed480fd2668%40googlegroups.com
> <https://groups.google.com/d/msgid/pandoc-discuss/9a9ac01c-b187-4346-a07f-9ed480fd2668%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/CAGOSsdkWGpMnxkFxJo_yVWWzzzAfu3jD0CC3mUoXGckm9Qai3g%40mail.gmail.com.

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

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

* Re: Help with a filter to insert text from metadata in divs
       [not found]         ` <CAGOSsdkWGpMnxkFxJo_yVWWzzzAfu3jD0CC3mUoXGckm9Qai3g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2020-03-14 12:42           ` EBkysko
       [not found]             ` <b6e3ff99-9e9e-452e-8643-70e78868ef3f-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 5+ messages in thread
From: EBkysko @ 2020-03-14 12:42 UTC (permalink / raw)
  To: pandoc-discuss


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



On Saturday, March 14, 2020 at 5:09:49 AM UTC-4, jcr wrote:
>
> That's very helpful. I didn't realize that the order in which the 
> functions were returned was significant.
>
>
More exactly, it's the order of the filter sets returned. The *Lua filter 
guide <https://pandoc.org/lua-filters.html#lua-filter-structure>* was 
updated these last few months, you should have a look.

Does anyone have an answer to my other question?


If you have this instead as a comment section:
---
title: Test Document
commentary:
    1: |
        * This is a nice section.

        Indeed.
    3: |
        * Here it would be better to write, "Lorem ipsum…"
---

then your code wouldn't keep the "Indeed".

I would have gone with "comment.c" or "comment["c"]", but that doesn't work 
(AW explained it in a SO question somewhere, don't remember details).

This seems to work:

el.content[#el.content+1] = pandoc.Div( {table.unpack(comment)} , {class = 
'comment'})

so "comment" is a MetaBlocks, a List of Blocks, a table you unpack and use 
as content for the Div (but care for those extra curly braces around!).
(simply using Div(comment) didn't work; maybe unpacking then repacking puts 
it in a more digestible/indexed format for Div)

I'll let more capable minds comment on this for further details :)

-- 
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/b6e3ff99-9e9e-452e-8643-70e78868ef3f%40googlegroups.com.

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

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

* Re: Help with a filter to insert text from metadata in divs
       [not found]             ` <b6e3ff99-9e9e-452e-8643-70e78868ef3f-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2020-03-15  5:38               ` EBkysko
  0 siblings, 0 replies; 5+ messages in thread
From: EBkysko @ 2020-03-15  5:38 UTC (permalink / raw)
  To: pandoc-discuss


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

Late saturday addition:

This works too:

el.content[#el.content+1] = pandoc.Div( pandoc.List:new(comment) , {class = 
'comment'})

which can also be written (for pandoc 2.9.2+):

el.content:insert(pandoc.Div( pandoc.List(comment) , {class = 'comment'}))


-----

(Note: "AW" in my previous post should have been "AK"; but anyway, found 
the SO I was thinking about, and has not much to do with the current 
situation, so never mind)

-- 
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/927b8754-c7da-4e41-95f6-6c43ebb99fcb%40googlegroups.com.

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

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

end of thread, other threads:[~2020-03-15  5:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-13 17:58 Help with a filter to insert text from metadata in divs jcr
     [not found] ` <6069e92d-8f77-45af-930f-8c6de206b9db-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2020-03-14  3:09   ` EBkysko
     [not found]     ` <9a9ac01c-b187-4346-a07f-9ed480fd2668-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2020-03-14  9:09       ` FI Apps
     [not found]         ` <CAGOSsdkWGpMnxkFxJo_yVWWzzzAfu3jD0CC3mUoXGckm9Qai3g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2020-03-14 12:42           ` EBkysko
     [not found]             ` <b6e3ff99-9e9e-452e-8643-70e78868ef3f-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2020-03-15  5:38               ` EBkysko

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