From: Samuele Pilleri <pillerisamuele-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: pandoc-discuss <pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
Subject: Thoughts trying to write a Lua filter
Date: Fri, 17 Aug 2018 10:42:24 -0700 (PDT) [thread overview]
Message-ID: <6e09bd71-ee77-4de7-a002-953a62325234@googlegroups.com> (raw)
[-- Attachment #1.1: Type: text/plain, Size: 3505 bytes --]
Hello everyone.
I've decided to write a Lua filter for Pandoc on my own, something similar
to scotthartley/pandoc-wrapfig
<https://github.com/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
<https://pandoc.org/MANUAL.html#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
<figure>
<img src="image.png" alt="caption" /><figcaption><span class="underline">
caption</span></figcaption>
</figure>
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.
[-- Attachment #1.2: Type: text/html, Size: 18152 bytes --]
[-- Attachment #2: pandoc-wrapfig.lua --]
[-- Type: application/octet-stream, Size: 1538 bytes --]
-- 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
next reply other threads:[~2018-08-17 17:42 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-08-17 17:42 Samuele Pilleri [this message]
[not found] ` <6e09bd71-ee77-4de7-a002-953a62325234-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2018-08-19 18:40 ` John MacFarlane
[not found] ` <m2wosm5heu.fsf-pgq/RBwaQ+zq8tPRBa0AtqxOck334EZe@public.gmane.org>
2018-08-20 19:47 ` Samuele Pilleri
[not found] ` <9da35dc5-3eec-446b-8464-889ec1eb26f4-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2019-09-17 16:08 ` EBkysko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6e09bd71-ee77-4de7-a002-953a62325234@googlegroups.com \
--to=pillerisamuele-re5jqeeqqe8avxtiumwx3w@public.gmane.org \
--cc=pandoc-discuss-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).