-- Check URI
socket.http.TIMEOUT = 5 -- set URI timeout in seconds
hvdm.urireturncode = function (theuri)
-- Differentiate between file and http protocols.
local first,last = string.find(theuri, "file://")
if first == nil then
-- We have http to search for.
local content, status, authinfo = socket.http.request{
method = "HEAD",
url = theuri,
}
return type(status) == "number" and status or "failure"
else
-- We must ascertain the existence of the file.
local thefile = io.open(string.sub(theuri, last+1, -1), "r")
if thefile then
io.close(thefile)
return "200"
else
return "404"
end
end
end
% ..... Place an url link .....................................................
% BEWARE: spaces in file names must be %20 in the link !!!
% This global definition of the URLbase cannot be missed.
\def\THEURLBASE{http://}
% Signal the occurence of timeout on URI search.
\newif\ifURItimeout
\startxmlsetups xmlcommon:urlbase
\edef\THEURLBASE{\xmlstrippednolines{#1}{.}}
% Reset the URI timeout for new URLbase.
\global\URItimeoutfalse
\stopxmlsetups
\startxmlsetups xmlcommon:url
% Define the full uri.
\edef\theurl{\THEURLBASE\xmlatt{#1}{link}}
% File suffix should be one of the list.
\doifelse{\FileSuffixList{\xmlatt{#1}{link}}
{pdf,jpg,jpeg,html,png,tif,tiff}}{\empty}
{\def\suffix{.impossible}}% no suffix from the list here
{\def\suffix{\empty}}% one of the list is present
% First try link as given, beware of unreachable site.
\ifURItimeout
% For previously timeout on this site.
\else
\edef\returncode{\URIReturnCode{\theurl}}
\doif{\returncode}{failure}
{
\global\URItimeouttrue
\errorcall{#1}{url access fails for \THEURLBASE\xmlatt{#1}{link}}
}
\fi
% On URI timeout there is no hope to find the file.
\ifURItimeout
\else
% When there is no suffix from the list, try several.
\doifnot{\suffix}{\empty}
{
\scratchcounter=200\relax
\ifnum\returncode=\scratchcounter\else
\def\suffix{.pdf}
\edef\returncode{\URIReturnCode{\theurl\suffix}}
\fi
\ifnum\returncode=\scratchcounter\else
\def\suffix{.jpg}
\edef\returncode{\URIReturnCode{\theurl\suffix}}
\fi
\ifnum\returncode=\scratchcounter\else
\def\suffix{.jpeg}
\edef\returncode{\URIReturnCode{\theurl\suffix}}
\fi
\ifnum\returncode=\scratchcounter\else
\def\suffix{.html}
\edef\returncode{\URIReturnCode{\theurl\suffix}}
\fi
\ifnum\returncode=\scratchcounter\else
\def\suffix{.png}
\edef\returncode{\URIReturnCode{\theurl\suffix}}
\fi
\ifnum\returncode=\scratchcounter\else
\def\suffix{.tiff}
\edef\returncode{\URIReturnCode{\theurl\suffix}}
\fi
\ifnum\returncode=\scratchcounter\else
\def\suffix{.tif}
\edef\returncode{\URIReturnCode{\theurl\suffix}}
\fi
}
% Give up if returncode other then 200.
\ifnum\returncode=200\relax
% Page number might be added.
\doifelse{\xmlatt{#1}{page}}{\empty}
{\let\thep\empty}
{\edef\thep{\letterhash page=\xmlatt{#1}{page}}}
% Place the link, ref-attribute prevales in the presentation.
\doifelse{\xmlatt{#1}{ref}}{\empty}
{\edef\temp{\xmlatt{#1}{link}}}
{\edef\temp{\xmlatt{#1}{ref}}}
\goto
{\FirstLastCharacters{\temp}{\xmlatt{#1}{maxsize}}}
[url(\theurl\suffix\thep)]
\else
\errorcall{#1}
{\THEURLBASE\xmlatt{#1}{link} not found (\returncode)}
\fi
\fi % end of URItimeout
\stopxmlsetups