[NTG-context] 'figures.getinfo()' not to exit ConTeXt

Wolfgang Schuster wolfgang.schuster.lists at gmail.com
Thu Dec 6 12:25:58 CET 2018


Procházka Lukáš Ing. schrieb am 06.12.18 um 11:20:
> Hello,
> 
> I'm using the following code, mentioned here some time ago, to get some 
> info about PDF:
> 
> ----
> \startluacode
> figures.getinfo = function(name, page)
>      if type(name) == "string" then
>          name = { name = name, page = page }
>      end
>      if name.name then
>          local data = figures.push(name)
> 
>          --figures.identify()
> local ok, res = pcall(figures.identify)
> print(">>1", ok, res)
> 
>          --figures.check()
> local ok, res = pcall(figures.check)
> print(">>2", ok, res)
> 
>          figures.pop()
>          return data
>      end
> end
> 
> local data = figures.getinfo("NonExisting.pdf")
> 
> \stopluacode
> ----
> 
> The problem is that once the PDF file doesn't exist, the function causes 
> ConTeXt to exit, even when I enclose 'figure.check()' into the 'pcall' 
> (to be handled like try/catch mechanism).
> 
> Is there a way so that the function 'figures.getinfo()' does't exit 
> ConTeXt, it just returns 'nil', or even gives info which directories (or 
> even which file types) were searched?

You can use the \doifelsefigure command for a simple check


\setupexternalfigure[location={local,global,default}]

\starttext

\doifelsefigure{cow}{YES}{NO}

\doifelsefigure{mill}{YES}{NO}

\doifelsefigure{bridge}{YES}{NO}

\stoptext


or the following stripped down version of the underlying Lua
code of the \doifelsefigure command


\setupexternalfigure[location={local,global,default}]

\starttext

\startluacode

local cow = figures.getinfo("cow")

if figures.get("status","status",0) == 0 then
   context("NO")  context.par()
else
   context("YES") context.par()
end

local mill = figures.getinfo("mill")

if figures.get("status","status",0) == 0 then
   context("NO")  context.par()
else
   context("YES") context.par()
end

local bridge = figures.getinfo("bridge")

if figures.get("status","status",0) == 0 then
   context("NO")  context.par()
else
   context("YES") context.par()
end

\stopluacode

\stoptext


Wolfgang


More information about the ntg-context mailing list