public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* Wrapping sections of existing document in <div></div>
@ 2023-06-19 22:11 H
       [not found] ` <e3258495-d762-2f54-cff3-a2607261bcf7-FcZObrvlYduBUy7/sJONFg@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: H @ 2023-06-19 22:11 UTC (permalink / raw)
  To: Pandoc Mailing List

What is the "best" way of wrapping parts of an existing document in <div></div> in a filter?

Using insert.table(existingtable, pandoc.Div(existingtextblocks)) does not work since the existing text blocks get duplicated in the output.

I see two options but there might be others:

- Surround existing text blocks with pandoc.RawInline(1, 'html, '<div>') and pandoc.RawInLine('html', '</div>').

- Create a new table and use insert.table(newtable, pandoc.Div(existingtextblock)) and also insert the rest of the document.

Are there other options?


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

* Re: Wrapping sections of existing document in <div></div>
       [not found] ` <e3258495-d762-2f54-cff3-a2607261bcf7-FcZObrvlYduBUy7/sJONFg@public.gmane.org>
@ 2023-06-20  0:14   ` H
       [not found]     ` <AC708553-331A-45D3-A618-EAA8BB7784A5-FcZObrvlYduBUy7/sJONFg@public.gmane.org>
  0 siblings, 1 reply; 6+ messages in thread
From: H @ 2023-06-20  0:14 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

On June 19, 2023 6:11:35 PM EDT, H <agents-FcZObrvlYduBUy7/sJONFg@public.gmane.org> wrote:
>What is the "best" way of wrapping parts of an existing document in
><div></div> in a filter?
>
>Using insert.table(existingtable, pandoc.Div(existingtextblocks)) does
>not work since the existing text blocks get duplicated in the output.
>
>I see two options but there might be others:
>
>- Surround existing text blocks with pandoc.RawInline(1, 'html,
>'<div>') and pandoc.RawInLine('html', '</div>').
>
>- Create a new table and use insert.table(newtable,
>pandoc.Div(existingtextblock)) and also insert the rest of the
>document.
>
>Are there other options?

Replying to my own question, it seems the most "elegant" way of accomplishing this requires this to be done be in two stages:

- First, delete the blocks in question from the block list table, eg doc.blocks.
- Second, insert them into the same table using pandoc.Div().

Unfortunately it seems table.delete() does not accept a list of blocks, only one block at a time, pandoc.Div, on the other hand, accepts a table. Here is a code example when I was trying it out:

local temp = {}
for i = 3, 1, -1 do
  table.insert(temp, table.remove(doc.blocks, 5))
end

table.insert(doc.blocks, 2, pandoc.Div(temp))

The above code removes blocks 5 through 7 from doc.blocks, then inserts them, in the same order but surrounded by <div></div>, in position 2.

If there is a better way of accomplishing this, I would appreciate hearing it.


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

* Re: Wrapping sections of existing document in <div></div>
       [not found]     ` <AC708553-331A-45D3-A618-EAA8BB7784A5-FcZObrvlYduBUy7/sJONFg@public.gmane.org>
@ 2023-06-20 10:37       ` mf
       [not found]         ` <e42adc8d-f75c-4b91-7777-cc162e157c64-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
  2023-06-21 15:36       ` BPJ
  1 sibling, 1 reply; 6+ messages in thread
From: mf @ 2023-06-20 10:37 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

Il 20/06/23 02:14, H ha scritto:
> On June 19, 2023 6:11:35 PM EDT, H <agents-FcZObrvlYduBUy7/sJONFg@public.gmane.org> wrote:
>> What is the "best" way of wrapping parts of an existing document in
>> <div></div> in a filter?
>>
>> Using insert.table(existingtable, pandoc.Div(existingtextblocks)) does
>> not work since the existing text blocks get duplicated in the output.
>>
>> I see two options but there might be others:
>>
>> - Surround existing text blocks with pandoc.RawInline(1, 'html,
>> '<div>') and pandoc.RawInLine('html', '</div>').
>>
>> - Create a new table and use insert.table(newtable,
>> pandoc.Div(existingtextblock)) and also insert the rest of the
>> document.
>>

This would apply only to HTML output.

>> Are there other options?
> 
> Replying to my own question, it seems the most "elegant" way of accomplishing this requires this to be done be in two stages:
> 
> - First, delete the blocks in question from the block list table, eg doc.blocks.
> - Second, insert them into the same table using pandoc.Div().
> 
> Unfortunately it seems table.delete() does not accept a list of blocks, only one block at a time, pandoc.Div, on the other hand, accepts a table. Here is a code example when I was trying it out:
> 
> local temp = {}
> for i = 3, 1, -1 do
>    table.insert(temp, table.remove(doc.blocks, 5))
> end
> 
> table.insert(doc.blocks, 2, pandoc.Div(temp))
> 
> The above code removes blocks 5 through 7 from doc.blocks, then inserts them, in the same order but surrounded by <div></div>, in position 2.
> 
> If there is a better way of accomplishing this, I would appreciate hearing it.
> 

Consider using the Blocks function in the filter (see 
https://pandoc.org/lua-filters.html#filters-on-element-sequences).

The Blocks function is called on every sequence of blocks.
Here's an example:

local my_filter = {
   {
     Blocks = function(blocks)
       local first_in_div, last_in_div = detect_seq_for_div(blocks)
       -- check if last is after first and the Div will not be empty
       if first_in_div and last_in_div and last_in_div - first_in_div > 
1 then
         local newblocks = pandoc.List()
         local divblocks = pandoc.List()
         for i = 1, #blocks do
           if i < first_in_div or i > last_in_div then
             newblocks:insert(blocks[i])
           else
             divblocks:insert(blocks[i])
             if i == last_in_div then
               local div = pandoc.Div(divblocks)
               newblocks:insert(div)
             end
           end
         end
         return newblocks
       end
       -- if no meaningful sequence is detected,
       -- no return statement to keep the current blocks
     end,
   }
}

You should write the detect_seq_for_div function that scans a list of 
blocks and returns two positive integers only if it contains a 
meaningful sequence of blocks that you want to wrap in a Div. Otherwise 
it should return nil.

Those two integers are the indexes of the first and the last block you 
want to put in the Div.


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

* Re: Wrapping sections of existing document in <div></div>
       [not found]         ` <e42adc8d-f75c-4b91-7777-cc162e157c64-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
@ 2023-06-21  0:21           ` H
  0 siblings, 0 replies; 6+ messages in thread
From: H @ 2023-06-21  0:21 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

On 06/20/2023 06:37 AM, mf wrote:
> Il 20/06/23 02:14, H ha scritto:
>> On June 19, 2023 6:11:35 PM EDT, H <agents-FcZObrvlYduBUy7/sJONFg@public.gmane.org> wrote:
>>> What is the "best" way of wrapping parts of an existing document in
>>> <div></div> in a filter?
>>>
>>> Using insert.table(existingtable, pandoc.Div(existingtextblocks)) does
>>> not work since the existing text blocks get duplicated in the output.
>>>
>>> I see two options but there might be others:
>>>
>>> - Surround existing text blocks with pandoc.RawInline(1, 'html,
>>> '<div>') and pandoc.RawInLine('html', '</div>').
>>>
>>> - Create a new table and use insert.table(newtable,
>>> pandoc.Div(existingtextblock)) and also insert the rest of the
>>> document.
>>>
>
> This would apply only to HTML output.
>
>>> Are there other options?
>>
>> Replying to my own question, it seems the most "elegant" way of accomplishing this requires this to be done be in two stages:
>>
>> - First, delete the blocks in question from the block list table, eg doc.blocks.
>> - Second, insert them into the same table using pandoc.Div().
>>
>> Unfortunately it seems table.delete() does not accept a list of blocks, only one block at a time, pandoc.Div, on the other hand, accepts a table. Here is a code example when I was trying it out:
>>
>> local temp = {}
>> for i = 3, 1, -1 do
>>    table.insert(temp, table.remove(doc.blocks, 5))
>> end
>>
>> table.insert(doc.blocks, 2, pandoc.Div(temp))
>>
>> The above code removes blocks 5 through 7 from doc.blocks, then inserts them, in the same order but surrounded by <div></div>, in position 2.
>>
>> If there is a better way of accomplishing this, I would appreciate hearing it.
>>
>
> Consider using the Blocks function in the filter (see https://pandoc.org/lua-filters.html#filters-on-element-sequences).
>
> The Blocks function is called on every sequence of blocks.
> Here's an example:
>
> local my_filter = {
>   {
>     Blocks = function(blocks)
>       local first_in_div, last_in_div = detect_seq_for_div(blocks)
>       -- check if last is after first and the Div will not be empty
>       if first_in_div and last_in_div and last_in_div - first_in_div > 1 then
>         local newblocks = pandoc.List()
>         local divblocks = pandoc.List()
>         for i = 1, #blocks do
>           if i < first_in_div or i > last_in_div then
>             newblocks:insert(blocks[i])
>           else
>             divblocks:insert(blocks[i])
>             if i == last_in_div then
>               local div = pandoc.Div(divblocks)
>               newblocks:insert(div)
>             end
>           end
>         end
>         return newblocks
>       end
>       -- if no meaningful sequence is detected,
>       -- no return statement to keep the current blocks
>     end,
>   }
> }
>
> You should write the detect_seq_for_div function that scans a list of blocks and returns two positive integers only if it contains a meaningful sequence of blocks that you want to wrap in a Div. Otherwise it should return nil.
>
> Those two integers are the indexes of the first and the last block you want to put in the Div.
>
Thank you! I will play around with the code.

-- 
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/de6bf20a-1e87-0005-3a2e-86c459ff9543%40meddatainc.com.


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

* Re: Wrapping sections of existing document in <div></div>
       [not found]     ` <AC708553-331A-45D3-A618-EAA8BB7784A5-FcZObrvlYduBUy7/sJONFg@public.gmane.org>
  2023-06-20 10:37       ` mf
@ 2023-06-21 15:36       ` BPJ
       [not found]         ` <CADAJKhBZGdihMMgb=2_9rT04RsobJyu+48HxtO5755ADPcnZBQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
  1 sibling, 1 reply; 6+ messages in thread
From: BPJ @ 2023-06-21 15:36 UTC (permalink / raw)
  To: pandoc-discuss

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

1.  You have two lists, the original/old one and the new one.
2.  Push elements from the old list onto the new list until you get to the
first element which should go into the new nested div.
3.  Create a third list and
push elements from the old list onto that list until you get to the last
element which should go into the div.
4.  Create a div from the third
list.
5.  Push the div onto the new list.
6.  Either repeat steps 2–5 or resume pushing elements from the old
list onto the new list until you get to the last element.

``````lua
function Pandoc(doc)
  local old = doc.blocks
  local new = pandoc.Blocks({ })
  local div = false
  local i = 0
  local elem = nil
  while i < #old do
    i = i + 1
    elem = old[i]
    if test_div_start_condition(elem) then
      div = pandoc.Blocks({ elem })
    elseif test_div_end_condition(elem) then
      if div then
        div[#div + 1] = elem
        new[#new + 1] = pandoc.Div(div, { class = 'foo', bar = 'baz' })
        div = nil
        -- -- If this is the only div
        -- while i < #old do
        --   i = i + 1
        --   new[#new + 1] = = old[i]
        -- end
      else
        new[#new + 1] = elem
      end
    elseif div then
      div[#div + 1] = elem
    else
      new[#new + 1] = elem
    end
  end
  doc.blocks = new
  return doc
end
``````


Den tis 20 juni 2023 02:16H <agents-FcZObrvlYduBUy7/sJONFg@public.gmane.org> skrev:

> On June 19, 2023 6:11:35 PM EDT, H <agents-FcZObrvlYduBUy7/sJONFg@public.gmane.org> wrote:
> >What is the "best" way of wrapping parts of an existing document in
> ><div></div> in a filter?
> >
> >Using insert.table(existingtable, pandoc.Div(existingtextblocks)) does
> >not work since the existing text blocks get duplicated in the output.
> >
> >I see two options but there might be others:
> >
> >- Surround existing text blocks with pandoc.RawInline(1, 'html,
> >'<div>') and pandoc.RawInLine('html', '</div>').
> >
> >- Create a new table and use insert.table(newtable,
> >pandoc.Div(existingtextblock)) and also insert the rest of the
> >document.
> >
> >Are there other options?
>
> Replying to my own question, it seems the most "elegant" way of
> accomplishing this requires this to be done be in two stages:
>
> - First, delete the blocks in question from the block list table, eg
> doc.blocks.
> - Second, insert them into the same table using pandoc.Div().
>
> Unfortunately it seems table.delete() does not accept a list of blocks,
> only one block at a time, pandoc.Div, on the other hand, accepts a table.
> Here is a code example when I was trying it out:
>
> local temp = {}
> for i = 3, 1, -1 do
>   table.insert(temp, table.remove(doc.blocks, 5))
> end
>
> table.insert(doc.blocks, 2, pandoc.Div(temp))
>
> The above code removes blocks 5 through 7 from doc.blocks, then inserts
> them, in the same order but surrounded by <div></div>, in position 2.
>
> If there is a better way of accomplishing this, I would appreciate hearing
> 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/AC708553-331A-45D3-A618-EAA8BB7784A5%40meddatainc.com
> .
>

-- 
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/CADAJKhBZGdihMMgb%3D2_9rT04RsobJyu%2B48HxtO5755ADPcnZBQ%40mail.gmail.com.

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

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

* Re: Wrapping sections of existing document in <div></div>
       [not found]         ` <CADAJKhBZGdihMMgb=2_9rT04RsobJyu+48HxtO5755ADPcnZBQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
@ 2023-06-21 22:58           ` H
  0 siblings, 0 replies; 6+ messages in thread
From: H @ 2023-06-21 22:58 UTC (permalink / raw)
  To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw

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

On 06/21/2023 11:36 AM, BPJ wrote:
> 1.  You have two lists, the original/old one and the new one.
> 2.  Push elements from the old list onto the new list until you get to the first element which should go into the new nested div.
> 3.  Create a third list and
> push elements from the old list onto that list until you get to the last
> element which should go into the div.
> 4.  Create a div from the third
> list.
> 5.  Push the div onto the new list.
> 6.  Either repeat steps 2–5 or resume pushing elements from the old
> list onto the new list until you get to the last element.
>
> ``````lua
> function Pandoc(doc)
>   local old = doc.blocks
>   local new = pandoc.Blocks({ })
>   local div = false
>   local i = 0
>   local elem = nil
>   while i < #old do
>     i = i + 1
>     elem = old[i]
>     if test_div_start_condition(elem) then
>       div = pandoc.Blocks({ elem })
>     elseif test_div_end_condition(elem) then
>       if div then
>         div[#div + 1] = elem
>         new[#new + 1] = pandoc.Div(div, { class = 'foo', bar = 'baz' })
>         div = nil
>         -- -- If this is the only div
>         -- while i < #old do
>         --   i = i + 1
>         --   new[#new + 1] = = old[i]
>         -- end
>       else
>         new[#new + 1] = elem
>       end
>     elseif div then
>       div[#div + 1] = elem
>     else
>       new[#new + 1] = elem
>     end
>   end
>   doc.blocks = new
>   return doc
> end
> ``````
>
>
> Den tis 20 juni 2023 02:16H <agents-FcZObrvlYduBUy7/sJONFg@public.gmane.org <mailto:agents@meddatainc.com>> skrev:
>
>     On June 19, 2023 6:11:35 PM EDT, H <agents-FcZObrvlYduBUy7/sJONFg@public.gmane.org <mailto:agents-FcZObrvlYduBUy7/sJONFg@public.gmane.org>> wrote:
>     >What is the "best" way of wrapping parts of an existing document in
>     ><div></div> in a filter?
>     >
>     >Using insert.table(existingtable, pandoc.Div(existingtextblocks)) does
>     >not work since the existing text blocks get duplicated in the output.
>     >
>     >I see two options but there might be others:
>     >
>     >- Surround existing text blocks with pandoc.RawInline(1, 'html,
>     >'<div>') and pandoc.RawInLine('html', '</div>').
>     >
>     >- Create a new table and use insert.table(newtable,
>     >pandoc.Div(existingtextblock)) and also insert the rest of the
>     >document.
>     >
>     >Are there other options?
>
>     Replying to my own question, it seems the most "elegant" way of accomplishing this requires this to be done be in two stages:
>
>     - First, delete the blocks in question from the block list table, eg doc.blocks.
>     - Second, insert them into the same table using pandoc.Div().
>
>     Unfortunately it seems table.delete() does not accept a list of blocks, only one block at a time, pandoc.Div, on the other hand, accepts a table. Here is a code example when I was trying it out:
>
>     local temp = {}
>     for i = 3, 1, -1 do
>       table.insert(temp, table.remove(doc.blocks, 5))
>     end
>
>     table.insert(doc.blocks, 2, pandoc.Div(temp))
>
>     The above code removes blocks 5 through 7 from doc.blocks, then inserts them, in the same order but surrounded by <div></div>, in position 2.
>
>     If there is a better way of accomplishing this, I would appreciate hearing it.
>
Thank you, it seems both your and mf's suggestion are similar so I should be all set.

-- 
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/70f542f6-e44d-c423-fa0e-22d486d998e0%40meddatainc.com.

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

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

end of thread, other threads:[~2023-06-21 22:58 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-19 22:11 Wrapping sections of existing document in <div></div> H
     [not found] ` <e3258495-d762-2f54-cff3-a2607261bcf7-FcZObrvlYduBUy7/sJONFg@public.gmane.org>
2023-06-20  0:14   ` H
     [not found]     ` <AC708553-331A-45D3-A618-EAA8BB7784A5-FcZObrvlYduBUy7/sJONFg@public.gmane.org>
2023-06-20 10:37       ` mf
     [not found]         ` <e42adc8d-f75c-4b91-7777-cc162e157c64-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2023-06-21  0:21           ` H
2023-06-21 15:36       ` BPJ
     [not found]         ` <CADAJKhBZGdihMMgb=2_9rT04RsobJyu+48HxtO5755ADPcnZBQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2023-06-21 22:58           ` H

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