Here in Terminal on my Mac:

21 ~: lua

Lua 5.4.6  Copyright (C) 1994-2023 Lua.org, PUC-Rio

> string.gsub('a%20b', '%%20', ' ')

a b 1

> (string.gsub('a%20b', '%%20', ' '))

a b

> ^D

21 ~: 


The first call returns the 1 giving the match, the extra ()'s get rid of that second return value.

yours sincerely
dr. Hans van der Meer



On 21 Jul 2023, at 12:37, denis.maier@unibe.ch wrote:

Hi,
 
I’m typesetting an XML document where the links to the images contain spaces which are encoded as %20
<graphic xlink:href="a%20filename%20with%20spaces.jpg"/>
 
Using this works in cases without spaces:
 
\startxmlsetups xml:fig:graphic
  \externalfigure[\xmlatt{#1}{xlink:href}]
\stopxmlsetups
 
This also works for normal spaces, but is there a way to decode/expand the %20s to regular spaces?
 
I’ve tried with some Lua, but I don’t really know how to pass data around.
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\startluacode
function replaceSpaces(s)
  result = string.gsub(s, "%%20", " ")
  context(result)
end
\stopluacode
 
\define[1]\replaceSpaces{\ctxlua{replaceSpaces("#1")}}
 
\startxmlsetups xml:fig:graphic
  \externalfigure[\replaceSpaces{\xmlatt{#1}{xlink:href}}]
\stopxmlsetups
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
Any hints, why this does not work?`
 
Also, maybe there’s a simpler way for this kind of scenario?

Best,
Denis
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : https://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : https://contextgarden.net
___________________________________________________________________________________