public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
From: BPJ <bpj-J3H7GcXPSITLoDKTGw+V6w@public.gmane.org>
To: pandoc-discuss <pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
Subject: Re: How to manipulate with Block elements with Lua filters
Date: Wed, 12 Jan 2022 10:45:57 +0100	[thread overview]
Message-ID: <CADAJKhD6WVs3UxD0Dt0bYLA8mE4-A4n=QyyKrwkT=MYO+QNaVg@mail.gmail.com> (raw)
In-Reply-To: <aea85ef4-af50-46e5-8e21-d677801cb971n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>

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

The div content is an enhanced `pandoc.List` table with some useful methods:

https://pandoc.org/lua-filters.html#module-pandoc.list

The filter documentation notes where you will find List objects in the type
descriptions.

BTW you probably don't want to use the return value of
pandoc.utils.stringify the way you did because it strips all formatting. If
you just inject the HTML tags the way I showed formatting in the div
content will be preserved.

Den tis 11 jan. 2022 15:08Tomáš Kruliš <tomas.krulis@integromat.com> skrev:

> Oh, so the `div` content is a `table` in lua terms? That didn't occured to
> me, even though it probably should ... I actually managed to replace it not
> as a `RawInline`, but `RawBlock`, like so:
>
>  ```.{lua}
>   if elem.t == 'Div' and elem.classes[1] == "replace-me" then
>     content = pandoc.utils.stringify(elem.content)
>     return pandoc.RawBlock('html', '<note>' .. content.. '</note>')
>   else
>     return elem
>   end
> ```
>
> But thank you for your solution, for me it was very educational!
> Another thing that I struggle with is to wrap my head around `walk_block`
> function. From the examples in the pandoc official page it is not much
> clearer to me. I think I now understand that they are used to
> iterate/filter over the contents of the block that is being walked over.
> But if the block element contains only String elements (paragraph with
> simple text), then also Str-filter functions are applied to it?
> Maybe my confusion can be voiced like this: When should I use
> `pandoc.walk_block` function to get what I want and cannot use something
> else? Explained not only with code, but also with commentary ...
> Dne pondělí 10. ledna 2022 v 19:46:37 UTC+1 uživatel BPJ napsal:
>
>> It is neither possible nor needed to convert the whole block to HTML
>> within the filter; rather you should just inject the start and end tags:
>>
>> ``````lua
>> -- Create these only once, for speed and resources saving!
>> local pre = pandoc.RawBlock('html', '<note>')
>> local post = pandoc.RawBlock('html', '</note>')
>>
>> function Div (div)
>>   -- The order of the classes shouldn't matter!
>>   if div.classes:includes('replace-me') then
>>     local content = div.content
>>     table.insert(content, 1, pre)
>>     table.insert(content, post)
>>     return content
>>   end
>>   return nil
>> end
>> ``````
>>
>> Den mån 10 jan. 2022 15:33Tomáš Kruliš <tomas....@integromat.com> skrev:
>>
>>> Hello,
>>>
>>> I would like to ask how you should, in general, detect and manipulate
>>> with Pandoc `block` elements. Currently, I am trying to replace `<div
>>> class='replace-me'>` tag with `<note>` tag in similar (highly simplified)
>>> HTML file:
>>>
>>> ```.{html}
>>> <html>
>>> <body>
>>> <p> First line. </p>
>>> <div class="replace-me another-class"> This should carry on to converted
>>> document. </div>
>>> <p>End.</p>
>>> </body>
>>> </html>
>>> ```
>>>
>>> I have tried to detect the `<div>` tag, use `walk_block` to get the
>>> `<div>` content and put it in `<note>` tag, I also found a code using
>>> `:walk` method. Lastly, I tried to convert `<div>` content to simple string
>>> and concatenate that in `RawInline` type:
>>>
>>> ```.{lua}
>>>   if elem.t == 'Div' and elem.classes[1] == "replace-me" then
>>>     content = pandoc.utils.stringify(elem.content)
>>>     return pandoc.RawInline('html', '<note>' .. content.. '</note>')
>>>   else
>>>     return elem
>>>   end
>>> ```
>>>
>>> But none of that is working. I would like to ask you, how to work in
>>> general with `pandoc_walk` or `:walk` (are they the same?) and how to deal
>>> with my specific situation?
>>> Thank you very much for any help, I ope that afterwards I will be able
>>> to help myself a little bit more :)
>>> Regards Tomas
>>>
>>> --
>>> 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-discus...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/pandoc-discuss/590abdf0-6bc5-4f37-a978-a46ad5cff5a8n%40googlegroups.com
>>> <https://groups.google.com/d/msgid/pandoc-discuss/590abdf0-6bc5-4f37-a978-a46ad5cff5a8n%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/aea85ef4-af50-46e5-8e21-d677801cb971n%40googlegroups.com
> <https://groups.google.com/d/msgid/pandoc-discuss/aea85ef4-af50-46e5-8e21-d677801cb971n%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/CADAJKhD6WVs3UxD0Dt0bYLA8mE4-A4n%3DQyyKrwkT%3DMYO%2BQNaVg%40mail.gmail.com.

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

  parent reply	other threads:[~2022-01-12  9:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-10 14:32 Tomáš Kruliš
     [not found] ` <590abdf0-6bc5-4f37-a978-a46ad5cff5a8n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2022-01-10 18:46   ` BPJ
     [not found]     ` <CADAJKhAzeK-kPd7yHWbtGX=363VvcgFUj8gt_vEMUXfGkBd+ug-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2022-01-11 14:07       ` Tomáš Kruliš
     [not found]         ` <aea85ef4-af50-46e5-8e21-d677801cb971n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2022-01-11 20:56           ` Bastien DUMONT
2022-01-11 21:25           ` Bastien DUMONT
2022-01-12  9:45           ` BPJ [this message]
     [not found]             ` <CADAJKhD6WVs3UxD0Dt0bYLA8mE4-A4n=QyyKrwkT=MYO+QNaVg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2022-01-13 14:40               ` Tomáš Kruliš
2023-06-26 19:12       ` Ioan Muntean
     [not found]         ` <ae032a8d-4d0a-4608-b479-61965cee2793n-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2023-07-05 15:44           ` BPJ

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CADAJKhD6WVs3UxD0Dt0bYLA8mE4-A4n=QyyKrwkT=MYO+QNaVg@mail.gmail.com' \
    --to=bpj-j3h7gcxpsitlodktgw+v6w@public.gmane.org \
    --cc=pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).