From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/29951 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: BPJ Newsgroups: gmane.text.pandoc Subject: Re: How to manipulate with Block elements with Lua filters Date: Wed, 12 Jan 2022 10:45:57 +0100 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/alternative; boundary="0000000000007494a205d55f7024" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="10036"; mail-complaints-to="usenet@ciao.gmane.io" To: pandoc-discuss Original-X-From: pandoc-discuss+bncBCWMVYEK54FRBYWG7KHAMGQE3CE3VZI-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Wed Jan 12 10:46:13 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 1n7aCq-0002Wd-TF for gtp-pandoc-discuss@m.gmane-mx.org; Wed, 12 Jan 2022 10:46:12 +0100 Original-Received: by mail-wm1-f57.google.com with SMTP id m9-20020a05600c4f4900b0034644da3525sf1298680wmq.3 for ; Wed, 12 Jan 2022 01:46:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1641980772; cv=pass; d=google.com; s=arc-20160816; b=P853ChYWWMt6+rtqjcbhkjC/roIEO75Y5k+xBmJrWRn9G//zxZeWyiTIuYgW3gzptb vgKCV5AF8edq7LmSbnhb7SzjJfgyZP6RuFr2/Y8sI4fq6iMoqi3QixFAaXxs3j4+CEpR QiSTX9tb/AMHQs1F2L9JWbEIsOYF3ybEmWuQViJtYyTBWYbxEkHeTY6WvhdKNZ3UAjtr JBumIc/lc2EJ4RfReI5HINkCZQ5uu/jXKqFQy9tt6fdZFocKPfvzK8Yydlf2LiSEs0Ik 7mi2zBATzZBiKoGpxwv9iY2nQ/PhUp+pp5Ite8b6FzI/3EcfKxZw5+LLqnzq3Z1Sre5d ByuA== 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:to:subject:message-id:date:from :reply-to:in-reply-to:references:mime-version:sender:dkim-signature; bh=TpjySihkOe02VCn/dBfGJhOXLvts2Hf3BoqQacOSkro=; b=DxapvZep+lK9no/NiKPNNXkXwK2g7wSg1t9Q9wyMa33fHsu+DNmOYe3pJaZLOCrmwx iQ0/AzCUpjLISYJ6d2YZgKhgADFhPo3qbIsbwa3F7p71TmI17czpHyhF9uTGuA2pPK1e 1HCxBdpJS9hhjErB1dQxj3hsVaLOQM0ElAaQARxhQVS+UpiSsgPtbfGKnaFJ+UG0Ik2R SiVKiMTI9I3xHpsYcBzQaVX5oGmlZ/PhuLQeWD36rRALTagFauapj4xYWD8jsjJ9Hgq9 oIPH9anDUFNW/NjM7/r2tshwHBHvMpLtYTyWrafdwqKoqkqrsB5Dn/pER6MQvtV6ClMF cSpQ== ARC-Authentication-Results: i=2; gmr-mx.google.com; spf=pass (google.com: domain of melroch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org designates 209.85.167.41 as permitted sender) smtp.mailfrom=melroch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20210112; h=sender:mime-version:references:in-reply-to:reply-to:from:date :message-id:subject:to:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-subscribe:list-unsubscribe; bh=TpjySihkOe02VCn/dBfGJhOXLvts2Hf3BoqQacOSkro=; b=Rsq61F33bSQusLCGedlIYpN06l2y0aQ96+HBhh4G/EPeA4Ze05pnQ2b/MNj5euwyYb yAigmK+XfdOfZ5GCHH0YguwXsOTphQU3by2U1AzXNecmaiq7l5NYw42IkP9J98uGC17N YPZLCa1JOoU1xdg6X2KwGFzHGgG9frZIUH4w7uJKVRxxNQXnM3CVnKfbiLP5Na75TaWF K8t+7hDXNXntjUJsPGf9LhtlSqUCwhB/5qOKVUP3LqmMh1K6JvxzcTKPCu4vdfgSXbSj avgdwCT59RHvLNiHvEvvRh57SWA31XtaoRYyyEOkdBoh4K1c8lidy1hI88d/zl3BvFXv kDpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=sender:x-gm-message-state:mime-version:references:in-reply-to :reply-to:from:date:message-id:subject:to:x-original-sender :x-original-authentication-results:precedence:mailing-list:list-id :x-spam-checked-in-group:list-post:list-help:list-archive :list-subscribe:list-unsubscribe; bh=TpjySihkOe02VCn/dBfGJhOXLvts2Hf3BoqQacOSkro=; b=xdq8zpPDJeFeXtu6gJN/eTYRfvjFrCbt/WzzullRSuN3UlvQFpHVYCvqDNhSJYlM0y wWBKpjnCwshz9iOV7cItkHbGw6r/lzTvImsxiBaU5vGZnltikSaOdrxNxmadJUnpR3u7 RTSB377TxeL0hUAQWdFZSUU52TeZ+AyXnpXoJiF7TViYCde8CvUbMhtFXQsRp/V6bWqV mT4dPbgoi42hEsfwGZKmFS9SUMSmfoV99hjaN3dI7+kj4OBOvmvTwfHHn++grLadPuJa XS3hwCQDWcRc69bRar/hPNE0sviePB2tWir7jVmV92II2TaC8V2Sb8lBbCe6xriGa69s PeHw== Original-Sender: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org X-Gm-Message-State: AOAM533ICdF/QkKCHn4aAcQES7VMYToKfvfDAbVYBmMLgB6nfS+B1cvo t4Gq3XnkqYGoCIuQaNBIbzA= X-Google-Smtp-Source: ABdhPJzkjw6yewOArUmzoi6JHao7VpVTfsNaD3xp7mFzG2ViCcyJaqfm7LSd2+eXaZmYRgf8iLvDTg== X-Received: by 2002:a05:600c:28c8:: with SMTP id h8mr5804577wmd.37.1641980772475; Wed, 12 Jan 2022 01:46:12 -0800 (PST) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:adf:c7cd:: with SMTP id y13ls273520wrg.2.gmail; Wed, 12 Jan 2022 01:46:09 -0800 (PST) X-Received: by 2002:adf:d1e3:: with SMTP id g3mr29856wrd.714.1641980769710; Wed, 12 Jan 2022 01:46:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1641980769; cv=none; d=google.com; s=arc-20160816; b=byq1EewoInnHvp1L2h/fiPl6hkmIc1BtL/8RX1Gp3/cU88oYA6cgWZhZgZ20E/Y82p nywy0eWCVLKw2CxUM2NXYu0rq5/5AsD1mnoP2XC90dTQreu5IH+oW4KSqUHDDP81a/KO e1PAPYDJfa0PVDJREZXOMzRDjIdp8Y5TDSvRlMTf5XsY/+/1zgrVU62LKguk64NwW/WR zhJntbiYM+dKVxGTFMPxiFFmNWAsDqRvGktPp40D8iEiB7VSPWMAdfoQOHHHdXtCqPwM Xd5hvzU/RdyGQBm2jeGhCx97KcDkz0xKwn1XV3QnbtYxQ5V5/+gPQ4MsX44zG18RDgzU xS3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:subject:message-id:date:from:reply-to:in-reply-to:references :mime-version; bh=0phegnmoo1/xExicIJMm7NEQDr46f7uXpbEL9iK8XMo=; b=Of568IYdqVHkg5UZHufkJ56K7Bd2u2qAfxqZBhPXoa80nRe1qc0h6iAiCK5tiSNXeC cWFARKhUsPBAhG/+BCqsdmK4EvFEFmUpDnmrfm/+EdvhF2dqwT2InUDwbpFkkA+OcZSy p6muvOuuonghy9bGlmOndB2whoLE8s+x+F9yjijiEzowO62qtqDX7Gv5GKR7FscE0dX2 VQ6ihfHZ+EIkkNpJu6BHD3s3p8l+wsnQXLorgC7OGRntgU7aA9q6Q6l+e8GS6V7MvDDg pWXKRuYpWxBtTZd4XT8u7epzXL9NHDuaCR8BEU1xGKO9ZTnsLorNZc/Q0zRef7tjGOqv bdbQ== ARC-Authentication-Results: i=1; gmr-mx.google.com; spf=pass (google.com: domain of melroch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org designates 209.85.167.41 as permitted sender) smtp.mailfrom=melroch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org Original-Received: from mail-lf1-f41.google.com (mail-lf1-f41.google.com. [209.85.167.41]) by gmr-mx.google.com with ESMTPS id s202si230156wme.2.2022.01.12.01.46.09 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 12 Jan 2022 01:46:09 -0800 (PST) Received-SPF: pass (google.com: domain of melroch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org designates 209.85.167.41 as permitted sender) client-ip=209.85.167.41; Original-Received: by mail-lf1-f41.google.com with SMTP id j11so6122279lfg.3 for ; Wed, 12 Jan 2022 01:46:09 -0800 (PST) X-Received: by 2002:ac2:44d1:: with SMTP id d17mr6531482lfm.259.1641980768849; Wed, 12 Jan 2022 01:46:08 -0800 (PST) In-Reply-To: X-Original-Sender: melroch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org X-Original-Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of melroch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org designates 209.85.167.41 as permitted sender) smtp.mailfrom=melroch-Re5JQEeQqe8AvxtiuMwx3w@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:29951 Archived-At: --0000000000007494a205d55f7024 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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=C3=A1=C5=A1 Kruli=C5=A1 skrev: > 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 n= ot > 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` > 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=C4=9Bl=C3=AD 10. ledna 2022 v 19:46:37 UTC+1 u=C5=BEivatel BPJ n= apsal: > >> 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 =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 >>> with Pandoc `block` elements. Currently, I am trying to replace `
>> class=3D'replace-me'>` tag with `` tag in similar (highly simplif= ied) >>> HTML file: >>> >>> ```.{html} >>> >>> >>>

First line.

>>>
This should carry on to conver= ted >>> document.
>>>

End.

>>> >>> >>> ``` >>> >>> I have tried to detect the `
` tag, use `walk_block` to get the >>> `
` content and put it in `` tag, I also found a code using >>> `:walk` method. Lastly, I tried to convert `
` content to simple st= ring >>> 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 >>> general with `pandoc_walk` or `:walk` (are they the same?) and how to d= eal >>> 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-a97= 8-a46ad5cff5a8n%40googlegroups.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/aea85ef4-af50-46e5-8e21-= d677801cb971n%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 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/CADAJKhD6WVs3UxD0Dt0bYLA8mE4-A4n%3DQyyKrwkT%3DMYO%2BQNaVg%40= mail.gmail.com. --0000000000007494a205d55f7024 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
The div content is an enhanced `pandoc.List` table with s= ome useful methods:


The filter documentation notes where you will find Lis= t objects in the type descriptions.

BTW you probably don't want to use the return value of pand= oc.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 w= ill be preserved.

Den tis 11 jan. 2022 15:08Tom=C3=A1=C5=A1 Kruli=C5= =A1 <tomas.krulis@integro= mat.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 `R= awInline`, but `RawBlock`, like so:

=C2=A0```.{lua}
= =C2=A0 if elem.t =3D=3D 'Div' and elem.classes[1] =3D=3D "repl= ace-me" then
=C2=A0 =C2=A0 content =3D pandoc.utils.stringify(elem.= content)
=C2=A0 =C2=A0 return pandoc.RawBlock('html', '<n= ote>' .. content.. '</note>')
=C2=A0 else
=C2=A0= =C2=A0 return elem
=C2=A0 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 mu= ch clearer to me. I think I now understand that they are used to iterate/fi= lter over the contents of the block that is being walked over. But if the b= lock element contains only String elements (paragraph with simple text), th= en also Str-filter functions are applied to it?
Maybe my confusio= n can be voiced like this: When should I use `pandoc.walk_block` function t= o get what I want and cannot use something else? Explained not only with co= de, 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 neithe= r 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 t= hese only once, for speed and resources saving!
loca= l pre =3D pandoc.RawBlock('html', '<note>')
local post =3D pandoc.RawBlock('html', '</note= >')

function Div = (div)
=C2=A0 -- The order of the classes shouldn'= ;t matter!
=C2=A0 if div.classes:includes('repla= ce-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 en= d
=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 wo= uld like to ask how you should, in general, detect and manipulate with Pand= oc `block` elements. Currently, I am trying to replace `<div class=3D= 9;replace-me'>` tag with `<note>` tag in similar (highly simpl= ified) HTML file:

```.{html}
<html>= ;
<body>
<p> First line. </p>
<div class=3D"replace-me another-class"> This should car= ry 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}
=C2=A0 if elem.t =3D=3D 'Div'= and elem.classes[1] =3D=3D "replace-me" then
=C2=A0 =C2=A0 co= ntent =3D pandoc.utils.stringify(elem.content)
=C2=A0 =C2=A0 return pand= oc.RawInline('html', '<note>' .. content.. '</= note>')
=C2=A0 else
=C2=A0 =C2=A0 return elem
=C2=A0 end
```

But none of that is working. I wou= ld like to ask you, how to work in general with `pandoc_walk` or `:walk` (a= re they the same?) and how to deal with my specific situation?
Th= ank you very much for any help, I ope that afterwards I will be able to hel= p 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-discuss+unsubscribe-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.org= m.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/aea85ef4-a= f50-46e5-8e21-d677801cb971n%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 pand= oc-discuss+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To view this discussion on the web visit https://g= roups.google.com/d/msgid/pandoc-discuss/CADAJKhD6WVs3UxD0Dt0bYLA8mE4-A4n%3D= QyyKrwkT%3DMYO%2BQNaVg%40mail.gmail.com.
--0000000000007494a205d55f7024--