public inbox archive for pandoc-discuss@googlegroups.com
 help / color / mirror / Atom feed
* Lua filter to insert image with caption
@ 2019-01-01 20:16 Thorsten Sommer
       [not found] ` <463cce3d-8746-4ebd-b047-ddfb3ddf2467-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
  0 siblings, 1 reply; 3+ messages in thread
From: Thorsten Sommer @ 2019-01-01 20:16 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 1713 bytes --]

Dear community,

I worked on the PlantUML filter (cf. 
https://github.com/pandoc/lua-filters/blob/master/plantuml/plantuml.lua) to 
give the generated graphics a caption. 

It looked simple at first. All I want is to get a figure with caption, so 
in Markdown:

![This is my caption.](img.jpg)

As long as this stands alone in a paragraph, it gets transformed into a 
figure with caption. Now, I want to do the same from a Lua filter. My 
attempt was this:

local imgObj = pandoc.Image({pandoc.Str("Test caption.")}, fname)
local para = pandoc.Para({imgObj})

Thus, it generates one paragraph which contains only the generated PlantUML 
graphics. Unfortunately, it generates the image only, without the caption. 
What is wrong?

Second question: How can I pass the individual captions from markdown? 
Right now, the PlantUML filter needs a code block of the type 'plantuml', 
e.g.:

```plantuml
@startuml
A - B 
@enduml
```

I would need something like:

```plantuml[My individual caption is this...]
@startuml
A - B 
@enduml
```

Is it possible to read these additional text inside the Lua filter?


Thanks for all answers.


Best
Thorsten

-- 
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/463cce3d-8746-4ebd-b047-ddfb3ddf2467%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

[-- Attachment #1.2: Type: text/html, Size: 5838 bytes --]

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

* Re: Lua filter to insert image with caption
       [not found] ` <463cce3d-8746-4ebd-b047-ddfb3ddf2467-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
@ 2019-01-03 20:43   ` John MacFarlane
       [not found]     ` <yh480kh8ep4gld.fsf-pgq/RBwaQ+zq8tPRBa0AtqxOck334EZe@public.gmane.org>
  0 siblings, 1 reply; 3+ messages in thread
From: John MacFarlane @ 2019-01-03 20:43 UTC (permalink / raw)
  To: Thorsten Sommer, pandoc-discuss


There's a little hackish trick we use internally to distinguish
figures from regular images in paragraphs in the AST.
(This will go away when we get a proper Figure type.)

The hack is to add `fig:` to the front of the image
title.

   local imgObj = pandoc.Image({pandoc.Str("Test caption.")}, fname, 'fig:')

Try that.

Thorsten Sommer <vektoren-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:

> Dear community,
>
> I worked on the PlantUML filter (cf. 
> https://github.com/pandoc/lua-filters/blob/master/plantuml/plantuml.lua) to 
> give the generated graphics a caption. 
>
> It looked simple at first. All I want is to get a figure with caption, so 
> in Markdown:
>
> ![This is my caption.](img.jpg)
>
> As long as this stands alone in a paragraph, it gets transformed into a 
> figure with caption. Now, I want to do the same from a Lua filter. My 
> attempt was this:
>
> local imgObj = pandoc.Image({pandoc.Str("Test caption.")}, fname)
> local para = pandoc.Para({imgObj})
>
> Thus, it generates one paragraph which contains only the generated PlantUML 
> graphics. Unfortunately, it generates the image only, without the caption. 
> What is wrong?
>
> Second question: How can I pass the individual captions from markdown? 
> Right now, the PlantUML filter needs a code block of the type 'plantuml', 
> e.g.:
>
> ```plantuml
> @startuml
> A - B 
> @enduml
> ```
>
> I would need something like:
>
> ```plantuml[My individual caption is this...]
> @startuml
> A - B 
> @enduml
> ```
>
> Is it possible to read these additional text inside the Lua filter?
>
>
> Thanks for all answers.
>
>
> Best
> Thorsten
>
> -- 
> 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/463cce3d-8746-4ebd-b047-ddfb3ddf2467%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.


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

* Re: Lua filter to insert image with caption
       [not found]     ` <yh480kh8ep4gld.fsf-pgq/RBwaQ+zq8tPRBa0AtqxOck334EZe@public.gmane.org>
@ 2019-01-04  9:12       ` Thorsten Sommer
  0 siblings, 0 replies; 3+ messages in thread
From: Thorsten Sommer @ 2019-01-04  9:12 UTC (permalink / raw)
  To: pandoc-discuss


[-- Attachment #1.1: Type: text/plain, Size: 4280 bytes --]

Thanks John. It works just fine. I also figured out, how to solve my second 
issue. Just in case anyone else had the question as well:

Let's say, this is the markdown document:

# Test Section



```{.plantuml caption="This is a test caption."}
@startuml
skinparam dpi 300
A - B
@enduml
```


In this case, `.plantuml` is the class and `caption="..."` is an attribute 
of this code block. Now, we can change the PlantUML lua filter 
(https://github.com/pandoc/lua-filters/blob/master/plantuml/plantuml.lua) 
like this:

-- search for class "plantuml" and replace with image
function CodeBlock(block)
    if block.classes[1] == "plantuml" then
        local img = plantuml(block.text, filetype, plantumlPath)
        local fname = pandoc.sha1(img) .. "." .. filetype
        pandoc.mediabag.insert(fname, mimetype, img)
        return pandoc.Para{ pandoc.Image({pandoc.Str(block.attributes[
"caption"])}, fname, "fig:") }
    end
end

Here, `block.attributes["caption"]` reads the desired attribute's content 
which gets a caption of the image.

At the weekend, I will create a fork + pull request of 
https://github.com/pandoc/lua-filters in order to provide this solution to 
everyone.


Best
Thorsten

Am Donnerstag, 3. Januar 2019 21:43:57 UTC+1 schrieb John MacFarlane:
>
>
> There's a little hackish trick we use internally to distinguish 
> figures from regular images in paragraphs in the AST. 
> (This will go away when we get a proper Figure type.) 
>
> The hack is to add `fig:` to the front of the image 
> title. 
>
>    local imgObj = pandoc.Image({pandoc.Str("Test caption.")}, fname, 
> 'fig:') 
>
> Try that. 
>
> Thorsten Sommer <vekt...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <javascript:>> writes: 
>
> > Dear community, 
> > 
> > I worked on the PlantUML filter (cf. 
> > https://github.com/pandoc/lua-filters/blob/master/plantuml/plantuml.lua) 
> to 
> > give the generated graphics a caption. 
> > 
> > It looked simple at first. All I want is to get a figure with caption, 
> so 
> > in Markdown: 
> > 
> > ![This is my caption.](img.jpg) 
> > 
> > As long as this stands alone in a paragraph, it gets transformed into a 
> > figure with caption. Now, I want to do the same from a Lua filter. My 
> > attempt was this: 
> > 
> > local imgObj = pandoc.Image({pandoc.Str("Test caption.")}, fname) 
> > local para = pandoc.Para({imgObj}) 
> > 
> > Thus, it generates one paragraph which contains only the generated 
> PlantUML 
> > graphics. Unfortunately, it generates the image only, without the 
> caption. 
> > What is wrong? 
> > 
> > Second question: How can I pass the individual captions from markdown? 
> > Right now, the PlantUML filter needs a code block of the type 
> 'plantuml', 
> > e.g.: 
> > 
> > ```plantuml 
> > @startuml 
> > A - B 
> > @enduml 
> > ``` 
> > 
> > I would need something like: 
> > 
> > ```plantuml[My individual caption is this...] 
> > @startuml 
> > A - B 
> > @enduml 
> > ``` 
> > 
> > Is it possible to read these additional text inside the Lua filter? 
> > 
> > 
> > Thanks for all answers. 
> > 
> > 
> > Best 
> > Thorsten 
> > 
> > -- 
> > 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...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <javascript:>. 
> > To post to this group, send email to pandoc-...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org 
> <javascript:>. 
> > To view this discussion on the web visit 
> https://groups.google.com/d/msgid/pandoc-discuss/463cce3d-8746-4ebd-b047-ddfb3ddf2467%40googlegroups.com. 
>
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
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/a1b6c7ca-eab9-4aa9-86a6-42d1e8fbcac4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

[-- Attachment #1.2: Type: text/html, Size: 14456 bytes --]

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

end of thread, other threads:[~2019-01-04  9:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-01 20:16 Lua filter to insert image with caption Thorsten Sommer
     [not found] ` <463cce3d-8746-4ebd-b047-ddfb3ddf2467-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org>
2019-01-03 20:43   ` John MacFarlane
     [not found]     ` <yh480kh8ep4gld.fsf-pgq/RBwaQ+zq8tPRBa0AtqxOck334EZe@public.gmane.org>
2019-01-04  9:12       ` Thorsten Sommer

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