From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/32393 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Pa Sc Newsgroups: gmane.text.pandoc Subject: Help with Lua filter for markdown to LaTeX with Pandoc Date: Sat, 25 Mar 2023 05:42:45 -0700 (PDT) Message-ID: <79ac7814-9558-4d49-a682-cf31f970d27dn@googlegroups.com> Reply-To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_6490_1023278551.1679748165794" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="29196"; mail-complaints-to="usenet@ciao.gmane.io" To: pandoc-discuss Original-X-From: pandoc-discuss+bncBDS5HMVM7MCBBRWY7OQAMGQEPTRWGRQ-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Sat Mar 25 13:42:50 2023 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane-mx.org Original-Received: from mail-oo1-f56.google.com ([209.85.161.56]) by ciao.gmane.io with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pg3EQ-0007Qo-1p for gtp-pandoc-discuss@m.gmane-mx.org; Sat, 25 Mar 2023 13:42:50 +0100 Original-Received: by mail-oo1-f56.google.com with SMTP id w3-20020a4ae083000000b0052645d4a2dfsf1323879oos.18 for ; Sat, 25 Mar 2023 05:42:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20210112; t=1679748169; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :list-id:mailing-list:precedence:reply-to:x-original-sender :mime-version:subject:message-id:to:from:date:sender:from:to:cc :subject:date:message-id:reply-to; bh=cyPJoJt7Dyddx/qf5HjDZ9aHYB/pAKK5mdFQp0YpMM0=; b=BzdVTI1ApAeQKQa4WiJopLN/pwLqlROEcA5CxdhLKfXknnFs6SQjEucP9iKQjeGIzl zLgS69L7f/jGXKHF288kgnnuYorgbNjtFUfGsvSW1Qp7bMjmHzfslrV+TZx4PfiOx53C /YEwk8Rxa/OA2dn/jIA/+PCDdTJ8xzIhEVzZVGpNMHshWRMLequFDNhGXkaGRkCHblrW MpRwTwUTZS3ORhHhLIWwgRGya1j7U3/PF4ysHidOVpRY8pNq+Xdz6h0U6XrCoBIWbJi/ 2GsaW4MnFcJQzM3Nh7St1/1tDOLF6HQPTSFO/cgRGeOGxcFYTyZHEK7U47XRpHipHsMJ gCKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679748169; h=list-unsubscribe:list-subscribe:list-archive:list-help:list-post :x-spam-checked-in-group:list-id:mailing-list:precedence:reply-to :x-original-sender:mime-version:subject:message-id:to:from:date :x-gm-message-state:sender:from:to:cc:subject:date:message-id :reply-to; bh=cyPJoJt7Dyddx/qf5HjDZ9aHYB/pAKK5mdFQp0YpMM0=; b=g9zEBjGp5I/9x6+uCoA+ClT32xYOz6WQ9i8uLO0K5BVNelb7f10HR3injcgQHZOv5Y i/8TisT9PwO3VGvFEGezGMh09d7XGz9ToYPG0GLNKlcXlyI52XBgHmq4R+Zi21Gr62te n5qlvmHmokewIp914MYjNrVUmLlyTWr5uWWv3q4er+jyhqEcTu+xZpfrfCG/r6nwRmV/ 59oIWpBty/LjklVxFBNm5/x2RwFIBkylqqVlAexBV1+nXDX36TnzOJgELgjfeLorj16D k37KrRgvIzC5wuMfm3F+oM4RSa92qgrxRIXPCmpMNb6IbNcVddW7i8hwxtj0UF3uyiDB Pk1g== Original-Sender: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org X-Gm-Message-State: AO0yUKXLmqzAYGoe003/RUHKlJpEkhty0UZ4jkd44aKv0qTwwp0z9VYU OFHSER0guykuy+MIyDOHhuk= X-Google-Smtp-Source: AK7set/EqLSYYxL9o4ZkTRcM4M3R9XTf3btTREVlQz2A7xlwIhUz6JERBQJ3czMOyRb+VSu3eb6KNQ== X-Received: by 2002:aca:3cc6:0:b0:386:e33f:84f2 with SMTP id j189-20020aca3cc6000000b00386e33f84f2mr1281180oia.1.1679748168863; Sat, 25 Mar 2023 05:42:48 -0700 (PDT) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:a05:6808:202a:b0:387:60a7:95ae with SMTP id q42-20020a056808202a00b0038760a795aels960422oiw.10.-pod-prod-gmail; Sat, 25 Mar 2023 05:42:46 -0700 (PDT) X-Received: by 2002:a05:6808:1a01:b0:387:33bc:c3dc with SMTP id bk1-20020a0568081a0100b0038733bcc3dcmr5214324oib.1.1679748166350; Sat, 25 Mar 2023 05:42:46 -0700 (PDT) X-Original-Sender: ideas-and-peanutbutter-zqRNUXuvxA0b1SvskN2V4Q@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:32393 Archived-At: ------=_Part_6490_1023278551.1679748165794 Content-Type: multipart/alternative; boundary="----=_Part_6491_1641663809.1679748165794" ------=_Part_6491_1641663809.1679748165794 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable In markdown (written in Obsidian.md) I use a plugin/theme so that=20 `*=3D=3Dtext=3D=3D*` is rendered as red instead of italic and highlighted. I use this lua-filter to convert regular `=3D=3Dhighlighting=3D=3D` to LaTe= X=20 `\hl{highlighting}` which works perfectly: (I somehow require these two=20 files, I can't remember why)=20 **highlight.lua**: ``` --[[ Add support for a custom inline syntax. This pandoc Lua filter allows to add a custom markup syntax extension. It is designed to be adjustable; it should not be necessary to modify the code below the separator line. The example here allows to add highlighted text by enclosing the text with `=3D=3D` on each side. Pandoc supports this for HTML output out of the box. Other outputs will need additional filters. Copyright: =C2=A9 2022 Albert Krewinkel License: MIT ]] =20 -- Lua pattern matching the opening markup string. local opening =3D [[=3D=3D]] =20 -- Lua pattern matching the closing markup string. local closing =3D [[=3D=3D]] =20 -- Toggle whether the opening markup may be followed by whitespace. local nospace =3D true =20 -- Function converting the enclosed inlines to their internal pandoc -- representation. local function markup_inlines (inlines) return pandoc.Span(inlines, {class=3D"mark"}) end =20 -----------------------------------------------------------------------= - =20 local function is_space (inline) return inline and (inline.t =3D=3D 'Space' or inline.t =3D=3D 'LineBreak' or inline.t =3D=3D 'SoftBreak') end =20 function Inlines (inlines) local result =3D pandoc.Inlines{} local markup =3D nil local start =3D nil for i, inline in ipairs(inlines) do if inline.tag =3D=3D 'Str' then if not markup then local first =3D inline.text:match('^' .. opening .. '(.*)') if first then start =3D inline -- keep element around in case the -- markup is not closed. Check if the -- closing pattern is already in this -- string. local selfclosing =3D first:match('(.*)' .. closing .. '$') if selfclosing then result:insert(markup_inlines{pandoc.Str(selfclosing)}) elseif nospace and first =3D=3D '' and is_space(inlines[i+1])= then -- the opening pattern is followed by a space, but the -- config disallows this. result:insert(inline) else markup =3D pandoc.Inlines{pandoc.Str(first)} end else result:insert(inline) end else local last =3D inline.text:match('(.*)' .. closing .. '$') if last then markup:insert(pandoc.Str(last)) result:insert(markup_inlines(markup)) markup =3D nil else markup:insert(inline) end end else local acc =3D markup or result acc:insert(inline) end end =20 -- keep unterminated markup if markup then markup:remove(1) -- the stripped-down first element result:insert(start) result:extend(markup) end return result end =20 local function markup_inlines (inlines) return=20 {pandoc.RawInline('tex', '\\hl{')} .. inlines .. {pandoc.RawInline('tex',= =20 '}')} end ``` **Span.lua**: ``` function Span (span) if span.classes:includes 'mark' then return {pandoc.RawInline('latex', '\\hl{')} .. span.content .. {pandoc.RawInline('latex', '}')} end end ``` This works great, I got this here:=20 [https://gist.github.com/tarleb/a0646da1834318d4f71a780edaf9f870](https://g= ist.github.com/tarleb/a0646da1834318d4f71a780edaf9f870) However, for my other usecase I'd like it to be able to render `*=3D=3Dthis= =20 kind of highlighting=3D=3D*` as `\colorbox{lightred}{this new kind of=20 highlighting}` I've tried replacing the `\\hl{` with `\colorbox{declared-color}{` but that= =20 didn't work. I was wondering if it had something to do with the class? But renaming that= =20 class from `mark` to `mark-red` didn't work either. Any advice or ideas? Thanks! --=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/79ac7814-9558-4d49-a682-cf31f970d27dn%40googlegroups.com. ------=_Part_6491_1641663809.1679748165794 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable In markdown (written in Obsidian.md) I use a plugin/theme so that `*=3D=3Dt= ext=3D=3D*` is rendered as red instead of italic and highlighted.

I use this lua-filter to convert regular `=3D=3Dhighlighting=3D=3D` = to LaTeX `\hl{highlighting}` which works perfectly: (I somehow require thes= e two files, I can't remember why)

**high= light.lua**:
```
=C2=A0 =C2=A0 --[[
=C2=A0 =C2=A0 Add suppor= t for a custom inline syntax.
=C2=A0 =C2=A0 This pandoc Lua filter all= ows to add a custom markup syntax
=C2=A0 =C2=A0 extension. It is desig= ned to be adjustable; it should not be
=C2=A0 =C2=A0 necessary to modi= fy the code below the separator line.
=C2=A0 =C2=A0 The example here a= llows to add highlighted text by enclosing the
=C2=A0 =C2=A0 text with= `=3D=3D` on each side. Pandoc supports this for HTML output
=C2=A0 = =C2=A0 out of the box. Other outputs will need additional filters.
=C2= =A0 =C2=A0 Copyright: =C2=A9 2022 Albert Krewinkel
=C2=A0 =C2=A0 Licen= se: MIT
=C2=A0 =C2=A0 ]]
=C2=A0 =C2=A0
=C2=A0 =C2=A0 -- Lua= pattern matching the opening markup string.
=C2=A0 =C2=A0 local openi= ng =3D [[=3D=3D]]
=C2=A0 =C2=A0
=C2=A0 =C2=A0 -- Lua pattern mat= ching the closing markup string.
=C2=A0 =C2=A0 local closing =3D [[=3D= =3D]]
=C2=A0 =C2=A0
=C2=A0 =C2=A0 -- Toggle whether the opening = markup may be followed by whitespace.
=C2=A0 =C2=A0 local nospace =3D = true
=C2=A0 =C2=A0
=C2=A0 =C2=A0 -- Function converting the encl= osed inlines to their internal pandoc
=C2=A0 =C2=A0 -- representation.=
=C2=A0 =C2=A0 local function markup_inlines (inlines)
=C2=A0 =C2= =A0 =C2=A0 return pandoc.Span(inlines, {class=3D"mark"})
=C2=A0 =C2=A0= end
=C2=A0 =C2=A0
=C2=A0 =C2=A0 -------------------------------= -----------------------------------------
=C2=A0 =C2=A0
=C2=A0 = =C2=A0 local function is_space (inline)
=C2=A0 =C2=A0 =C2=A0 return in= line and
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (inline.t =3D=3D 'Space' or
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0inline.t =3D=3D 'LineBreak' or
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0inline.t =3D=3D 'SoftBreak')
=C2=A0 =C2= =A0 end
=C2=A0 =C2=A0
=C2=A0 =C2=A0 function Inlines (inlines)=C2=A0 =C2=A0 =C2=A0 local result =3D pandoc.Inlines{}
=C2=A0 =C2= =A0 =C2=A0 local markup =3D nil
=C2=A0 =C2=A0 =C2=A0 local start =3D n= il
=C2=A0 =C2=A0 =C2=A0 for i, inline in ipairs(inlines) do
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 if inline.tag =3D=3D 'Str' then
=C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 if not markup then
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 local first =3D inline.text:match('^' .. opening .. '(.*)')=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if first then
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 start =3D inline -- keep element aro= und in case the
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-- markup is not closed= . Check if the
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-- closing pattern is alre= ady in this
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0-- string.
=C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 local selfclosing =3D first:match('(= .*)' .. closing .. '$')
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 if selfclosing then
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 result:insert(markup_inlines{pandoc.Str(selfclosing)})
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 elseif nospace and first =3D= =3D '' and is_space(inlines[i+1]) then
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 -- the opening pattern is followed by a space, but= the
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 -- config= disallows this.
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 result:insert(inline)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 else
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ma= rkup =3D pandoc.Inlines{pandoc.Str(first)}
=C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 end
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 el= se
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 result:insert(inli= ne)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 end
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 else
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 lo= cal last =3D inline.text:match('(.*)' .. closing .. '$')
=C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 if last then
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 markup:insert(pandoc.Str(last))
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 result:insert(markup_inlines(markup))
= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 markup =3D nil
=C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 else
=C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 markup:insert(inline)
=C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 end
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 end
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 else
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 local acc= =3D markup or result
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 acc:insert(in= line)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 end
=C2=A0 =C2=A0 =C2=A0 end=C2=A0 =C2=A0
=C2=A0 =C2=A0 =C2=A0 -- keep unterminated markup
=C2=A0 =C2=A0 =C2=A0 if markup then
=C2=A0 =C2=A0 =C2=A0 =C2=A0 mark= up:remove(1) -- the stripped-down first element
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 result:insert(start)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 result:extend(= markup)
=C2=A0 =C2=A0 =C2=A0 end
=C2=A0 =C2=A0 =C2=A0 return resu= lt
=C2=A0 =C2=A0 end
=C2=A0 =C2=A0
=C2=A0 =C2=A0 local func= tion markup_inlines (inlines) =C2=A0 return {pandoc.RawInline('tex', '\\hl{= ')} .. inlines .. {pandoc.RawInline('tex', '}')} end
```

**Span.lua**:
```
=C2=A0 =C2=A0 function Span (span= )
=C2=A0 =C2=A0 =C2=A0 if span.classes:includes 'mark' then
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 return {pandoc.RawInline('latex', '\\hl{')} ..
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 span.content ..
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 {pandoc.RawInline('latex', '}')}
=C2=A0 =C2=A0 =C2= =A0 end
=C2=A0 =C2=A0 end
```
This works great, I got this h= ere: [https://gist.github.com/tarleb/a0646da1834318d4f71a780edaf9f870](http= s://gist.github.com/tarleb/a0646da1834318d4f71a780edaf9f870)

How= ever, for my other usecase I'd like it to be able to render `*=3D=3Dthis ki= nd of highlighting=3D=3D*` as `\colorbox{lightred}{this new kind of highlig= hting}`

I've tried replacing the `\\hl{` with `\colorbox{declare= d-color}{` but that didn't work.

I was wondering if it had somet= hing to do with the class? But renaming that class from `mark` to `mark-red= ` didn't work either.

Any advice or ideas? Thanks!

--
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/79ac7814-9558-4d49-a682-cf31f970d27dn%40googlegroups.= com.
------=_Part_6491_1641663809.1679748165794-- ------=_Part_6490_1023278551.1679748165794--