From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.text.pandoc/20806 Path: news.gmane.org!.POSTED!not-for-mail From: Samuele Pilleri Newsgroups: gmane.text.pandoc Subject: Thoughts trying to write a Lua filter Date: Fri, 17 Aug 2018 10:42:24 -0700 (PDT) Message-ID: <6e09bd71-ee77-4de7-a002-953a62325234@googlegroups.com> Reply-To: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_791_2143612034.1534527744949" X-Trace: blaine.gmane.org 1534527621 5689 195.159.176.226 (17 Aug 2018 17:40:21 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Fri, 17 Aug 2018 17:40:21 +0000 (UTC) To: pandoc-discuss Original-X-From: pandoc-discuss+bncBDNPZQMP5ICRBAUS3TNQKGQEX2HXUMY-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Fri Aug 17 19:40:17 2018 Return-path: Envelope-to: gtp-pandoc-discuss@m.gmane.org Original-Received: from mail-oi0-f56.google.com ([209.85.218.56]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fqijQ-0001Mu-93 for gtp-pandoc-discuss@m.gmane.org; Fri, 17 Aug 2018 19:40:16 +0200 Original-Received: by mail-oi0-f56.google.com with SMTP id j17-v6sf7746642oii.8 for ; Fri, 17 Aug 2018 10:42:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlegroups.com; s=20161025; h=sender:date:from:to:message-id:subject:mime-version :x-original-sender:reply-to:precedence:mailing-list:list-id :list-post:list-help:list-archive:list-subscribe:list-unsubscribe; bh=IAwayO3xw1w1MP/6zfp9VhRAyx3ydOUW80D884Gp2WM=; b=kT9rWiwzXBiQLf7Lg1fKc7Jvh3TeoTGg8NoUfRQZaucNhfiKOPITiYlFftqO44td3/ bHs3BGeyKghr3neBn+ANYwG0FIgxuMGtXCjQLOeQ+7Xd2nkEdezcHMH1oY/6wuTnmKSk 4xiEOrS50X3JAGl7BYNgN/fw2P2HMyAvjVMlSzVXDzmwz0eeanBAcR3eNZJJpZIGb8L4 vmP6iu5FzgIUX2GKmjf7laM/lXmsEoscWqVnQzC1qvcJetRERRW0oZhMraJlYTkDtTJm lQOkIh/zArYwz1XrYgz2J/a7CRNA9od+mMsT2MmB5sqiYZgI07bcMYDueeVGyAZwe8ss V2NA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:message-id:subject:mime-version:x-original-sender :reply-to:precedence:mailing-list:list-id:list-post:list-help :list-archive:list-subscribe:list-unsubscribe; bh=IAwayO3xw1w1MP/6zfp9VhRAyx3ydOUW80D884Gp2WM=; b=PJEmlRWrfoZ4AJMy3sLStKuMUpUvKeFxsqWiP5bPBudFI1SbvnHlkoFlM3RWbvw5Ru XrNfJJ0na5bGMJq3mNuFCWtRRpM+uikAu34+c0BOJuUbCX8ypwCG1S1wPlPQg1FVnG8d Qbi/lcEdrhE87/0jfbwjfRpc/bu3QquuKWD3h/YEY4/tYWbnGPCW2nNc3HHXM/+BRrKG SH94FIkPA4MdYjewsD3XjQACnMPVYOmzI/6NRKCaBXoNXwbpHAvvnLxk0KmAhBELekZg uFUe8luB+mODl4ge66UKYjStnj3oA8mkPS7zAJGcjHBhuDlFypcUhgIy/ZNvhA3+1Xo9 RT/Q== 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: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=IAwayO3xw1w1MP/6zfp9VhRAyx3ydOUW80D884Gp2WM=; b=fWyq7aXEaXTfIb6ZEYpCpvTXxeqaGetKCGH0oVAxfI7QDbnjKpAfakuVCcvSLfiGup Lh2KWWV807nYLnZ7Pgv6wnHkbgxtoK/oaasrJChpAsC6ZwTElaiIFDTBvhyN6M6JyQlQ jaIQa6HThhTQ67P3GqnCbvuJ2XPho2TdPW67ZYJXY74JjMV8W3YmPDWbhg+mtJnTqGZB +hszzI+8PcmImwPygalyUgGbup1EEyov3I0U3JU22TqX8swbsYfb2v26mN4O3c+kdcG/ VMeDcTe7DzZkl5v1Zp982Mg3Hq9Din+OLlU7vqDRif4SKwV2oAKUQ9G5+kudTz2WA+cY Dhaw== Original-Sender: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org X-Gm-Message-State: AOUpUlEWW7NC+izABvVldJ9HF7AA7pvadt1SgaPdWqgEoV+shhqBRfB6 rFFmMU+jrDSZiOfJrP1VtvY= X-Google-Smtp-Source: AA+uWPygMLvClR154Vmdw7tDv5Oxom1oyBH8M3F4a2MhvRY7wjKvRdX+LPjmnPvrRtS9HGLg8SeeYA== X-Received: by 2002:aca:f495:: with SMTP id s143-v6mr907370oih.7.1534527746741; Fri, 17 Aug 2018 10:42:26 -0700 (PDT) X-BeenThere: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org Original-Received: by 2002:aca:1b1a:: with SMTP id b26-v6ls4716285oib.20.gmail; Fri, 17 Aug 2018 10:42:25 -0700 (PDT) X-Received: by 2002:aca:eb15:: with SMTP id j21-v6mr927509oih.6.1534527745623; Fri, 17 Aug 2018 10:42:25 -0700 (PDT) X-Original-Sender: pillerisamuele-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-Spam-Checked-In-Group: pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org X-Google-Group-Id: 1007024079513 List-Post: , List-Help: , List-Archive: , List-Unsubscribe: , Xref: news.gmane.org gmane.text.pandoc:20806 Archived-At: ------=_Part_791_2143612034.1534527744949 Content-Type: multipart/alternative; boundary="----=_Part_792_2147062929.1534527744950" ------=_Part_792_2147062929.1534527744950 Content-Type: text/plain; charset="UTF-8" Hello everyone. I've decided to write a Lua filter for Pandoc on my own, something similar to scotthartley/pandoc-wrapfig with a slightly different syntax: it uses classes and attributes instead of modifying caption format. ![caption with **bold**](image.png "Image title"){#img .wrap width=3} So far so good, but diving deeper in the details I've had some problems. *Width attribute* Quoting the manual (extension: link attributes ): The width and height attributes on images are treated specially. When used > without a unit, the unit is assumed to be pixels. > and Dimensions are converted to inches for output in page-based formats like > LaTeX. [...] Use the --dpi option to specify the number of pixels per > inch. The default is 96dpi. > However, PANDOC_READER_OPTIONS doesn't provide --dpi such option. I think it would be cool if Pandoc could handle it itself *before* calling the writers, or at least provide a conversion function within the API. *Image with attributes and formatted caption* Such syntax ![[caption]{.underline}](image.png) produces the following output: $ pandoc demo.md -t native [Para [Image ("",[],[]) [Span ("",["underline"],[]) [Str "caption"]] ("image.png","fig:")]] $ pandoc demo.md -t latex \begin{figure} \centering \includegraphics{image.png} \caption{{caption}} \end{figure} $ pandoc demo.md -t html5
caption
caption
I suppose it could be a bug, or maybe LaTeX doesn't support underline within the context of a caption. However I think some clarification is needed and the AST passed to Lua should be revised as well since it's pretty a mess: ![[caption]{.underline}](image.png) 1: c: 1: 1: 2: 1: underline 3: 2: 1: c: caption ![caption](image.png) 1: c: caption ![*caption*](image.png) 1: c: 1: c: caption (*): these tables refer to el.c[2] only Also, I couldn't find any good documentation on parameter's fields (at least for images) and it took me a couple of hours to understand it. I would add something like this to the docs: -- Creates an image identical to the one given function Image (el) local id, classes, attrs = unpack(el.c[1]) local caption = el.c[2] local src, title = unpack(el.c[3]) return pandoc.Image(caption, src, title, {id, classes, attrs}) end Finally, this it the first filter I write (and my first experience with Lua) so I would really appreciate if the community could give me a feedback to check if I got everything right: current version is attached, thanks in advance! I would really like to take this opportunity to thank John and all the devs for creating Pandoc: it's a unique piece of software, couldn't wish any better! -- 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 post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/6e09bd71-ee77-4de7-a002-953a62325234%40googlegroups.com. For more options, visit https://groups.google.com/d/optout. ------=_Part_792_2147062929.1534527744950 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello everyone.

I've dec= ided to write a Lua filter for Pandoc on my own, something similar to scotthartley/pandoc-w= rapfig with a slightly different syntax: it uses classes and attributes= instead of modifying caption format.

= ![caption with **bold**](image.p= ng "I= mage title"){#i= mg .wrap width=3D3}

So far so good,= but diving deeper in the details I've had some problems.
Width attribute

Quoting the manual (extension: link attributes):

<= /div>
The wid= th and height attributes on images are treated specially. When used without= a unit, the unit is assumed to be pixels.

and

<= blockquote class=3D"gmail_quote" style=3D"margin: 0px 0px 0px 0.8ex; border= -left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">Dimensions are con= verted to inches for output in page-based formats like LaTeX. [...] Use the= --dpi option t= o specify the number of pixels per inch. The default is 96dpi.

However, PANDO= C_READER_OPTIONS doesn't provide --dpi such option. I think it would be cool if= Pandoc could handle it itself before calling the writers, or at lea= st provide a conversion function within the API.

<= u>Image with attributes and formatted caption

Such syntax

![[caption]{.underline}](image.png)

produces the following outpu= t:

$ pandoc demo.md -t native
[Pa= ra [Image ("",[],[]) [Span ("",["underline"],= []) [Str "caption"]] ("image.png","fig:")]]
$ pandoc demo.md -t latex
\begin{figure= }
\centering
\
includegraphics{image.png}
\= caption{{<= /span>caption}}
\end{figure}

$ pandoc demo.md -t html5
<= div style=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 1= 87, 187); border-style: solid; border-width: 1px; overflow-wrap: break-word= ;" class=3D"prettyprint">
<figur= e>
<= /span><img src=3D"image.png" alt=3D"caption" /><figcaption><span class=3D"underline">caption</span></figcaption>
</figure>
=

I suppose it could be a bug, or maybe LaTeX d= oesn't support underline within the context of a caption. However I thi= nk some clarification is needed and the AST passed to Lua should be revised= as well since it's pretty a mess:

![[caption]{.underline}](image.png)
1:
=C2=A0 c:
=C2=A0=C2=A0=C2= =A0 1:
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 1:
=C2=A0=C2=A0=C2=A0=C2=A0=C2= =A0 2:
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 1: underline
=C2=A0= =C2=A0=C2=A0=C2=A0=C2=A0 3:
=C2=A0=C2=A0=C2=A0 2:
=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 1:
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 c: caption
![<= /span>caption](image.png)
1:
=C2=A0 c: capti= on

![*capt= ion*](image.png)
1:
=C2=A0 c= :
=C2=A0=C2=A0=C2=A0 1:
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 c: caption
=
(*): these tables refer= to el.c[2] onl= y
<= span style=3D"font-family: arial, sans-serif;">

Also, I couldn't find any good documentation on parameter's f= ields (at least for images) and it took me a couple of hours to understand = it. I would add something like this to the docs:

<= div style=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 1= 87, 187); border-style: solid; border-width: 1px; overflow-wrap: break-word= ;" class=3D"prettyprint">
--= Creates an image identical to the o= ne given
function= Image (el)
=C2=A0 =C2=A0
local id, classes, attrs =3D unpac= k(<= span style=3D"color: #000;" class=3D"styled-by-prettify">el
.c[1])
=C2=A0 =C2=A0
local caption =3D el.c[2]
=C2=A0 =C2=A0
local src, title =3D unpack(el.c[3])

=C2=A0 =C2=A0
return pandoc.Image(ca= ption, src, title, {id, = classes, attrs})
end

Finally, this it the first filter I write (and my first exp= erience with Lua) so I would really appreciate if the community could give = me a feedback to check if I got everything right: current version is attach= ed, thanks in advance!

I would really like to take= this opportunity to thank John and all the devs for creating Pandoc: it= 9;s a unique piece of software, couldn't wish any better!

--
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 post to this group, send email to pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org.
To view this discussion on the web visit https://groups.google.com/d/= msgid/pandoc-discuss/6e09bd71-ee77-4de7-a002-953a62325234%40googlegroups.co= m.
For more options, visit http= s://groups.google.com/d/optout.
------=_Part_792_2147062929.1534527744950-- ------=_Part_791_2143612034.1534527744949 Content-Type: application/octet-stream; name=pandoc-wrapfig.lua Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=pandoc-wrapfig.lua X-Attachment-Id: 591f38f3-7cb2-4489-b932-bffd10017a62 Content-ID: <591f38f3-7cb2-4489-b932-bffd10017a62> -- See https://tex.stackexchange.com/q/56176 -- Checks if a table contains a key local function contains (table, val) for i=1,#table do if table[i] == val then return true end end return false end -- Searches an array of tuples for a specific key -- Returns the value if found, nil otherwise local function search_tuple_value (table, key) for i=1,#table do if table[i][1] == key then return table[i][2] end end end function Image (el) local id, classes, attrs = unpack(el.c[1]) local caption = el.c[2] local src, title = unpack(el.c[3]) tprint(caption) if FORMAT == "latex" and contains(classes, "wrap") then local side if contains(classes, "wrap-float") then if contains(classes, "wrap-left") then side = "L" else side = "R" end else if contains(classes, "wrap-left") then side = "l" else side = "r" end end local size = search_tuple_value(attrs, "width") or 0 local latex_head = [[\begin{wrapfigure}{]] .. side .. '}{' .. size .. 'in}' if #caption > 0 then local latex_body = [[\centering\includegraphics{]] .. src .. [[}\caption]] local latex_tail = [[\end{wrapfigure}]] return { pandoc.RawInline(FORMAT, latex_head .. latex_body), pandoc.Span(caption), pandoc.RawInline(FORMAT, latex_tail) } -- Should this ^ really be a Span? else local latex_body = [[\centering\includegraphics{]] .. src .. '}' local latex_tail = [[\end{wrapfigure}]] return pandoc.RawInline(FORMAT, latex_head .. latex_body .. latex_tail) end end end ------=_Part_791_2143612034.1534527744949--