From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/29948 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Bastien DUMONT Newsgroups: gmane.text.pandoc Subject: Re: How to manipulate with Block elements with Lua filters Date: Tue, 11 Jan 2022 20:56:50 +0000 Message-ID: References: <590abdf0-6bc5-4f37-a978-a46ad5cff5a8n@googlegroups.com> Reply-To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="1491"; mail-complaints-to="usenet@ciao.gmane.io" To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-X-From: pandoc-discuss+bncBDCINCES2QJRBGG766HAMGQEZTKQNYA-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Tue Jan 11 21:59:07 2022 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane-mx.org Original-Received: from mail-wm1-f59.google.com ([209.85.128.59]) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1n7OEU-0000BP-QS for gtp-pandoc-discuss@m.gmane-mx.org; Tue, 11 Jan 2022 21:59:06 +0100 Original-Received: by mail-wm1-f59.google.com with SMTP id o185-20020a1ca5c2000000b003478a458f01sf504210wme.4 for ; Tue, 11 Jan 2022 12:59:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1641934746; cv=pass; d=google.com; s=arc-20160816; b=0VeM3ZTVDXbYvdZOCbyWd11fw+CsPROZT5cpV7dJmBRGaOCi8SysaYuKLT9hgO8ADE b/0hmhD+z34BHKqqIypoGoksZFdc0vjWFOg0Mdp4heDL7efIXFOTmdvRZ5y6Ha1rwNjG SBKRTtfjHrrzx34OsHP1T76WWhzxN5j+2tEvc7thCWH/hgiMIkbbXuIGZ+DCakwmoSZX VRW0JKwcXNCkpVNjjVEbFpD1ptuHvizQQ3Erju1nxmaKw7lTIwMwpVYpE0Ybkx25U5Fz tEJIUGG68cOq1U4oF9Scrl81U6nLeKN2fCjUcTikB2KuZ5UoId7jhHV1ilUwahUndgK0 ms/g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:reply-to:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:to:from:date:sender:dkim-signature; bh=Wyy8yJ3Zf8D3ZD7ReAavuveAsunM85tk1ho2/Fpis6A=; b=lXP6mhi5r+9BWf1RqhJ8sgbZbQXUfeg+gZyRnrBDmHDgBTpTS1NPGbt3T3lpFJIoji RwOI3FeUKnAqw8QzcVnRa4i9jE5x7WgLHMSwRn+DStTyahQ4fysT2TOYBQ036W7RA34M p7LI8jrqg1RFCilgoEDFsRACG8zGSj4RHOoZVFpTwCWPzkOzuKm2g5qgkMKR3g8YSTFM ZKkdMrKbnA0AJK9IB1ozEe7jQfL6/CJU742JlrOy7yWr6IjsmOfY7zHho7w6/swiUQNC eFTuC9U+Stt9O5bZmVhIZ7GuFZkGtEgbqPIRYg8A54oABCTnaZK+Mq9v9jQmWXlP8gNb yEZQ== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=HGCCNpza; spf=pass (google.com: domain of bastien.dumont-VwIFZPTo/vqsTnJN9+BGXg@public.gmane.org designates 185.67.36.65 as permitted sender) smtp.mailfrom=bastien.dumont-VwIFZPTo/vqsTnJN9+BGXg@public.gmane.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=posteo.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20210112; h=sender:date:from:to:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :x-original-sender:x-original-authentication-results:reply-to :precedence:mailing-list:list-id:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=Wyy8yJ3Zf8D3ZD7ReAavuveAsunM85tk1ho2/Fpis6A=; b=CUCG5IWcNKJDJReHS59P4U0mTyLR8BgSIMUqeAS+HzvUeYc7h3q+mhpr8QMBj8C2n6 UBnyATtVfUj8TPR59o2D9s5rkMEvT8Q+hSovxkYSFHvli0WyZ/8e6AhIZY34P7KQ+1OX LWHd0n7CufDtZNo1IjBK5PgJrdc5BFeyM0Uuls0Ln2kKl1iWOHYytWuZgyLxl/iyeWNq j3qp1tO2iHCZST0JdrNroMMcN5EoN6bUj/PHwIoteXjrC7uLO3FTtbKs9Qe6A0HcM/Nk VGJSvFtuKteiba33CBkUB2qLx5DnmoyjhMqBX++/t4eD4CBzEnIrxKSPGu0bS92grUrp 6sNA== 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:subject:message-id :references:mime-version:content-disposition :content-transfer-encoding:in-reply-to:x-original-sender :x-original-authentication-results:reply-to:precedence:mailing-list :list-id:x-spam-checked-in-group:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=Wyy8yJ3Zf8D3ZD7ReAavuveAsunM85tk1ho2/Fpis6A=; b=VpAq/0HkI4sOP9UCNQdTBUpH4zJXXTRRASoqX53RNAjlmt/1Iom6DCk/YW4UbuNzuP PBnzxS4SsiTKLEgTG48oKi3q40Ab9pxlSsuxT1ANLgSL44UvZR2NwxhauRVc9+TPeJ6V 8yYLmD5zEnZU3mGYyrv4U65p/n0t/+DFxIlnImH8+HgPFx61b1Dq+D0JlxvM2KSCaY+f ZPkgtzQpBwUuHOcDjh+drZjCrLdlabAFwAqckw1erY81yxAmY2g3r5hreKxIN3IWG9Wi aj7m0nY/hA1mLNUHkvefqpDa85WAeRak00i5Y2QfnYRZ Original-Sender: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org X-Gm-Message-State: AOAM533x6ThnD3e4GmQ5KIK1843LhtQQX7Gz7nEGeop6s9NQs3I9r2Wl Rs5+5IZEfWV9v04pps2xVso= X-Google-Smtp-Source: ABdhPJztMDSmd/AZjVxJxQGT4f9kgJBjWVN2Do/cOajHksRp/4ZbXDmigqIY7E7u376fEjketCJejw== X-Received: by 2002:a5d:4ad2:: with SMTP id y18mr5346401wrs.141.1641934746316; Tue, 11 Jan 2022 12:59:06 -0800 (PST) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:a1c:256:: with SMTP id 83ls1579743wmc.3.canary-gmail; Tue, 11 Jan 2022 12:59:04 -0800 (PST) X-Received: by 2002:a05:600c:3844:: with SMTP id s4mr3916409wmr.124.1641934743999; Tue, 11 Jan 2022 12:59:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1641934743; cv=none; d=google.com; s=arc-20160816; b=dQYwrI5OvUtBJV2ALfDLGCSL59SZaH7EfCrXtf+5iwQCpRWVoK7MfCwYkZtwUFfwbm SsnPUpmbpp2A2PYQQhINriyZu20e+gGmMPLCpg3ztkzSKJtH0cI0gcb2uMMaz8gjPbDs fnRhlqg9HLpVNpsNQO5dFfEm1zUF3M77ps2zyr45Jr6fziO4Y1wRYUs0zp/3TU6DfTt3 pQT2L6sn3MnkoULdlapcUCJQfm6rZVmbqyVgbMUXscWVu87uwRGOvXLXf8HbjRdP4Xrk bUVNyuJ55M6VnhzPNZUQA4x/hudvAxGmrTiqEYRs6+OQET1IkBxUKIB1UjZvNVVxtMI+ EsNw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:to:from:date :dkim-signature; bh=lfQbQ5mr3QwdCFiOEY+TTx576CJ31daTQouaEpJYc1g=; b=nwzr9ZF88u3J89X4UVzBRkbrwz6emZnbXTz05v9o9pUk8dr8kfn1eanPNjFTdhnERz xeItuQURe0MEX2ZRvkf7UR5Z+aO56eiYP3Mb2Vk0kBa6bgrkUb9sk/m/0oRDCjK5DtEN CU3zo5lGl4N77U4PKRnsknjwSHmsZGR5hcazW8mHZJMHU+ELQk0u17yrSgafSECkedSz M7D4AH6Gjyw1IQ5JteWli347XIquAAUws1C6K40iVyIm95P2s2SBMIHLHX3h3f1fiWiy B/aSMPRXTzmo1Xgk5MkG30wEOBt2U1vr5cQ3TgvHGX0lot9N9yhq70050GeKjCITD1nZ lKyg== ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=HGCCNpza; spf=pass (google.com: domain of bastien.dumont-VwIFZPTo/vqsTnJN9+BGXg@public.gmane.org designates 185.67.36.65 as permitted sender) smtp.mailfrom=bastien.dumont-VwIFZPTo/vqsTnJN9+BGXg@public.gmane.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=posteo.net Original-Received: from mout01.posteo.de (mout01.posteo.de. [185.67.36.65]) by gmr-mx.google.com with ESMTPS id bi17si92194wmb.0.2022.01.11.12.59.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jan 2022 12:59:03 -0800 (PST) Received-SPF: pass (google.com: domain of bastien.dumont-VwIFZPTo/vqsTnJN9+BGXg@public.gmane.org designates 185.67.36.65 as permitted sender) client-ip=185.67.36.65; Original-Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id ABDBF24002C for ; Tue, 11 Jan 2022 21:59:03 +0100 (CET) Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4JYNN71sLqz9rxN for ; Tue, 11 Jan 2022 21:59:03 +0100 (CET) Content-Disposition: inline In-Reply-To: X-Original-Sender: bastien.dumont-VwIFZPTo/vqsTnJN9+BGXg@public.gmane.org X-Original-Authentication-Results: gmr-mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=HGCCNpza; spf=pass (google.com: domain of bastien.dumont-VwIFZPTo/vqsTnJN9+BGXg@public.gmane.org designates 185.67.36.65 as permitted sender) smtp.mailfrom=bastien.dumont-VwIFZPTo/vqsTnJN9+BGXg@public.gmane.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=posteo.net 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:29948 Archived-At: You should use it when you want to apply a filter inside specific blocs onl= y. The applied filter is a table of key-value pairs, where the key is one o= f Pandoc's types and the value a function. Whith this input file: ```.md Here emphasis is _italics_. ::: {.change-emphasis} Here emphasized and **bold** strings are _underlined_. ::: It's _normal_ again. ``` And this filter: ```.lua -- The filter to be applied inside "change-emphasis" divs. -- Notice that as we are in a table, entries are separated by a comma. local to_underline =3D { Emph =3D function(el) return pandoc.Underline(el.content) end, Strong =3D function(el) return pandoc.Underline(el.content) end } function Div(div) if div.classes:includes('change-emphasis', 1) then -- We return the transformed div. return pandoc.walk_block(div, to_underline) end end ``` you will get: pandoc -L test.lua test.md

Here emphasis is italics.

Here emphasized and bold strings are underlined.

It=E2=80=99s normal again.

Le Tuesday 11 January 2022 =C3=A0 06:07:32AM, Tom=C3=A1=C5=A1 Kruli=C5=A1 a= =C3=A9crit : > Oh, so the `div` content is a `table` in lua terms? That didn't occured t= o me, > even though it probably should ... I actually managed to replace it not a= s a > `RawInline`, but `RawBlock`, like so: >=20 > ```.{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 > ``` >=20 > 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 cl= earer > 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=C4=9Bl=C3=AD 10. ledna 2022=C2=A0v=C2=A019:46:37 UTC+1 u=C5=BEiv= atel BPJ napsal: >=20 > 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: >=20 > ``````lua > -- Create these only once, for speed and resources saving! > local pre =3D pandoc.RawBlock('html', '') > local post =3D pandoc.RawBlock('html', '') >=20 > function Div (div) > =C2=A0 -- The order of the classes shouldn't matter! > =C2=A0 if div.classes:includes('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 > `````` >=20 > Den m=C3=A5n 10 jan. 2022 15:33Tom=C3=A1=C5=A1 Kruli=C5=A1 skrev: >=20 > Hello, >=20 > I would like to ask how you should, in general, detect and manipu= late > with Pandoc `block` elements. Currently, I am trying to replace `=
class=3D'replace-me'>` tag with `` tag in similar (highly > simplified) HTML file: >=20 > ```.{html} > > >

First line.

>
This should carry on to > converted document.
>

End.

> > > ``` >=20 > I have tried to detect the `
` tag, use `walk_block` to get t= he > `
` content and put it in `` tag, I also found a code u= sing > `:walk` method. Lastly, I tried to convert `
` content to sim= ple > string and concatenate that in `RawInline` type: >=20 > ```.{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 return pandoc.RawInline('html', '' .. content= .. '') > =C2=A0 else > =C2=A0 =C2=A0 return elem > =C2=A0 end > ``` >=20 > 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 ho= w 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 >=20 > -- > You received this message because you are subscribed to the Googl= e > 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 [1]https://groups.google= .com/d > /msgid/pandoc-discuss/ > 590abdf0-6bc5-4f37-a978-a46ad5cff5a8n%40googlegroups.com. >=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= email > to [2]pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit [3]https://groups.google.com/d/m= sgid/ > pandoc-discuss/aea85ef4-af50-46e5-8e21-d677801cb971n%40googlegroups.com. >=20 > References: >=20 > [1] https://groups.google.com/d/msgid/pandoc-discuss/590abdf0-6bc5-4f37-a= 978-a46ad5cff5a8n%40googlegroups.com?utm_medium=3Demail&utm_source=3Dfooter > [2] mailto:pandoc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org > [3] https://groups.google.com/d/msgid/pandoc-discuss/aea85ef4-af50-46e5-8= e21-d677801cb971n%40googlegroups.com?utm_medium=3Demail&utm_source=3Dfooter --=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/Yd3vEifRjimcUHBI%40localhost.