From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/29949 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 21:25:30 +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="25928"; mail-complaints-to="usenet@ciao.gmane.io" To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-X-From: pandoc-discuss+bncBDCINCES2QJRBJPM66HAMGQEPY5ROJY-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Tue Jan 11 22:27:03 2022 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane-mx.org Original-Received: from mail-wm1-f57.google.com ([209.85.128.57]) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1n7OfX-0006PD-6X for gtp-pandoc-discuss@m.gmane-mx.org; Tue, 11 Jan 2022 22:27:03 +0100 Original-Received: by mail-wm1-f57.google.com with SMTP id r2-20020a05600c35c200b00345c3b82b22sf2292055wmq.0 for ; Tue, 11 Jan 2022 13:27:03 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1641936422; cv=pass; d=google.com; s=arc-20160816; b=KJcksmakLB2VpesioD58leXxptCtc952uxbiZZmMOtb9Btxt4OU8owL043uzcaU9fE 3X8iB5qh9DZk26du8XwC6X/Z4awYSQzwv81mslxneELEVsG5btz78Mp04QmbyPUyYmkv 77KPS7V1YwEs2j/tY5lda4REcltHyO/7F/7t/eJs9IuxCuvHR/tL0MkCxMn5MvKZ3o6b qmJU6Mx6Xjqp0OygLkxX4PNb3Ahb9NCUsL7fL2zop5g7f4y76XRgf6zbbf0mY40aVm63 PhNnu50dD9DYenrmNiu3f7TByTATKrvrtCrnWmoFd37wx8PfzHwihY4X/tp6h4fF7tPr OXkg== 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=FtoEMUt9U2nN1YcyiDbW8xmC5owvDoH/bAnyrd/G5Y4=; b=NmZYDY5H2sxfFpDuIcxrTj/4JEKK1yxVDFGmoApIdQyZcEZqUJCAtp84s6IRRSFjdC bhwxRpqulI+UQQaAg4wOx9Y7rJUwitzItlvtw+5u2DQdXdCZwGU6EoW7LKhV44yO9trT zVtCnbD212tcqoxzxPpcuJEG5AUNZcJFtHHpTpl8UYz+H35uJSy9jB34UwPQVCawYeu6 /f01KdaeG3w/fx+M17PnAaq+2RXGMP4I8pGdD8EbUFrBKquqRRdDj9PZM5N29+3Ec1fk qVIDMY68riHPlpjk5fbQfpBNMiMqOVdcfTPwsBUd+z325cVSjpyb0IzaVP0921crAJdl ANTg== ARC-Authentication-Results: i=2; gmr-mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=GtxtjnzP; 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=FtoEMUt9U2nN1YcyiDbW8xmC5owvDoH/bAnyrd/G5Y4=; b=X0hl4PSYhQt28SB5VUE7acELE5WmnDzSrvkL/ziVp9gKu7aYVsdFf/dw9LBB4Z00Hr vLlcz+FrVv+Bs4E/07BEDgUjZMJwaVMXYhfYOZc1vbpgVS1njEgvBCBUEHgwqq+8IcH0 2Lcn7xm64TSGRhwfyEvw7eaqwDA+rUB19QNgZ9gBe6He1Si4hDORPUxsKr+KhmCRlOS7 FC4nb4PZEVuYvobqvJKx+mO4i9fTYuJb+wUmc5wyWlVH25HImgfArvkorITbHnKfCkRm 8IKwxfgUgS3a+Qkjub56M9oDw3yDrwo29vcuWLbEXiUDr75UvT4L/rqd8A8Q+MvP2Lc/ cbog== 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=FtoEMUt9U2nN1YcyiDbW8xmC5owvDoH/bAnyrd/G5Y4=; b=VUBrOpKKjbD94t0sFkc7rkg8lkKBKwVlH573m81jcWIbDKw2ONQnTod4WwJCkyHAMo ah5WVT/rca95IcdXLzd7z8tZroU/rtY2F6LHA4Cn3PUkh6jBAD37HPuITQivRyJXTVjy GgGgAz0I4R7AbFdvfNqZv1dLxjvK58KlMt2oYosE7GScJ36gL/CFQfI/h2Rpg1h1gK3X swvL3n7Z7Y3qn8SxeBTCSUYy+L/wbDBVq1uMa3BVmISePkFPm9pgAoW4DGW+VBChijzW R7bof9stL00NHgRh8QSpfIB2Kx0AyHuSWLJCW73Uce8u Original-Sender: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org X-Gm-Message-State: AOAM532xhRDYihs7gYgJ9i5YO3rAL7BqVqkkGXVEsTaQ7vbopF7yxaAf DexNnLEPl2PyXsLk8P5pYkE= X-Google-Smtp-Source: ABdhPJyTwWjlenLAxtrqCUBQY02GvoHIR4asewqbos96Xy5iUgROHQS5iOT/u19Rh6EL8da4p4e+6g== X-Received: by 2002:a05:6000:1acb:: with SMTP id i11mr5218492wry.244.1641936422638; Tue, 11 Jan 2022 13:27:02 -0800 (PST) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:a5d:53d2:: with SMTP id a18ls242155wrw.1.gmail; Tue, 11 Jan 2022 13:27:00 -0800 (PST) X-Received: by 2002:a5d:6d8f:: with SMTP id l15mr5281747wrs.466.1641936420292; Tue, 11 Jan 2022 13:27:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1641936420; cv=none; d=google.com; s=arc-20160816; b=fCdyGdlqNez8O6Smb/xSITsnrbt1vGByEypmxjQhm/DorlgsLj5o/QOQnLU/f8/r1S ik/gEyToqin97MVvb4e5mGj2okCin6NnF1hQ84EnbzUBYXXunNzEt78EyjT8TqO4nahl vTmd5g+J70hJvW4sQxAihwHAPc/6OeAKbkGZ/Gnt9YHM5Dkcs+Ysp+QFxuewempdS3At pRDE1yL4BUX9iSsE/FtH2Pw5T/S6/tSuq+fNPomBibXzT357D85nS0vJrhd5nV0WEYGk 8ciNgAu2vy88uRTAxvcszi8pXIqVgfkKZqFM0+jJMSKhIz8EEXGhdO81NrCDcB3RiaDv H1WA== 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=lUE8tZ94HYwK3yKQxlB8bw2jXvBe4ewsEfZdBTF9pig=; b=IuNoJGT/TFs/jZtktIydHNna8EwfjKivITHDCK8FBBLQdHKfjw/g+JXMrFhPszuU/+ qZ/k4/7QPWnyvbQSNwyD5mV+7/T8270fTE8pDo5RBpVHGidwSYlBf9lbPiAiRe4SRVfh laWvjB0a18dbReX8QyhHxFqOj3Dy7Yk3+pWvU6mABto1Y0QOxBuAuMoasaQw5qArQo4Z KWlQ2u8GU9bO+o5NyRaeriMOHoG2RyhtVNAJgHJWczt7kb2oRvwJsn7Y/adhZB2YqG+9 uVABnBB9VbnNlYpJeV1IRQNUHzrwi42fV5Yt+RMik3tH5Fk6ywSjNjcb85RXMMS2hk5Q nigQ== ARC-Authentication-Results: i=1; gmr-mx.google.com; dkim=pass header.i=@posteo.net header.s=2017 header.b=GtxtjnzP; 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 j23si273486wms.4.2022.01.11.13.27.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jan 2022 13:27:00 -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 DC87A240026 for ; Tue, 11 Jan 2022 22:26:59 +0100 (CET) Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4JYP0M3JF2z9rxW for ; Tue, 11 Jan 2022 22:26:59 +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=GtxtjnzP; 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:29949 Archived-At: And yes, top-level functions for Str will apply inside the block too. If yo= u wan't to "protect" strings in this block, I would suggest the following: ```.md All this will be changed. ::: {.protected} Here not. ::: All this will be changed. ``` ```.lua function Str(el) el.text =3D 'changed' return el end local protect =3D { Str =3D function(el) return pandoc.RawInline('protected', el.text) end } local function protect_protected(div) if div.classes:includes('protected', 1) then return pandoc.walk_block(div, protect) end end local function Str(str) str.text =3D 'changed' return str end local function to_str_again(rawinline) if rawinline.format =3D=3D 'protected' then return pandoc.Str(rawinline.text) end end -- Here we change the execution order of the functions return { { Div =3D protect_protected }, { Str =3D Str }, { RawInline =3D to_str_again } } ``` pandoc -L test.lua test.md

changed changed changed changed changed

Here not.

changed changed changed changed changed

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/Yd31yvv71qtrABKF%40localhost.