From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/29962 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?Tom=C3=A1=C5=A1_Kruli=C5=A1?= Newsgroups: gmane.text.pandoc Subject: Re: How to manipulate with Block elements with Lua filters Date: Thu, 13 Jan 2022 06:40:53 -0800 (PST) Message-ID: References: <590abdf0-6bc5-4f37-a978-a46ad5cff5a8n@googlegroups.com> Reply-To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_318_1291584620.1642084853465" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="16962"; mail-complaints-to="usenet@ciao.gmane.io" To: pandoc-discuss Original-X-From: pandoc-discuss+bncBC32RZ7W74FRB5XTQCHQMGQEU2TGOJQ-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Thu Jan 13 15:40:58 2022 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane-mx.org Original-Received: from mail-oi1-f183.google.com ([209.85.167.183]) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1n81Hd-0004EN-58 for gtp-pandoc-discuss@m.gmane-mx.org; Thu, 13 Jan 2022 15:40:57 +0100 Original-Received: by mail-oi1-f183.google.com with SMTP id n2-20020acaef02000000b002c6c1c37a03sf3897556oih.21 for ; Thu, 13 Jan 2022 06:40:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20210112; h=sender:date:from:to:message-id:in-reply-to:references:subject :mime-version:x-original-sender:reply-to:precedence:mailing-list :list-id:list-post:list-help:list-archive:list-subscribe :list-unsubscribe; bh=/uJaYnackTsS+ek0nu5kcBwMzGjyTvF97qU1bvm3VtI=; b=mJRpoqfAJUgYhgV4VtkcZ34DZk8057xa6uqjgc9Lnr7sPoORq7KDqhoUXo/B+ci45/ /HJUpi6HfX2IJghydQCi1rw21Y7wOY0VjXSK0Dv9QIiuKE2VHWCJDagx0d0ulFc0L/K6 PfQx9Wnnd0RJNXuTPwVOec5jUXnb9FNIbc8t9tjVrHXVrwcwdMyd5OUMLjq/NaS1Hl7S Z5mAK64oeVhWznrw7CYQsPH5VW9/dvFFoJQnCU5owZ3TINjdTC9+cDnAQlP4Lecc8MaJ gTw5O1KYQ4QLizH8pCC/9Mp8Zr9IPhaLVYCBsbMUEhqrAsXk9YuMuCaxiQ4DZ4WQ3/VR IznA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=integromat-com.20210112.gappssmtp.com; s=20210112; h=date:from:to:message-id:in-reply-to:references:subject:mime-version :x-original-sender:reply-to:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-subscribe:list-unsubscribe; bh=/uJaYnackTsS+ek0nu5kcBwMzGjyTvF97qU1bvm3VtI=; b=CPXExcRZo56O21RyI2JhMSawX0EGRw00+7hMeo3TCHDpNdB2FNeNj+U+eJ4GHJP+XC xYSLaQX7hqo3X8SJj3PzlTtFY0VM0/6y3GjhFhZIN0CVq3jB9lnDPYSRApSqfyAClmdU mEakTJx6+lomaY8/jtRrOfRycj0CKfQLgpZAuiy1c0PYLjcaoGZwsOhGyEu1hmnKLlOy +RXncPDwjVe5HelUDCmRemaxzeblL2RP330MaTF8XOUQcq1uhDNzBFO102EnbG1IAfpE Ho08wIpRODe5rxqYJucntdgRRLewPaCh9HAVr4dTuA2CyoZHM0iIkLNEiRa/QFCD/ZfO PvAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=sender:x-gm-message-state:date:from:to:message-id:in-reply-to :references:subject:mime-version:x-original-sender:reply-to :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post :list-help:list-archive:list-subscribe:list-unsubscribe; bh=/uJaYnackTsS+ek0nu5kcBwMzGjyTvF97qU1bvm3VtI=; b=ZpbBeR2e0LvAttY+UIJx7OBPCwdrk1M0LV/jdEAGyM9hDBXYSXtf/MBqoq0a2cmZIB cxTKzVl5LfBze3ZrKYGr9uSd+3ZEL67fCNo91/ZEXqZg+kcdh9DNVCGD8/7dUV3SiAEk e9oOUBWhr+qfstu7gPONHgRb7j6TZKll8/TKVnEFGPL0SbmZTjmKJ7XNp6Syysyc0SOw X0B+0VMXpR80CpP3d0BkNVdRwvDPNJ0wQnpta17TJUNkmXmJQKmufuoXrNrbvwpeDhE9 1xPcCuSPVP8JGnBWBA2r5WrEsyjscTOG81DIyVuMWR1h8WB6lkyhzIfst+CrXWS0C+Ds y7mw== Original-Sender: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org X-Gm-Message-State: AOAM532TMhnaPjgGyNRN+QOzO9oVVrAk2S+W6NCYT0CvkeeIVrwLr4m1 XI/ET9XBp3JAkTY1aHjTSMM= X-Google-Smtp-Source: ABdhPJzZ9hBjwAY6Xs9+875O80h76TOb4jSsOFB0Y0gbjuzSZUF99m17s5DL23+caOxg6nMlkX5gbg== X-Received: by 2002:a05:6808:300d:: with SMTP id ay13mr8867105oib.144.1642084855981; Thu, 13 Jan 2022 06:40:55 -0800 (PST) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:a05:6808:1a2a:: with SMTP id bk42ls839572oib.9.gmail; Thu, 13 Jan 2022 06:40:54 -0800 (PST) X-Received: by 2002:a05:6808:300b:: with SMTP id ay11mr3337243oib.120.1642084854103; Thu, 13 Jan 2022 06:40:54 -0800 (PST) In-Reply-To: X-Original-Sender: tomas.krulis-hR4I4Euo7CWBik42HM7KXg@public.gmane.org Precedence: list Mailing-list: list pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org; contact pandoc-discuss+owners-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org List-ID: X-Google-Group-Id: 1007024079513 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Xref: news.gmane.io gmane.text.pandoc:29962 Archived-At: ------=_Part_318_1291584620.1642084853465 Content-Type: multipart/alternative; boundary="----=_Part_319_1206195093.1642084853466" ------=_Part_319_1206195093.1642084853466 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Thank you BPJ and Bastien for great examples and clarifications around the= =20 `walk_block` function and how to do the specific changes I was struggling= =20 with. In this specific case, I could omit the formatting, but at the same= =20 time, I didn't know that `pandoc.utils.stringify` does that. About the string protecting, it is a great idea, but I have to study a=20 little bit more because I am getting lost there :) Thank you guys for your help! Dne st=C5=99eda 12. ledna 2022 v 10:46:13 UTC+1 u=C5=BEivatel BPJ napsal: > The div content is an enhanced `pandoc.List` table with some useful=20 > methods: > > https://pandoc.org/lua-filters.html#module-pandoc.list > > The filter documentation notes where you will find List objects in the=20 > type descriptions. > > BTW you probably don't want to use the return value of=20 > pandoc.utils.stringify the way you did because it strips all formatting. = If=20 > you just inject the HTML tags the way I showed formatting in the div=20 > content will be preserved. > > Den tis 11 jan. 2022 15:08Tom=C3=A1=C5=A1 Kruli=C5=A1 skrev: > >> Oh, so the `div` content is a `table` in lua terms? That didn't occured= =20 >> to me, even though it probably should ... I actually managed to replace = it=20 >> not as a `RawInline`, but `RawBlock`, like so: >> >> ```.{lua} >> if elem.t =3D=3D 'Div' and elem.classes[1] =3D=3D "replace-me" then >> content =3D pandoc.utils.stringify(elem.content) >> return pandoc.RawBlock('html', '' .. content.. '') >> 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= `=20 >> function. From the examples in the pandoc official page it is not much= =20 >> clearer to me. I think I now understand that they are used to=20 >> iterate/filter over the contents of the block that is being walked over.= =20 >> But if the block element contains only String elements (paragraph with= =20 >> simple text), then also Str-filter functions are applied to it? >> Maybe my confusion can be voiced like this: When should I use=20 >> `pandoc.walk_block` function to get what I want and cannot use something= =20 >> else? Explained not only with code, but also with commentary ... >> Dne pond=C4=9Bl=C3=AD 10. ledna 2022 v 19:46:37 UTC+1 u=C5=BEivatel BPJ = napsal: >> >>> It is neither possible nor needed to convert the whole block to HTML=20 >>> 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 =3D pandoc.RawBlock('html', '') >>> local post =3D pandoc.RawBlock('html', '') >>> >>> function Div (div) >>> -- The order of the classes shouldn't matter! >>> if div.classes:includes('replace-me') then >>> local content =3D div.content >>> table.insert(content, 1, pre) >>> table.insert(content, post) >>> return content >>> end >>> return nil >>> end >>> `````` >>> >>> Den m=C3=A5n 10 jan. 2022 15:33Tom=C3=A1=C5=A1 Kruli=C5=A1 skrev: >>> >>>> Hello, >>>> >>>> I would like to ask how you should, in general, detect and manipulate= =20 >>>> with Pandoc `block` elements. Currently, I am trying to replace `>>> class=3D'replace-me'>` tag with `` tag in similar (highly simpli= fied)=20 >>>> HTML file: >>>> >>>> ```.{html} >>>> >>>> >>>>

First line.

>>>>
This should carry on to=20 >>>> converted document.
>>>>

End.

>>>> >>>> >>>> ``` >>>> >>>> I have tried to detect the `
` tag, use `walk_block` to get the=20 >>>> `
` content and put it in `` tag, I also found a code using= =20 >>>> `:walk` method. Lastly, I tried to convert `
` content to simple s= tring=20 >>>> and concatenate that in `RawInline` type: >>>> >>>> ```.{lua} >>>> if elem.t =3D=3D 'Div' and elem.classes[1] =3D=3D "replace-me" then >>>> content =3D pandoc.utils.stringify(elem.content) >>>> return pandoc.RawInline('html', '' .. content.. '') >>>> else >>>> return elem >>>> end >>>> ``` >>>> >>>> But none of that is working. I would like to ask you, how to work in= =20 >>>> general with `pandoc_walk` or `:walk` (are they the same?) and how to = deal=20 >>>> with my specific situation? >>>> Thank you very much for any help, I ope that afterwards I will be able= =20 >>>> to help myself a little bit more :) >>>> Regards Tomas >>>> >>>> --=20 >>>> You received this message because you are subscribed to the Google=20 >>>> Groups "pandoc-discuss" group. >>>> To unsubscribe from this group and stop receiving emails from it, send= =20 >>>> an email to pandoc-discus...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>>> To view this discussion on the web visit=20 >>>> https://groups.google.com/d/msgid/pandoc-discuss/590abdf0-6bc5-4f37-a9= 78-a46ad5cff5a8n%40googlegroups.com=20 >>>> >>>> . >>>> >>> --=20 >> You received this message because you are subscribed to the Google Group= s=20 >> "pandoc-discuss" group. >> To unsubscribe from this group and stop receiving emails from it, send a= n=20 >> email to pandoc-discus...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > To view this discussion on the web visit=20 >> https://groups.google.com/d/msgid/pandoc-discuss/aea85ef4-af50-46e5-8e21= -d677801cb971n%40googlegroups.com=20 >> >> . >> > --=20 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 e= mail 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/a56ac936-48ce-4cc8-9053-c28a9d66f6d1n%40googlegroups.com. ------=_Part_319_1206195093.1642084853466 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Thank you BPJ and Bastien for great examples and clarifications around the = `walk_block` function and how to do the specific changes I was struggling w= ith. In this specific case, I could omit the formatting, but at the same ti= me, I didn't know that `pandoc.utils.stringify` does that.
About the st= ring protecting, it is a great idea, but I have to study a little bit more = because I am getting lost there :)
Thank you guys for your help!<= br>
Dne st=C5=99eda 12. ledna 2022=C2=A0v=C2=A010:46:13 UTC+1 u=C5=BEivatel= BPJ napsal:
=
The div content is an enhanced `pandoc.List` table with s= ome useful methods:

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

The filter documentation not= es where you will find List objects in the type descriptions.

BTW you probably don't want to us= e the return value of pandoc.utils.stringify the way you did because it str= ips all formatting. If you just inject the HTML tags the way I showed forma= tting in the div content will be preserved.

Den tis 11 jan. 2022 15:08Tom=C3=A1=C5=A1 Kruli=C5=A1 <tomas....-hR4I4Euo7CWBik42HM7KXg@public.gmane.org> 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:

=C2=A0```.{l= ua}
=C2=A0 if elem.t =3D=3D 'Div' and elem.classes[1] =3D=3D &q= uot;replace-me" then
=C2=A0 =C2=A0 content =3D pandoc.utils.stringi= fy(elem.content)
=C2=A0 =C2=A0 return pandoc.RawBlock('html', &#= 39;<note>' .. content.. '</note>')
=C2=A0 else=C2=A0 =C2=A0 return elem
=C2=A0 end
```

<= /div>
But thank you for your solution, for me it was very educati= onal!
Another thing that I struggle with is to wrap my head aroun= d `walk_block` function. From the examples in the pandoc official page it i= s not much clearer to me. I think I now understand that they are used to it= erate/filter over the contents of the block that is being walked over. But = if the block element contains only String elements (paragraph with simple t= ext), then also Str-filter functions are applied to it?
Maybe my = confusion can be voiced like this: When should I use `pandoc.walk_block` fu= nction to get what I want and cannot use something else? Explained not only= with code, but also with commentary ...
Dne pond=C4=9Bl=C3=AD 10. ledna 2022= =C2=A0v=C2=A019:46:37 UTC+1 u=C5=BEivatel BPJ napsal:
It is n= either possible nor needed to convert the whole block to HTML within the fi= lter; rather you should just inject the start and end tags:

``````lua
-- C= reate these only once, for speed and resources saving!
local pre =3D pandoc.RawBlock('html', '<note>')
local post =3D pandoc.RawBlock('html', '&l= t;/note>')

functi= on Div (div)
=C2=A0 -- The order of the classes shou= ldn't matter!
=C2=A0 if div.classes:includes(= 9;replace-me') then
=C2=A0 =C2=A0 local content = =3D div.content
=C2=A0 =C2=A0 table.insert(content, = 1, pre)
=C2=A0 =C2=A0 table.insert(content, post)
=C2=A0 =C2=A0 return content
= =C2=A0 end
=C2=A0 return nil
= end
``````

Den= m=C3=A5n 10 jan. 2022 15:33Tom=C3=A1=C5=A1 Kruli=C5=A1 <tomas....-hR4I4Euo7CWBik42HM7KXg@public.gmane.org> 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 cl= ass=3D'replace-me'>` tag with `<note>` tag in similar (hig= hly simplified) HTML file:

```.{html}
&l= t;html>
<body>
<p> First line. </p>= ;
<div class=3D"replace-me another-class"> This s= hould carry on to converted document. </div>
<p>End.&= lt;/p>
</body>
</html>
```

I have tried to detect the `<div>` tag, use `wa= lk_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}
=C2=A0 if elem.t =3D=3D '= ;Div' and elem.classes[1] =3D=3D "replace-me" then
=C2=A0 = =C2=A0 content =3D pandoc.utils.stringify(elem.content)
=C2=A0 =C2=A0 re= turn pandoc.RawInline('html', '<note>' .. content.. &= #39;</note>')
=C2=A0 else
=C2=A0 =C2=A0 return elem
=C2= =A0 end
```

But none of that is work= ing. 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 a= ble to help myself a little bit more :)
Regards Tomas

--
You received this message because you are subscribed to the Google Groups &= quot;pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an e= mail to pandoc-discus...@googlegr= oups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid= /pandoc-discuss/590abdf0-6bc5-4f37-a978-a46ad5cff5a8n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups &= quot;pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an e= mail to
pandoc-discus= ...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups &= quot;pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an e= mail to pand= oc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To view this discussion on the web visit https://groups.google.com/d= /msgid/pandoc-discuss/a56ac936-48ce-4cc8-9053-c28a9d66f6d1n%40googlegroups.= com.
------=_Part_319_1206195093.1642084853466-- ------=_Part_318_1291584620.1642084853465--