From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/27480 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Peter Newsgroups: gmane.text.pandoc Subject: Re: Lua filter to convert Markdown comment Date: Fri, 15 Jan 2021 11:14:53 -0800 (PST) Message-ID: References: Reply-To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_1096_187581799.1610738093956" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="32115"; mail-complaints-to="usenet@ciao.gmane.io" To: pandoc-discuss Original-X-From: pandoc-discuss+bncBCUL3CX4YUARBL6TQ6AAMGQEVKOJJVQ-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Fri Jan 15 20:14:58 2021 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane-mx.org Original-Received: from mail-oo1-f55.google.com ([209.85.161.55]) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1l0UYj-0008D0-Kg for gtp-pandoc-discuss@m.gmane-mx.org; Fri, 15 Jan 2021 20:14:57 +0100 Original-Received: by mail-oo1-f55.google.com with SMTP id r10sf5531418oom.20 for ; Fri, 15 Jan 2021 11:14:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; 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=Zh5r1E8Nd1NtnVXsBA7cQBWTilV/g9BevlG4axaAWp4=; b=QKBGnJvWDpmrOPEvTx7pmsRm5YbOmwy8MK+sgQgJawkXHqna7EA8pJYNu7IKJthuK9 q/SZjfl+Wjy8U21GXlMs/hjBfvuZ5Wss2w7wNHiRVJS0NaVAdj2nxYuM49c0Uao+IOSk +OQ6akFIFnIVOARTEqBHp8tvY920ryy4FfeWqHW3z+M8iWC9EXMpdwMxCajvjuhJSphb eVlDz75sftnDhLLhpBe+3dKEdsVwh16z0bruVJGW1ZtFPQHgoiWgcmseLAAhuwGtDuFV f3afqLRMm3ckirV552FpUILaIVsVz4x9e7upbkRGB5Ypj+wxewb7KboW3UkL4K3Dpctm pLTQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; 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=Zh5r1E8Nd1NtnVXsBA7cQBWTilV/g9BevlG4axaAWp4=; b=gx8UQacZOjEJVXaZJWCvZfQiTJ7PoBGy9YWoMWbn70wlwKqIOjGkaTN4QXDpyNwOqW GDmS7g6rWLu7rcASR+FdaTl5w0B+xB9e3pRAD4SjKCKz5+AoKgNMJZCt9nBFIT5uWK8n FXzkIsA6Z1yfE0IfbiLTl06+tTAn/jc2FliZfq+zcaFj0wxle7KOe3nnnMT7SGfE2emE UO5aZjMFeZf4refbKWsmmFglaFB2Tn2+IuB61dsDPMOkZd0HEiUQxFdZa7LzaC6c061+ Tjde+G5oRmr6a6rV3mgPKwaBlXM47ZpySkFyZ1RuZWu29jGRK6+VQB5Q4lB3EinxeuA0 IVbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; 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=Zh5r1E8Nd1NtnVXsBA7cQBWTilV/g9BevlG4axaAWp4=; b=GQW7hwiY5sI5mmTBhcGNJlttQWqqeb62hLQBaB76StuGgpR2PsYz0sdIAp3fZ2Jnu1 rJjke13g3saOmW/eHJqhaJjKLf5k7AH7sJiEtrPmpITglEhIYtVKc6sBst3dc7a6dTwM 5ti1b4SJ8SiqLzXqXgQ8+2EvL4kvTDa77BqAEUi4CvL5bNMSr3gO5aVyI4z+yAe9WLuN aRsCuA8PIYEoZSDFwJ1jvLLmVYIQRrKuLgAlj0U1yfvFeYf/bg44ceewQoIMb7i0TSXI b5O5yRFLrzft+Se5ethabWXNlrdE0wy5rX0gFoXT7Nny4QiFsD+G+KyXHdTAiwokp+EE VgBw== Original-Sender: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org X-Gm-Message-State: AOAM531f7WZRCvtTQ1bkOcOZNLIOBdNf57lwkhmUyygiffaJLrT36/0a wITZbbIppOV56fz1iN1hcR0= X-Google-Smtp-Source: ABdhPJzs8duZzntbNDUszwdbOE0E/Xp9KOmFtBPjQckdNxn2wPdnNfFvEcE/2Vb8j0VIik5yhZSZhA== X-Received: by 2002:aca:3c8a:: with SMTP id j132mr6566634oia.73.1610738096597; Fri, 15 Jan 2021 11:14:56 -0800 (PST) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:a54:4694:: with SMTP id k20ls2417951oic.0.gmail; Fri, 15 Jan 2021 11:14:54 -0800 (PST) X-Received: by 2002:a05:6808:49:: with SMTP id v9mr6777390oic.138.1610738094788; Fri, 15 Jan 2021 11:14:54 -0800 (PST) In-Reply-To: X-Original-Sender: pmatulis-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:27480 Archived-At: ------=_Part_1096_187581799.1610738093956 Content-Type: multipart/alternative; boundary="----=_Part_1097_1875388003.1610738093956" ------=_Part_1097_1875388003.1610738093956 Content-Type: text/plain; charset="UTF-8" This is really nice. Gratitude. On Friday, 15 January 2021 at 04:19:51 UTC-5 BP wrote: > > Almost there. You need > > > `content = {pandoc.Str"Foo"}` > > > since the content of a Header is a list of inline elements. > > As for multiple possible values Lua patterns are, as you seem to have > discovered, a bit challenged in that they don't support alternations. The > best solution is to use a table: > > ``````lua > -- Localize `pandoc` to a shorter name to save space & type less! > local p = pandoc > > -- This is your "substitution" table. > local text4match = { > -- If you need styling do this: > foo = { p.Str"Foo", p.Space(), p.Str"and", p.Space(), > p.Emph(p.Str"more.") }, > -- If you do not need styling you can do this: > bar = { p.Str"Bar and less." }, > } > > function RawBlock (elem) > local key = elem.text:match"%<%!%-%-%s*(%a+)%s*%-%-%>" > if key then > local text = text4match[key] > if text then > return p.Header(2, text) > end > end > -- No match, no text, ignore this elem > return nil > end > `````` > > HTH, > > /bpj > > -- > Better --help|less than helpless > > Den fre 15 jan. 2021 04:39Peter Matulis skrev: > >> So I now have this: >> >> function RawBlock(el) >> if el.text:match("%<%!%-%- foo %-%-%>") then >> content = "foo" >> return pandoc.Header(2, content) >> end >> end >> >> There are actually about a dozen such known Markdown comments (e.g. >> 'foo', 'bar'). >> Instead of creating multiple files I'd like to have just one that uses a >> regex and capture group: >> >> if el.text:match("%<%!%-%- (%a+) %-%-%>") then >> >> I'm just unsure how to refer to the capture in a conditional statement >> (i.e. "if 'foo' is captured then 'content' = "foo", elsif 'bar' is >> captured then 'content' = "bar"). >> >> >> On Thu, 14 Jan 2021 at 15:57, Peter Matulis wrote: >> >>> Gratitude. This got me going. >>> >>> On Thu, 14 Jan 2021 at 13:52, John MacFarlane wrote: >>> >>>> >>>> What you want to do is match on a RawBlock element. >>>> Something like >>>> >>>> function RawBlock(el) >>>> if el.text:match(" >>>> > >>>> > I simply want to convert this into an actual (second-level) MD header: >>>> > >>>> > ## blah >>>> > >>>> > From what I've read, I would ideally specify an "element" that will be >>>> > acted upon, but what is a comment considered? Anyway, I think I can >>>> specify >>>> > `Pandoc` to mean "check the entire document", and that's what I've >>>> done >>>> > here (I will eventually be iterating through multiple MD files so I >>>> should >>>> > really not be doing it like this; please advise!): >>>> > >>>> > function Pandoc (value) >>>> > comment = "" >>>> > comment = comment:gsub("%<%!%-%- blah %-%-%>", "## blah") >>>> > return value >>>> > end >>>> > >>>> > I've run the main logic through Lua on the terminal and it works: >>>> > >>>> > $ lua >>>> > Lua 5.3.3 Copyright (C) 1994-2016 Lua.org, PUC-Rio >>>> >> comment = "" >>>> >> comment = comment:gsub("%<%!%-%- blah %-%-%>", "## blah") >>>> >> print (comment) >>>> > ## blah >>>> > >>>> > But I'm failing to integrate it with Pandoc (no change is registered >>>> in >>>> > file.html). It's probably due to my misunderstanding of functions >>>> (not a >>>> > programmer!). >>>> > >>>> > TIA. >>>> > >>>> > /pm >>>> > >>>> > -- >>>> > 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/CAMxYqzEvRHcFQ1BYsFODC-Bd36%3DLfO-UKcVYPNzR8i8rDZorXw%40mail.gmail.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-discus...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> > To view this discussion on the web visit >> https://groups.google.com/d/msgid/pandoc-discuss/CAMxYqzHK6BvhbzE__4__Po%2Bwizq_4b3CZT9q%3D9ZQ1iF9JR-VSg%40mail.gmail.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/e17e3930-8788-437b-b874-9b9bd9b6d14bn%40googlegroups.com. ------=_Part_1097_1875388003.1610738093956 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable This is really nice. Gratitude.

On Friday, 15 January 2021 at 04:19:51 UTC-5= BP wrote:

Almost there.= You need=C2=A0


`content =3D {pandoc.Str"Foo"}`


since the= content of a Header is a list of inline elements.
<= br>
As for multiple possible values Lua patterns are= , as you seem to have discovered, a bit challenged in that they don't s= upport alternations. The best solution is to use a table:

``````lua
-- Local= ize `pandoc` to a shorter name to save space & type less!
local p =3D pandoc

-- This is your "substitution" table.
local text4match =3D {
=C2=A0 -- If you need stylin= g do this:
=C2=A0 foo =3D { p.Str"Foo", p.= Space(), p.Str"and", p.Space(), p.Emph(p.Str"more.") },=
=C2=A0 -- If you do not need styling you can do thi= s:
=C2=A0 bar =3D { p.Str"Bar and less." }= ,
}

function RawBlock (elem)
=C2=A0 local key =3D ele= m.text:match"%<%!%-%-%s*(%a+)%s*%-%-%>"
=C2=A0 if key then
=C2=A0 =C2=A0 local text =3D = text4match[key]
=C2=A0 =C2=A0 if text then
=C2=A0 =C2=A0 =C2=A0 return p.Header(2, text)
=C2=A0 =C2=A0 end
=C2=A0 end
=C2=A0 -- No match, no text, ignore this elem
=C2=A0 return nil
end
```= ```

HTH,

/bpj

--
Better --help|less than helpless

Den fre 15 jan. 2021 04:39Peter Matulis <pmat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> skrev:
<= /div>
= So I now have this:

function RawBlock(el)
=C2=A0 if el.text:match("%<%!%-%-= foo %-%-%>") then
=C2=A0 =C2=A0 content =3D "foo"
= =C2=A0 =C2=A0 return pandoc.Header(2, content)
=C2=A0 end
end
<= br>

<= /div>
There = are actually about a dozen such known Markdown comments (e.g. 'foo'= , 'bar').
Instead of creating multiple files=C2=A0I'd like to have just one that us= es a regex and capture group:

=
=C2=A0 if el.text:match("%<%!%-%- (%a+) %-%-%>&qu= ot;) then

= I'm just unsure how to r= efer to the=C2=A0capt= ure in a conditional statement
(i.e. "if 'foo' is= captured then 'content' =3D "foo", elsif 'bar' i= s captured then 'content' =3D "bar").


<= div class=3D"gmail_quote">
On Thu, 14 = Jan 2021 at 15:57, Peter Matulis <pmat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
Gratitude. This got me going.<= /div>

On Thu, 14 Jan 2021 at 13:52, John MacFarlane <j...-TVLZxgkOlNX2fBVCVOL8/A@public.gmane.org> wrote:

What you want to do is match on a RawBlock element.
Something like

function RawBlock(el)
=C2=A0 if el.text:match("<!--") then
=C2=A0 =C2=A0 content =3D -- fill this in based on el.text
=C2=A0 =C2=A0 return pandoc.Header(2, content)
=C2=A0 end
end


Peter Matulis <pma= t...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:

> Hi. I'm new to Pandoc & Lua but my present issue is straightfo= rward. I am
> converting a Markdown file to HTML with pandoc like this:
>
> pandoc -L filter.lua -f gfm -t html -o file.html file.md
>
> Where file.md contains a line with this MD comment (the MD file was > obtained by converting from RST where I had to seed that file with an = RST
> comment to use as a placeholder for what I'm doing here):
>
> <!-- blah -->
>
> I simply want to convert this into an actual (second-level) MD header:=
>
> ## blah
>
> From what I've read, I would ideally specify an "element"= ; that will be
> acted upon, but what is a comment considered? Anyway, I think I can sp= ecify
> `Pandoc` to mean "check the entire document", and that's= what I've done
> here (I will eventually be iterating through multiple MD files so I sh= ould
> really not be doing it like this; please advise!):
>
> function Pandoc (value)
>=C2=A0 =C2=A0comment =3D "<!-- blah -->"
>=C2=A0 =C2=A0comment =3D comment:gsub("%<%!%-%- blah %-%-%>&= quot;, "## blah")
>=C2=A0 =C2=A0return value
> end
>
> I've run the main logic through Lua on the terminal and it works:<= br> >
> $ lua
> Lua 5.3.3=C2=A0 Copyright (C) 1994-2016 Lua.org, PUC-Rio
>> comment =3D "<!-- blah -->"
>> comment =3D comment:gsub("%<%!%-%- blah %-%-%>", &= quot;## blah")
>> print (comment)
> ## blah
>
> But I'm failing to integrate it with Pandoc (no change is register= ed in
> file.html). It's probably due to my misunderstanding of functions = (not a
> programmer!).
>
> TIA.
>
> /pm
>
> --
> You received this message because you are subscribed to the Google Gro= ups "pandoc-discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send= an email to pandoc-d= iscus...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
> To view this discussion on the web visit ht= tps://groups.google.com/d/msgid/pandoc-discuss/CAMxYqzEvRHcFQ1BYsFODC-Bd36%= 3DLfO-UKcVYPNzR8i8rDZorXw%40mail.gmail.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/e17e3930-8788-437b-b874-9b9bd9b6d14bn%40googlegroups.= com.
------=_Part_1097_1875388003.1610738093956-- ------=_Part_1096_187581799.1610738093956--