public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* Thoughts trying to write a Lua filter
@ 2018-08-17 17:42 Samuele Pilleri
       [not found] ` <6e09bd71-ee77-4de7-a002-953a62325234-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 4+ messages in thread
From: Samuele Pilleri @ 2018-08-17 17:42 UTC (permalink / raw)
  To: pandoc-discuss


[-- 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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2019-09-17 16:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-17 17:42 Thoughts trying to write a Lua filter Samuele Pilleri
     [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

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).